@cryptotaxi247 / kubo / commits / 20d6803dc

config: option to apply profile after init

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 1, 2017 at 03:23 UTC 20d6803dc872a9e8f17f93bbfb4dc310298c1cdd
1 file changed +54
core/commands/config.go
+54
@@ -142,6 +142,7 @@ Set the value of the 'Datastore.Path' key:
142 "show": configShowCmd,
143 "edit": configEditCmd,
144 "replace": configReplaceCmd,
145 + "profile": configProfileCmd,
146 },
147 }
148
@@ -293,6 +294,59 @@ can't be undone.
294 },
295 }
296
297 +var configProfileCmd = &cmds.Command{
298 + Helptext: cmds.HelpText{
299 + Tagline: "Apply profiles to config.",
300 + },
301 +
302 + Subcommands: map[string]*cmds.Command{
303 + "apply": configProfileApplyCmd,
304 + },
305 +}
306 +
307 +var configProfileApplyCmd = &cmds.Command{
308 + Helptext: cmds.HelpText{
309 + Tagline: "Apply profile to config.",
310 + },
311 + Arguments: []cmds.Argument{
312 + cmds.StringArg("profile", true, false, "The profile to apply to the config."),
313 + },
314 + Run: func(req cmds.Request, res cmds.Response) {
315 + args := req.Arguments()
316 +
317 + profile, ok := config.ConfigProfiles[args[0]]
318 + if !ok {
319 + res.SetError(fmt.Errorf("%s in not a profile", args[0]), cmds.ErrNormal)
320 + return
321 + }
322 +
323 + r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
324 + if err != nil {
325 + res.SetError(err, cmds.ErrNormal)
326 + return
327 + }
328 + defer r.Close()
329 +
330 + cfg, err := r.Config()
331 + if err != nil {
332 + res.SetError(err, cmds.ErrNormal)
333 + return
334 + }
335 +
336 + err = profile(cfg)
337 + if err != nil {
338 + res.SetError(err, cmds.ErrNormal)
339 + return
340 + }
341 +
342 + err = r.SetConfig(cfg)
343 + if err != nil {
344 + res.SetError(err, cmds.ErrNormal)
345 + return
346 + }
347 + },
348 +}
349 +
350 func getConfig(r repo.Repo, key string) (*ConfigField, error) {
351 value, err := r.GetConfigKey(key)
352 if err != nil {