forked from glenux/mfm
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 "./config"
|
||||||
require "./fzf"
|
require "./fzf"
|
||||||
|
|
||||||
module GX
|
module GX
|
||||||
class Cli
|
class Cli
|
||||||
@home_dir : String
|
|
||||||
@config : Config
|
@config : Config
|
||||||
|
|
||||||
def initialize()
|
def initialize()
|
||||||
# Main execution starts here
|
# Main execution starts here
|
||||||
if !ENV["HOME"]?
|
@config = Config.new
|
||||||
raise "Home directory not found"
|
|
||||||
end
|
|
||||||
@home_dir = ENV["HOME"]
|
|
||||||
@config = Config.new(File.join(@home_dir, ".config/gx-vault.yml"))
|
|
||||||
|
|
||||||
## FIXME: check that FZF is installed
|
## FIXME: check that FZF is installed
|
||||||
end
|
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()
|
def run()
|
||||||
|
@config.load_from_file
|
||||||
# Correcting the fzf interaction part
|
# Correcting the fzf interaction part
|
||||||
names_display = @config.vaults.map do |vault|
|
names_display = @config.vaults.map do |vault|
|
||||||
vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name
|
vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name
|
||||||
|
@ -34,9 +81,6 @@ module GX
|
||||||
STDERR.puts "Vault not found.".colorize(:red)
|
STDERR.puts "Vault not found.".colorize(:red)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,23 +2,56 @@
|
||||||
require "./vault"
|
require "./vault"
|
||||||
|
|
||||||
module GX
|
module GX
|
||||||
class Config
|
class Config
|
||||||
getter vaults : Array(Vault)
|
enum Mode
|
||||||
|
Add
|
||||||
|
Edit
|
||||||
|
Run
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(config_path : String)
|
record NoArgs
|
||||||
@vaults = [] of Vault
|
record AddArgs, name : String, path : String
|
||||||
load_vaults(config_path)
|
record DelArgs, name : String
|
||||||
end
|
|
||||||
|
|
||||||
private def load_vaults(config_path : String)
|
getter vaults : Array(Vault)
|
||||||
yaml_data = YAML.parse(File.read(config_path))
|
getter home_dir : String
|
||||||
vaults_data = yaml_data["vaults"].as_a
|
property mode : Mode
|
||||||
|
property path : String
|
||||||
|
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
||||||
|
|
||||||
vaults_data.each do |vault_data|
|
DEFAULT_CONFIG_PATH = "gx-vault.yml"
|
||||||
name = vault_data["name"].as_s
|
|
||||||
encrypted_path = vault_data["encrypted_path"].as_s
|
def initialize()
|
||||||
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
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
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ require "./config"
|
||||||
require "./cli"
|
require "./cli"
|
||||||
|
|
||||||
app = GX::Cli.new
|
app = GX::Cli.new
|
||||||
|
app.parse_command_line(ARGV)
|
||||||
app.run
|
app.run
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue