-
Notifications
You must be signed in to change notification settings - Fork 9
/
args.go
50 lines (41 loc) · 1.06 KB
/
args.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package args
import (
"encoding/json"
"fmt"
"sort"
"namespacelabs.dev/foundation/internal/fnerrors"
)
type ArgsListOrMap struct {
args []string
}
var _ json.Unmarshaler = &ArgsListOrMap{}
func (args *ArgsListOrMap) Parsed() []string {
if args == nil {
return nil
}
return args.args
}
func (args *ArgsListOrMap) UnmarshalJSON(contents []byte) error {
var list []string
if json.Unmarshal(contents, &list) == nil {
args.args = list
return nil
}
var m map[string]string
if json.Unmarshal(contents, &m) == nil {
for k, v := range m {
if v != "" {
args.args = append(args.args, fmt.Sprintf("--%s=%s", k, v))
} else {
args.args = append(args.args, fmt.Sprintf("--%s", k))
}
}
// Ensure deterministic arg order
sort.Strings(args.args)
return nil
}
return fnerrors.InternalError("args: expected a list of strings, or a map of string to string")
}