From 4b9c1fc3ddd54ef156f481af5308eb0021e74fd9 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Thu, 28 Feb 2019 20:13:11 +0100 Subject: [PATCH] feat: rename remote name --- cmd/remote/rename.go | 40 ++++++++++++++++++++++++++++++++++++++ pkg/config/fluckyconfig.go | 14 ++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 cmd/remote/rename.go diff --git a/cmd/remote/rename.go b/cmd/remote/rename.go new file mode 100644 index 0000000..fc44916 --- /dev/null +++ b/cmd/remote/rename.go @@ -0,0 +1,40 @@ +package remote + +import ( + "log" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" + "github.com/spf13/cobra" +) + +var renameRemoteCmd = &cobra.Command{ + Use: "rename", + Short: "Rename Remote Server", + Args: cobra.ExactArgs(2), + Example: "flucky remote rename origin slave", + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + fc, err := config.Read(cfg) + if err != nil { + log.Fatalln(err) + } + + // rename remote address + err = fc.RenameRemote(args[0], args[1]) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(fc, cfg) + if err != nil { + log.Fatalln(err) + } + + }, +} + +func init() { + remoteCmd.AddCommand(renameRemoteCmd) +} diff --git a/pkg/config/fluckyconfig.go b/pkg/config/fluckyconfig.go index 7dc5a88..cd3f005 100644 --- a/pkg/config/fluckyconfig.go +++ b/pkg/config/fluckyconfig.go @@ -360,7 +360,19 @@ func (fc *FluckyConfig) RenameSensor(oldName, newName string) error { return nil } } - return fmt.Errorf("Could not find sensor %v and replace the name", oldName) + return fmt.Errorf("Could not find sensor %v to replace into ", oldName) +} + +// RenameRemote renames a remote address identified by the name or the UUID +func (fc *FluckyConfig) RenameRemote(oldName, newName string) error { + for _, remote := range fc.Remotes { + if remote.Name == oldName || + remote.RemoteID == oldName { + remote.Name = newName + return nil + } + } + return fmt.Errorf("Could not find remote name %v to replace into %v", oldName, newName) } // ToJSON returns the struct as JSON string