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