forked from glenux/mfm
feat: add support for version and verbose cli flags
This commit is contained in:
parent
9fc1ec3912
commit
27508ed5ac
2 changed files with 65 additions and 38 deletions
55
src/cli.cr
55
src/cli.cr
|
@ -8,6 +8,8 @@ require "./config"
|
||||||
require "./fzf"
|
require "./fzf"
|
||||||
|
|
||||||
module GX
|
module GX
|
||||||
|
VERSION="v0.1.9"
|
||||||
|
|
||||||
class Cli
|
class Cli
|
||||||
|
|
||||||
@config : Config
|
@config : Config
|
||||||
|
@ -29,16 +31,29 @@ module GX
|
||||||
parser.on("-c", "--config FILE", "Set configuration file") do |path|
|
parser.on("-c", "--config FILE", "Set configuration file") do |path|
|
||||||
@config.path = path
|
@config.path = path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
parser.on("-v", "--verbose", "Set more verbosity") do |flag|
|
||||||
|
@config.verbose = true
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on("--version", "Show version") do |flag|
|
||||||
|
@config.mode = Config::Mode::ShowVersion
|
||||||
|
end
|
||||||
|
|
||||||
parser.on("-h", "--help", "Show this help") do |flag|
|
parser.on("-h", "--help", "Show this help") do |flag|
|
||||||
STDOUT.puts parser
|
STDOUT.puts parser
|
||||||
exit(0)
|
exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.separator("\nCommands")
|
parser.separator("\nCommands")
|
||||||
parser.on("create", "Create vault") do
|
parser.on("config", "Manage configuration") do
|
||||||
@config.mode = Config::Mode::Add
|
parser.banner = "Usage: #{PROGRAM_NAME} config [commands] [options]\n\nGlobal options"
|
||||||
|
parser.separator("\nCommands")
|
||||||
|
|
||||||
parser.banner = "Usage: #{PROGRAM_NAME} create [options]\n\nGlobal options"
|
parser.on("create", "Create vault") do
|
||||||
|
@config.mode = Config::Mode::ConfigAdd
|
||||||
|
|
||||||
|
parser.banner = "Usage: #{PROGRAM_NAME} config create [commands] [options]\n\nGlobal options"
|
||||||
parser.separator("\nCommand options")
|
parser.separator("\nCommand options")
|
||||||
|
|
||||||
parser.on("-n", "--name", "Set vault name") do |name|
|
parser.on("-n", "--name", "Set vault name") do |name|
|
||||||
|
@ -50,7 +65,7 @@ module GX
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on("delete", "Delete vault") do
|
parser.on("delete", "Delete vault") do
|
||||||
@config.mode = Config::Mode::Add
|
@config.mode = Config::Mode::ConfigAdd
|
||||||
|
|
||||||
parser.banner = "Usage: #{PROGRAM_NAME} delete [options]\n\nGlobal options"
|
parser.banner = "Usage: #{PROGRAM_NAME} delete [options]\n\nGlobal options"
|
||||||
parser.separator("\nCommand options")
|
parser.separator("\nCommand options")
|
||||||
|
@ -61,7 +76,8 @@ module GX
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on("edit", "Edit configuration") do |flag|
|
parser.on("edit", "Edit configuration") do |flag|
|
||||||
@config.mode = Config::Mode::Edit
|
@config.mode = Config::Mode::ConfigEdit
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -71,29 +87,36 @@ module GX
|
||||||
def run()
|
def run()
|
||||||
@config.load_from_file
|
@config.load_from_file
|
||||||
|
|
||||||
|
case @config.mode
|
||||||
|
when Config::Mode::ShowVersion
|
||||||
|
STDOUT.puts "#{PROGRAM_NAME} #{VERSION}"
|
||||||
|
when Config::Mode::Mount
|
||||||
|
mount
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def mount()
|
||||||
names_display = {} of String => NamedTuple(filesystem: Filesystem, ansi_name: String)
|
names_display = {} of String => NamedTuple(filesystem: Filesystem, ansi_name: String)
|
||||||
@config.filesystems.each do |filesystem|
|
@config.filesystems.each do |filesystem|
|
||||||
fs_str = filesystem.type.ljust(12,' ')
|
fs_str = filesystem.type.ljust(12,' ')
|
||||||
result_name =
|
|
||||||
|
suffix = ""
|
||||||
|
suffix_ansi = ""
|
||||||
if filesystem.mounted?
|
if filesystem.mounted?
|
||||||
"#{fs_str} #{filesystem.name} [open]"
|
suffix = "[open]"
|
||||||
else
|
suffix_ansi = "[#{ "open".colorize(:green) }]"
|
||||||
"#{fs_str} #{filesystem.name}"
|
|
||||||
end
|
|
||||||
ansi_name =
|
|
||||||
if filesystem.mounted?
|
|
||||||
"#{fs_str.colorize(:dark_gray)} #{filesystem.name} [#{ "open".colorize(:green) }]"
|
|
||||||
else
|
|
||||||
"#{fs_str.colorize(:dark_gray)} #{filesystem.name}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
result_name = "#{fs_str} #{filesystem.name} #{suffix}".strip
|
||||||
|
ansi_name = "#{fs_str.colorize(:dark_gray)} #{filesystem.name} #{suffix_ansi}".strip
|
||||||
|
|
||||||
names_display[result_name] = {
|
names_display[result_name] = {
|
||||||
filesystem: filesystem,
|
filesystem: filesystem,
|
||||||
ansi_name: ansi_name
|
ansi_name: ansi_name
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
result_filesystem_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort)
|
result_filesystem_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort).strip
|
||||||
selected_filesystem = names_display[result_filesystem_name][:filesystem]
|
selected_filesystem = names_display[result_filesystem_name][:filesystem]
|
||||||
puts ">> #{selected_filesystem.name}".colorize(:yellow)
|
puts ">> #{selected_filesystem.name}".colorize(:yellow)
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,11 @@ require "./filesystems"
|
||||||
module GX
|
module GX
|
||||||
class Config
|
class Config
|
||||||
enum Mode
|
enum Mode
|
||||||
Add
|
ConfigAdd
|
||||||
Edit
|
ConfigDelete
|
||||||
Run
|
ConfigEdit
|
||||||
|
ShowVersion
|
||||||
|
Mount
|
||||||
end
|
end
|
||||||
|
|
||||||
record NoArgs
|
record NoArgs
|
||||||
|
@ -19,6 +21,7 @@ module GX
|
||||||
|
|
||||||
getter filesystems : Array(Filesystem)
|
getter filesystems : Array(Filesystem)
|
||||||
getter home_dir : String
|
getter home_dir : String
|
||||||
|
property verbose : Bool
|
||||||
property mode : Mode
|
property mode : Mode
|
||||||
property path : String
|
property path : String
|
||||||
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
||||||
|
@ -31,7 +34,8 @@ module GX
|
||||||
end
|
end
|
||||||
@home_dir = ENV["HOME"]
|
@home_dir = ENV["HOME"]
|
||||||
|
|
||||||
@mode = Mode::Run
|
@verbose = false
|
||||||
|
@mode = Mode::Mount
|
||||||
@filesystems = [] of Filesystem
|
@filesystems = [] of Filesystem
|
||||||
@path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH)
|
@path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH)
|
||||||
@args = NoArgs
|
@args = NoArgs
|
||||||
|
|
Loading…
Reference in a new issue