Skip to content

Commit a4db649

Browse files
committed
fix
1 parent 48cbe41 commit a4db649

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ launchr login \
2727
--keyring-passphrase-file=/path/to/your/secret
2828
```
2929

30+
To add a new key-value pair with an interactive shell:
31+
```shell
32+
launchr keyring:set your-key
33+
```
34+
35+
It's possible to parse a user value and store it as a struct in a keyring. Supported formats are [string, yaml, json]:
36+
```shell
37+
launchr keyring:set your-key value
38+
```
39+
40+
It's possible to parse a user value and store it as a struct in a keyring. Possible formats are [string, yaml, json]:
41+
```shell
42+
launchr keyring:set key --format yaml -- "- name: test-1
43+
- name: test-2"
44+
launchr keyring:set key --format yaml -- "$(cat file.yaml)"
45+
launchr keyring:set key --format json -- '[
46+
{
47+
"name": "test-1"
48+
},
49+
{
50+
"name": "test-2"
51+
}
52+
]'
53+
launchr keyring:set key --format json -- "$(cat file.json)"
54+
```
55+
3056
Flags `--keyring-passphrase` and `--keyring-passphrase-file` are available for all launchr commands, for example:
3157
```shell
3258
launchr compose --keyring-passphrase=YOURPASSHRPASE

action.set.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ action:
1717
- name: format
1818
title: Value Format
1919
description: Format to parse the input value
20-
enum: ["string", "yaml"]
20+
enum: ["string", "yaml", "json"]
2121
default: string

plugin.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,13 @@ func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) {
177177
}
178178

179179
userValue := input.Arg("value").(string)
180-
format := input.Opt("format")
181-
if format == "yaml" && userValue != "" {
182-
yamlValue, err := parseYamlString(userValue)
183-
if err != nil {
184-
return fmt.Errorf("failed to parse YAML: %w", err)
185-
}
186-
187-
key.Value = yamlValue
188-
} else {
189-
key.Value = userValue
180+
format := input.Opt("format").(string)
181+
var err error
182+
183+
// @TODO cover with tests
184+
key.Value, err = parseFromString(format, userValue)
185+
if err != nil {
186+
return fmt.Errorf("failed to parse %s: %w", format, err)
190187
}
191188

192189
return saveKey(p.k, key)

yaml.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keyring
22

33
import (
4+
"encoding/json"
45
"errors"
56
"os"
67
"strings"
@@ -236,8 +237,21 @@ func (s *dataStoreYaml) Destroy() error {
236237
return s.file.Remove()
237238
}
238239

239-
func parseYamlString(value string) (interface{}, error) {
240-
var yamlValue interface{}
241-
err := yaml.Unmarshal([]byte(value), &yamlValue)
242-
return yamlValue, err
240+
func parseFromString(format, value string) (interface{}, error) {
241+
if format == "string" || value == "" {
242+
return value, nil
243+
}
244+
245+
var parsed interface{}
246+
var err error
247+
switch format {
248+
case "json":
249+
err = json.Unmarshal([]byte(value), &parsed)
250+
case "yaml":
251+
err = yaml.Unmarshal([]byte(value), &parsed)
252+
default:
253+
panic("unsupported format: " + format)
254+
}
255+
256+
return parsed, err
243257
}

0 commit comments

Comments
 (0)