Sha256: 9fe72e602ee93a255040fe98dceaefe56e4586aa2b8c63d4754e60e26aa75aec

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# Copyright (C) 2016, 2017 Szymon Kopciewski
#
# This file is part of MayamlMutt.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
require "mayaml-mutt"
require "fileutils"

def puts_ussage_message
  puts <<-END_HELP
    Ussage: mayaml-mutt-init <path_to_yaml_file> [<accounts_destination_dir>]
  END_HELP
end

def present_configs(configs)
  default_acc = configs.delete(:default)
  puts "### Default account: #{default_acc}"
  configs.each do |name, config|
    puts "### Name: #{name} " + "=" * 50
    puts config
    puts
  end
end

def store_configs(configs, destination_dir)
  FileUtils.mkdir_p destination_dir
  default_acc = configs.delete(:default)
  configs.each do |name, config|
    file_path = File.join destination_dir, name.to_s
    File.open(file_path, "w") { |f| f.write(config) }
  end
  FileUtils.ln_s(
    File.join(destination_dir, default_acc), File.join(destination_dir, "current_acc")
  )
end

def check_yaml_path(yaml_path)
  if yaml_path.nil?
    puts_ussage_message
    exit 1
  end
  return if File.exist? yaml_path
  puts "Could not find file: #{yaml_path}"
  exit 1
end

def main(args)
  yaml_path = args.shift
  destination_dir = args.shift
  check_yaml_path yaml_path
  configs = MayamlMutt.accounts_creds_from_file(yaml_path)
  destination_dir.nil? ? present_configs(configs) : store_configs(configs, destination_dir)
end

main ARGV

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mayaml-mutt-4.0.1 bin/mayaml-mutt-creds
mayaml-mutt-4.0.0 bin/mayaml-mutt-creds