Skip to content

Commit 8d354fc

Browse files
authored
feat(secret): handle bytes data (#2849)
1 parent bfec5e1 commit 8d354fc

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

cmd/scw/testdata/test-all-usage-secret-version-create-usage.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
secret-id ID of the Secret
10-
[data] The base64-encoded secret payload of the SecretVersion
10+
data Content of the secret version. Base64 is handled by the SDK (Support file loading with @/path/to/file)
1111
[description] Description of the SecretVersion
1212
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par)
1313

docs/commands/secret.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ scw secret version create [arg=value ...]
198198
| Name | | Description |
199199
|------|---|-------------|
200200
| secret-id | Required | ID of the Secret |
201-
| data | | The base64-encoded secret payload of the SecretVersion |
201+
| data | Required | Content of the secret version. Base64 is handled by the SDK |
202202
| description | | Description of the SecretVersion |
203203
| region | Default: `fr-par`<br />One of: `fr-par` | Region to target. If none is passed will use default region from the config |
204204

internal/args/unmarshal.go

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ var unmarshalFuncs = map[reflect.Type]UnmarshalFunc{
107107
*(dest.(*scw.JSONObject)) = jsonObject
108108
return nil
109109
},
110+
reflect.TypeOf((*[]byte)(nil)).Elem(): func(value string, dest interface{}) error {
111+
*(dest.(*[]byte)) = []byte(value)
112+
return nil
113+
},
110114
}
111115

112116
// UnmarshalStruct parses args like ["arg1=1", "arg2=2"] to a Go structure using reflection.

internal/core/arg_file_content.go

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func loadArgsFileContent(cmd *Command, cmdArgs interface{}) error {
5656
}
5757
v.SetString(string(content))
5858
}
59+
case []byte:
60+
if strings.HasPrefix(string(i), "@") {
61+
content, err := os.ReadFile(string(i)[1:])
62+
if err != nil {
63+
return fmt.Errorf("could not open requested file: %s", err)
64+
}
65+
v.SetBytes(content)
66+
}
5967
case nil:
6068
continue
6169
default:

internal/human/marshal_func.go

+7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ func init() {
125125
}
126126
return string(data), nil
127127
})
128+
registerMarshaler(func(i []byte, opt *MarshalOpt) (string, error) {
129+
data, err := json.Marshal(i)
130+
if err != nil {
131+
return "", err
132+
}
133+
return strings.Trim(string(data), "\""), nil
134+
})
128135
}
129136

130137
func registerMarshaler[T any](marshalFunc func(i T, opt *MarshalOpt) (string, error)) {

internal/human/marshal_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Struct struct {
2424
Stringer Stringer
2525
StringerPtr *Stringer
2626
Size *scw.Size
27+
Bytes []byte
2728
}
2829

2930
type StructAny struct {
@@ -113,6 +114,7 @@ func TestMarshal(t *testing.T) {
113114
Stringer: Stringer{},
114115
StringerPtr: &Stringer{},
115116
Size: scw.SizePtr(13200),
117+
Bytes: []byte{0, 1},
116118
},
117119
result: `
118120
String This is a string
@@ -136,6 +138,7 @@ func TestMarshal(t *testing.T) {
136138
Stringer a stringer
137139
StringerPtr a stringer
138140
Size 13 kB
141+
Bytes AAE=
139142
`,
140143
}))
141144

@@ -242,7 +245,7 @@ func Test_getStructFieldsIndex(t *testing.T) {
242245
Strings: []string{"aa", "ab"}},
243246
),
244247
},
245-
want: [][]int{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}},
248+
want: [][]int{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}},
246249
},
247250
}
248251
for _, tt := range tests {

internal/namespaces/secret/v1alpha1/custom.go

+11
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,16 @@ import (
77
func GetCommands() *core.Commands {
88
cmds := GetGeneratedCommands()
99

10+
cmds.MustFind("secret", "version", "create").Override(dataCreateVersion)
1011
return cmds
1112
}
13+
14+
func dataCreateVersion(c *core.Command) *core.Command {
15+
*c.ArgSpecs.GetByName("data") = core.ArgSpec{
16+
Name: "data",
17+
Short: "Content of the secret version. Base64 is handled by the SDK",
18+
Required: true,
19+
CanLoadFile: true,
20+
}
21+
return c
22+
}

0 commit comments

Comments
 (0)