core/commands2: Added 'config' command
Matt Bell committed
Nov 5, 2014 at 16:22 UTC
7228c8fe486b539ab3ba7961c22d6639093ad7b6
2 files changed
+189
core/commands2/config.go
new
+188
@@ -0,0 +1,188 @@
1
+package commands
2
+
3
+import (
4
+ "encoding/json"
5
+ "errors"
6
+ "fmt"
7
+ "io"
8
+ "os"
9
+ "os/exec"
10
+
11
+ cmds "github.com/jbenet/go-ipfs/commands"
12
+ config "github.com/jbenet/go-ipfs/config"
13
+)
14
+
15
+type ConfigField struct {
16
+ Key string
17
+ Value interface{}
18
+}
19
+
20
+var configCmd = &cmds.Command{
21
+ Arguments: []cmds.Argument{
22
+ cmds.Argument{"key", cmds.ArgString, true, false},
23
+ cmds.Argument{"value", cmds.ArgString, false, false},
24
+ },
25
+ Help: `ipfs config <key> [value] - Get/Set ipfs config values.
26
+
27
+ ipfs config <key> - Get value of <key>
28
+ ipfs config <key> <value> - Set value of <key> to <value>
29
+ ipfs config show - Show config file
30
+ ipfs config edit - Edit config file in $EDITOR
31
+
32
+Examples:
33
+
34
+ Get the value of the 'datastore.path' key:
35
+
36
+ ipfs config datastore.path
37
+
38
+ Set the value of the 'datastore.path' key:
39
+
40
+ ipfs config datastore.path ~/.go-ipfs/datastore
41
+
42
+`,
43
+ Run: func(res cmds.Response, req cmds.Request) {
44
+ args := req.Arguments()
45
+
46
+ key, ok := args[0].(string)
47
+ if !ok {
48
+ res.SetError(errors.New("cast error"), cmds.ErrNormal)
49
+ return
50
+ }
51
+
52
+ filename, err := config.Filename(req.Context().ConfigRoot)
53
+ if err != nil {
54
+ res.SetError(err, cmds.ErrNormal)
55
+ return
56
+ }
57
+
58
+ var value string
59
+ if len(args) == 2 {
60
+ var ok bool
61
+ value, ok = args[1].(string)
62
+ if !ok {
63
+ res.SetError(errors.New("cast error"), cmds.ErrNormal)
64
+ return
65
+ }
66
+
67
+ field, err := setConfig(filename, key, value)
68
+ if err != nil {
69
+ res.SetError(err, cmds.ErrNormal)
70
+ return
71
+ }
72
+
73
+ res.SetOutput(field)
74
+ return
75
+
76
+ } else {
77
+ field, err := getConfig(filename, key)
78
+ if err != nil {
79
+ res.SetError(err, cmds.ErrNormal)
80
+ return
81
+ }
82
+
83
+ res.SetOutput(field)
84
+ return
85
+ }
86
+ },
87
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
88
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
89
+ v := res.Output().(*ConfigField)
90
+
91
+ s := ""
92
+ if len(res.Request().Arguments()) == 2 {
93
+ s += fmt.Sprintf("'%s' set to: ", v.Key)
94
+ }
95
+
96
+ marshalled, err := json.Marshal(v.Value)
97
+ if err != nil {
98
+ return nil, err
99
+ }
100
+ s += fmt.Sprintf("%s\n", marshalled)
101
+
102
+ return []byte(s), nil
103
+ },
104
+ },
105
+ Type: &ConfigField{},
106
+ Subcommands: map[string]*cmds.Command{
107
+ "show": configShowCmd,
108
+ "edit": configEditCmd,
109
+ },
110
+}
111
+
112
+var configShowCmd = &cmds.Command{
113
+ Run: func(res cmds.Response, req cmds.Request) {
114
+ filename, err := config.Filename(req.Context().ConfigRoot)
115
+ if err != nil {
116
+ res.SetError(err, cmds.ErrNormal)
117
+ return
118
+ }
119
+
120
+ reader, err := showConfig(filename)
121
+ if err != nil {
122
+ res.SetError(err, cmds.ErrNormal)
123
+ return
124
+ }
125
+
126
+ res.SetOutput(reader)
127
+ },
128
+}
129
+
130
+var configEditCmd = &cmds.Command{
131
+ Run: func(res cmds.Response, req cmds.Request) {
132
+ filename, err := config.Filename(req.Context().ConfigRoot)
133
+ if err != nil {
134
+ res.SetError(err, cmds.ErrNormal)
135
+ return
136
+ }
137
+
138
+ err = editConfig(filename)
139
+ if err != nil {
140
+ res.SetError(err, cmds.ErrNormal)
141
+ return
142
+ }
143
+ },
144
+}
145
+
146
+func getConfig(filename string, key string) (*ConfigField, error) {
147
+ value, err := config.ReadConfigKey(filename, key)
148
+ if err != nil {
149
+ return nil, fmt.Errorf("Failed to get config value: %s", err)
150
+ }
151
+
152
+ return &ConfigField{
153
+ Key: key,
154
+ Value: value,
155
+ }, nil
156
+}
157
+
158
+func setConfig(filename string, key, value string) (*ConfigField, error) {
159
+ err := config.WriteConfigKey(filename, key, value)
160
+ if err != nil {
161
+ return nil, fmt.Errorf("Failed to set config value: %s", err)
162
+ }
163
+
164
+ return getConfig(filename, key)
165
+}
166
+
167
+func showConfig(filename string) (io.Reader, error) {
168
+ // MAYBE_TODO: maybe we should omit privkey so we don't accidentally leak it?
169
+
170
+ file, err := os.Open(filename)
171
+ if err != nil {
172
+ return nil, err
173
+ }
174
+ //defer file.Close()
175
+
176
+ return file, nil
177
+}
178
+
179
+func editConfig(filename string) error {
180
+ editor := os.Getenv("EDITOR")
181
+ if editor == "" {
182
+ return errors.New("ENV variable $EDITOR not set")
183
+ }
184
+
185
+ cmd := exec.Command("sh", "-c", editor+" "+filename)
186
+ cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
187
+ return cmd.Run()
188
+}
core/commands2/root.go
+1
@@ -66,6 +66,7 @@ var rootSubcommands = map[string]*cmds.Command{
66
"pin": pinCmd,
67
"unpin": unpinCmd,
68
"version": versionCmd,
69
+ "config": configCmd,
70
71
// test subcommands
72
// TODO: remove these when we don't need them anymore