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,12 +2,45 @@
|
||||||
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
|
||||||
|
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
|
@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
|
end
|
||||||
|
|
||||||
private def load_vaults(config_path : String)
|
private def load_vaults(config_path : String)
|
||||||
|
@ -20,5 +53,5 @@ class Config
|
||||||
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
@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