-
Notifications
You must be signed in to change notification settings - Fork 30
/
params_option.go
72 lines (58 loc) · 1.24 KB
/
params_option.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"strings"
"github.com/jkcfg/jk/pkg/std"
"github.com/spf13/pflag"
)
type paramSource int
const (
paramSourceFile paramSource = iota
paramSourceCommandLine
)
// paramsOption implements a pflag.Value for script input parameters.
type paramsOption struct {
params *std.Params
source paramSource
files *[]string
}
func parameters(opts *vmOptions, source paramSource) pflag.Value {
return ¶msOption{
params: &opts.parameters,
source: source,
files: &opts.parameterFiles,
}
}
func (p *paramsOption) String() string {
return ""
}
func (p *paramsOption) setFromFile(s string) error {
params, err := std.NewParamsFromFile(s)
if err != nil {
return fmt.Errorf("%s: %v", s, err)
}
if p.files != nil {
*p.files = append(*p.files, s)
}
p.params.Merge(params)
return nil
}
func (p *paramsOption) setFromCommandline(s string) error {
parts := strings.SplitN(s, "=", 2)
path := parts[0]
v := parts[1]
p.params.SetString(path, v)
return nil
}
func (p *paramsOption) Set(s string) error {
if p.source == paramSourceFile {
return p.setFromFile(s)
}
return p.setFromCommandline(s)
}
func (p *paramsOption) Type() string {
if p.source == paramSourceFile {
return "filename"
}
return "name=value"
}