Skip to content

Commit

Permalink
optionize reject nil value
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohui-zhangxh committed Apr 17, 2024
1 parent 31669d4 commit c7b3f68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/kamal/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def argumentize(argument, attributes, sensitive: false)

# Returns a list of shell-dashed option arguments. If the value is true, it's treated like a value-less option.
def optionize(args, with: nil)
args = flatten_args(args).reject { |(_key, value)| value.nil? }
options = if with
flatten_args(args).collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" }
args.collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" }
else
flatten_args(args).collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }
args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] }
end

options.flatten.compact
Expand Down
5 changes: 5 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class UtilsTest < ActiveSupport::TestCase
Kamal::Utils.optionize({ foo: "bar", baz: "qux", quux: true }, with: "=")
end

test "optionize reject nil value" do
assert_equal [ "--foo", "\"bar\"", "--baz", "\"qux\"" ], \
Kamal::Utils.optionize({ foo: "bar", baz: "qux", quux: nil })
end

test "no redaction from #to_s" do
assert_equal "secret", Kamal::Utils.sensitive("secret").to_s
end
Expand Down

0 comments on commit c7b3f68

Please sign in to comment.