Skip to content

Commit

Permalink
experimental 'json' to get tabry JSON files from ruby CLIs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanbattaglia committed Mar 21, 2024
1 parent b543d7f commit d896676
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/tabry/cli/all_in_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/tabry/config_builder/top_level_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions lib/tabry/models/config_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/tabry/models/config_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/tabry/models/config_string_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/tabry/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(config:)
config
else
ConfigLoader.load(name: config)

end
end

Expand Down

0 comments on commit d896676

Please sign in to comment.