-
Notifications
You must be signed in to change notification settings - Fork 17
/
set.go
95 lines (87 loc) · 3.23 KB
/
set.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package config
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
)
func init() {
setCmd := &cobra.Command{
Use: "set file=content <file2=content ...>",
Short: "Create a new factory-wide configuration",
Long: `Creates a factory wide configuration. The fioconfig daemon running on
each device will then be able to grab the latest version of the configuration
and the device's configuration and apply it. Use the --group parameter to
create a device group wide configuration instead.`,
Example: `
# Basic use
fioctl config set npmtok="root" githubtok="1234" readme.md==./readme.md
There are several ways how to pass a file content into this command:
- with filename="filecontent" format, a file content is passed directly.
- with filename==/path/to/file format, a file content is read from a specified file path.
# The configuration format also allows specifying what command to
# run after a configuration file is updated on the device. To take
# advantage of this, the "--raw" flag must be used.
cat >tmp.json <<EOF
{
"reason": "I want to use the on-changed attribute",
"files": [
{
"name": "npmtok",
"value": "root",
"on-changed": ["/usr/bin/touch", "/tmp/npmtok-changed"]
},
{
"name": "A-Readable-Value",
"value": "This won't be encrypted and will be visible from the API",
"unencrypted": true
},
{
"name": "githubtok",
"value": "1234"
}
]
}
> EOF
fioctl config set --raw ./tmp.json
# fioctl will read in tmp.json and upload it to the OTA server.
# Instead of using ./tmp.json, the command can take a "-" and will read the
# content from STDIN instead of a file.`,
Run: doConfigSet,
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(setCmd)
setCmd.Flags().StringP("group", "g", "", "Device group to use")
setCmd.Flags().StringP("reason", "m", "", "Add a message to store as the \"reason\" for this change")
setCmd.Flags().BoolP("raw", "", false, "Use raw configuration file")
setCmd.Flags().BoolP("create", "", false, "Replace the whole config with these values. Default is to merge these values in with the existing config values")
}
func doConfigSet(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
group, _ := cmd.Flags().GetString("group")
reason, _ := cmd.Flags().GetString("reason")
isRaw, _ := cmd.Flags().GetBool("raw")
shouldCreate, _ := cmd.Flags().GetBool("create")
opts := subcommands.SetConfigOptions{FileArgs: args, Reason: reason, IsRawFile: isRaw}
if group == "" {
logrus.Debugf("Creating new config for %s", factory)
opts.SetFunc = func(cfg client.ConfigCreateRequest) error {
if shouldCreate {
return api.FactoryCreateConfig(factory, cfg)
} else {
return api.FactoryPatchConfig(factory, cfg, false)
}
}
} else {
logrus.Debugf("Creating new config for %s group %s", factory, group)
opts.SetFunc = func(cfg client.ConfigCreateRequest) error {
if shouldCreate {
return api.GroupCreateConfig(factory, group, cfg)
} else {
return api.GroupPatchConfig(factory, group, cfg, false)
}
}
}
subcommands.SetConfig(&opts)
}