From d8966767f8952b99d0313a917902f0be4cd66bc3 Mon Sep 17 00:00:00 2001 From: Evan Battaglia Date: Thu, 21 Mar 2024 21:13:58 +0000 Subject: [PATCH] experimental 'json' to get tabry JSON files from ruby CLIs --- lib/tabry/cli/all_in_one.rb | 4 ++++ lib/tabry/config_builder/top_level_builder.rb | 1 + lib/tabry/models/config_list.rb | 5 +++++ lib/tabry/models/config_object.rb | 17 +++++++++++++++++ lib/tabry/models/config_string_hash.rb | 5 +++++ lib/tabry/runner.rb | 1 + 6 files changed, 33 insertions(+) diff --git a/lib/tabry/cli/all_in_one.rb b/lib/tabry/cli/all_in_one.rb index 3459de9..aa026fc 100644 --- a/lib/tabry/cli/all_in_one.rb +++ b/lib/tabry/cli/all_in_one.rb @@ -45,6 +45,10 @@ def self.completion_only(completion_conf = nil, **opts, &cmd_conf_blk) def self.define_completion_methods(cli_class, config, cmd_name: nil) cli_class.module_eval do + define_method :completion__yml do + require 'json' + puts config.as_json.to_json + end define_method :completion__bash do require_relative "../shells/bash" diff --git a/lib/tabry/config_builder/top_level_builder.rb b/lib/tabry/config_builder/top_level_builder.rb index 349a2da..6ccc488 100644 --- a/lib/tabry/config_builder/top_level_builder.rb +++ b/lib/tabry/config_builder/top_level_builder.rb @@ -23,6 +23,7 @@ def defopts(name, &blk) def completion sub :completion do desc "Get tab completion shell config" + sub :yml, "Get tabry YAML config for command" sub :bash, "Get tab completion for bash or zsh" sub :fish, "Get tab completion for fish" arg :cmd_line, "(for internal usage, when used instead of subcommand) full command line for getting completion options" diff --git a/lib/tabry/models/config_list.rb b/lib/tabry/models/config_list.rb index 995ca6f..c6aa29a 100644 --- a/lib/tabry/models/config_list.rb +++ b/lib/tabry/models/config_list.rb @@ -24,6 +24,11 @@ def to_a flatten end + def as_json + val = to_a.map { ConfigObject.as_json(_1) } + val.empty? ? nil : val + end + def flatten @flatten ||= unflattened.map(&:flatten).flatten end diff --git a/lib/tabry/models/config_object.rb b/lib/tabry/models/config_object.rb index 3e3ded6..cf64ed5 100644 --- a/lib/tabry/models/config_object.rb +++ b/lib/tabry/models/config_object.rb @@ -73,6 +73,23 @@ def init_field_boolean(key, val) assert_of_class(key, val, [TrueClass, FalseClass]) val end + + def as_json + self.class::FIELDS.map do |k, _| + raw_val = send(k) + [k, ConfigObject.as_json(raw_val)] + end.to_h.compact + end + + def self.as_json(val) + if [ ConfigObject, ConfigList, ConfigStringHash ].any? { val.is_a?(_1) } + val.as_json + elsif val.is_a?(Array) + val.map { as_json(_1) } + else + val + end + end end end end diff --git a/lib/tabry/models/config_string_hash.rb b/lib/tabry/models/config_string_hash.rb index 86e2ecb..e74b1b3 100644 --- a/lib/tabry/models/config_string_hash.rb +++ b/lib/tabry/models/config_string_hash.rb @@ -39,6 +39,11 @@ def empty? def keys to_h.keys end + + def as_json + val = to_h.transform_values { ConfigObject.as_json(_1) } + val.empty? ? nil : val + end end end end diff --git a/lib/tabry/runner.rb b/lib/tabry/runner.rb index 4acdf21..d2019ec 100644 --- a/lib/tabry/runner.rb +++ b/lib/tabry/runner.rb @@ -16,6 +16,7 @@ def initialize(config:) config else ConfigLoader.load(name: config) + end end