feat: add basic support for command line
This commit is contained in:
parent
d49a3d1261
commit
8def1979c1
3 changed files with 101 additions and 23 deletions
62
src/cli.cr
62
src/cli.cr
|
@ -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
|
||||
|
|
|
@ -2,23 +2,56 @@
|
|||
require "./vault"
|
||||
|
||||
module GX
|
||||
class Config
|
||||
getter vaults : Array(Vault)
|
||||
class Config
|
||||
enum Mode
|
||||
Add
|
||||
Edit
|
||||
Run
|
||||
end
|
||||
|
||||
def initialize(config_path : String)
|
||||
@vaults = [] of Vault
|
||||
load_vaults(config_path)
|
||||
end
|
||||
record NoArgs
|
||||
record AddArgs, name : String, path : String
|
||||
record DelArgs, name : String
|
||||
|
||||
private def load_vaults(config_path : String)
|
||||
yaml_data = YAML.parse(File.read(config_path))
|
||||
vaults_data = yaml_data["vaults"].as_a
|
||||
getter vaults : Array(Vault)
|
||||
getter home_dir : String
|
||||
property mode : Mode
|
||||
property path : String
|
||||
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
||||
|
||||
vaults_data.each do |vault_data|
|
||||
name = vault_data["name"].as_s
|
||||
encrypted_path = vault_data["encrypted_path"].as_s
|
||||
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
||||
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
|
||||
@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)
|
||||
yaml_data = YAML.parse(File.read(config_path))
|
||||
vaults_data = yaml_data["vaults"].as_a
|
||||
|
||||
vaults_data.each do |vault_data|
|
||||
name = vault_data["name"].as_s
|
||||
encrypted_path = vault_data["encrypted_path"].as_s
|
||||
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,6 +7,7 @@ require "./config"
|
|||
require "./cli"
|
||||
|
||||
app = GX::Cli.new
|
||||
app.parse_command_line(ARGV)
|
||||
app.run
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue