1
0
Fork 0
forked from glenux/mfm

feat: add basic support for command line

This commit is contained in:
Glenn Y. Rolland 2023-10-23 23:11:12 +02:00
parent d49a3d1261
commit 8def1979c1
3 changed files with 101 additions and 23 deletions

View file

@ -1,24 +1,71 @@
require "option_parser"
require "./config"
require "./fzf"
module GX
class Cli
@home_dir : String
@config : Config
def initialize()
# Main execution starts here
if !ENV["HOME"]?
raise "Home directory not found"
end
@home_dir = ENV["HOME"]
@config = Config.new(File.join(@home_dir, ".config/gx-vault.yml"))
@config = Config.new
## FIXME: check that FZF is installed
end
def parse_command_line(args)
# update
add_args = { name: "", path: "" }
delete_args = { name: "" }
pparser = OptionParser.new do |parser|
parser.banner = "Usage: gx-vault [options]\n\nGlobal options"
parser.on("-c", "--config FILE", "Set configuration file") do |path|
@config.path = path
end
parser.on("-h", "--help", "Show this help") do |flag|
STDOUT.puts parser
exit(0)
end
parser.separator("\nCommands")
parser.on("create", "Create vault") do
@config.mode = Config::Mode::Add
parser.banner = "Usage: gx-vault create [options]\n\nGlobal options"
parser.separator("\nCommand options")
parser.on("-n", "--name", "Set vault name") do |name|
add_args = add_args.merge({ name: name })
end
parser.on("-p", "--path", "Set vault encrypted path") do |path|
add_args = add_args.merge({ path: path })
end
end
parser.on("delete", "Delete vault") do
@config.mode = Config::Mode::Add
parser.banner = "Usage: gx-vault delete [options]\n\nGlobal options"
parser.separator("\nCommand options")
parser.on("-n", "--name", "Set vault name") do |name|
delete_args = delete_args.merge({ name: name })
end
end
parser.on("edit", "Edit configuration") do |flag|
@config.mode = Config::Mode::Edit
end
end
pparser.parse(args)
end
def run()
@config.load_from_file
# Correcting the fzf interaction part
names_display = @config.vaults.map do |vault|
vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name
@ -34,9 +81,6 @@ module GX
STDERR.puts "Vault not found.".colorize(:red)
end
end
end
end

View file

@ -2,12 +2,45 @@
require "./vault"
module GX
class Config
getter vaults : Array(Vault)
class Config
enum Mode
Add
Edit
Run
end
def initialize(config_path : String)
record NoArgs
record AddArgs, name : String, path : String
record DelArgs, name : String
getter vaults : Array(Vault)
getter home_dir : String
property mode : Mode
property path : String
property args : AddArgs.class | DelArgs.class | NoArgs.class
DEFAULT_CONFIG_PATH = "gx-vault.yml"
def initialize()
if !ENV["HOME"]?
raise "Home directory not found"
end
@home_dir = ENV["HOME"]
@mode = Mode::Run
@vaults = [] of Vault
load_vaults(config_path)
@path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH)
@args = NoArgs
end
def load_from_file
@vaults = [] of Vault
if !File.exists? @path
STDERR.puts "Error: file #{@path} does not exist!".colorize(:red)
exit(1)
end
load_vaults(@path)
end
private def load_vaults(config_path : String)
@ -20,5 +53,5 @@ class Config
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
end
end
end
end
end

View file

@ -7,6 +7,7 @@ require "./config"
require "./cli"
app = GX::Cli.new
app.parse_command_line(ARGV)
app.run