Skip to content

Commit

Permalink
Fix JSON::ParserError
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdebruin committed Dec 11, 2024
1 parent fbc4515 commit 841b6df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/kamal/cli/secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def fetch(*secrets)
desc "extract", "Extract a single secret from the results of a fetch call"
option :inline, type: :boolean, required: false, hidden: true
def extract(name, secrets)
parsed_secrets = JSON.parse(secrets)
parsed_secrets = parse_secrets(secrets)
value = parsed_secrets[name] || parsed_secrets.find { |k, v| k.end_with?("/#{name}") }&.last

raise "Could not find secret #{name}" if value.nil?
Expand All @@ -35,6 +35,12 @@ def print
end

private
def parse_secrets(secrets)
secrets = secrets.shellsplit[0] if secrets.start_with?("\\{")

JSON.parse(secrets)
end

def initialize_adapter(adapter)
Kamal::Secrets::Adapters.lookup(adapter)
end
Expand Down
4 changes: 4 additions & 0 deletions test/cli/secrets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class CliSecretsTest < CliTestCase
assert_equal "oof", run_command("extract", "foo", "{\"foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
end

test "extract when value contains (" do
assert_equal "bar(", run_command("extract", "foo", "\\{\\\"foo\\\":\\\"bar\(\\\"\\}")
end

test "extract match from end" do
assert_equal "oof", run_command("extract", "foo", "{\"abc/foo\":\"oof\", \"bar\":\"rab\", \"baz\":\"zab\"}")
end
Expand Down

0 comments on commit 841b6df

Please sign in to comment.