@cryptotaxi247 / kubo / commits / 8d412e48c

core/commands: Added an 'ipfs config replace' command

Matt Bell committed Dec 10, 2014 at 23:16 UTC 8d412e48cf1c8507389b43d391d0a6bbdbb4f30b
1 file changed +43 -2
core/commands/config.go
+43 -2
@@ -2,6 +2,7 @@ package commands
2
3 import (
4 "bytes"
5 + "encoding/json"
6 "errors"
7 "fmt"
8 "io"
@@ -29,6 +30,7 @@ ipfs config <key> - Get value of <key>
30 ipfs config <key> <value> - Set value of <key> to <value>
31 ipfs config show - Show config file
32 ipfs config edit - Edit config file in $EDITOR
33 +ipfs config replace <file> - Replaces the config file with <file>
34 `,
35 ShortDescription: `
36 ipfs config controls configuration variables. It works like 'git config'.
@@ -100,8 +102,9 @@ Set the value of the 'datastore.path' key:
102 },
103 Type: ConfigField{},
104 Subcommands: map[string]*cmds.Command{
103 - "show": configShowCmd,
104 - "edit": configEditCmd,
105 + "show": configShowCmd,
106 + "edit": configEditCmd,
107 + "replace": configReplaceCmd,
108 },
109 }
110
@@ -143,6 +146,35 @@ variable set to your preferred text editor.
146 },
147 }
148
149 +var configReplaceCmd = &cmds.Command{
150 + Helptext: cmds.HelpText{
151 + Tagline: "Replaces the config with <file>",
152 + ShortDescription: `
153 +Make sure to back up the config file first if neccessary, this operation
154 +can't be undone.
155 +`,
156 + },
157 +
158 + Arguments: []cmds.Argument{
159 + cmds.FileArg("file", true, false, "The file to use as the new config"),
160 + },
161 + Run: func(req cmds.Request) (interface{}, error) {
162 + r := fsrepo.At(req.Context().ConfigRoot)
163 + if err := r.Open(); err != nil {
164 + return nil, err
165 + }
166 + defer r.Close()
167 +
168 + file, err := req.Files().NextFile()
169 + if err != nil {
170 + return nil, err
171 + }
172 + defer file.Close()
173 +
174 + return nil, replaceConfig(r, file)
175 + },
176 +}
177 +
178 func getConfig(r repo.Repo, key string) (*ConfigField, error) {
179 value, err := r.GetConfigKey(key)
180 if err != nil {
@@ -183,3 +215,12 @@ func editConfig(filename string) error {
215 cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
216 return cmd.Run()
217 }
218 +
219 +func replaceConfig(r repo.Repo, file io.Reader) error {
220 + var cfg config.Config
221 + if err := json.NewDecoder(file).Decode(&cfg); err != nil {
222 + return errors.New("Failed to decode file as config")
223 + }
224 +
225 + return r.SetConfig(&cfg)
226 +}