very simple option validation

This commit is contained in:
Jorge Morante 2023-09-20 09:02:32 +02:00
parent 9ee433eb2b
commit 24a2479a97
2 changed files with 23 additions and 4 deletions

View File

@ -7,8 +7,15 @@ require "../../tmux"
class Fingers::Commands::LoadConfig < Fingers::Commands::Base class Fingers::Commands::LoadConfig < Fingers::Commands::Base
@fingers_options_names : Array(String) | Nil @fingers_options_names : Array(String) | Nil
property config : Fingers::Config
DISALLOWED_CHARS = /cimqn/ DISALLOWED_CHARS = /cimqn/
def initialize(*args)
super(*args)
@config = Fingers::Config.new
end
def run def run
validate_options! validate_options!
parse_tmux_conf parse_tmux_conf
@ -24,8 +31,6 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
Fingers.reset_config Fingers.reset_config
config = Fingers::Config.new
config.tmux_version = `tmux -V`.chomp.split(" ").last config.tmux_version = `tmux -V`.chomp.split(" ").last
options.each do |option, value| options.each do |option, value|
@ -58,6 +63,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
end end
if option.match(/pattern/) if option.match(/pattern/)
check_pattern!(value)
user_defined_patterns.push(value) user_defined_patterns.push(value)
end end
end end
@ -132,8 +138,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
def valid_option?(option) def valid_option?(option)
option_method = option_to_method(option) option_method = option_to_method(option)
# TODO validate option @config.members.includes?(option_method) || option_method.match(/pattern_[0-9]+/) || option_method == "skip_wizard"
true
end end
def fingers_options_names def fingers_options_names
@ -144,6 +149,16 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
`tmux set-option -ug #{option}` `tmux set-option -ug #{option}`
end end
def check_pattern!(pattern)
begin
Regex.new(pattern)
rescue e: ArgumentError
puts "[tmux-fingers] Invalid pattern: #{pattern}"
puts "[tmux-fingers] #{e.message}"
exit(1)
end
end
def validate_options! def validate_options!
errors = [] of String errors = [] of String

View File

@ -87,6 +87,10 @@ module Fingers
def save def save
to_json(File.open(::Fingers::Dirs::CONFIG_PATH, "w")) to_json(File.open(::Fingers::Dirs::CONFIG_PATH, "w"))
end end
def members : Array(String)
JSON.parse(to_json).as_h.keys
end
end end
def self.config def self.config