/
rename.go
53 lines (43 loc) · 1.32 KB
/
rename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package extension
import (
"errors"
"github.com/raystack/salt/log"
"github.com/spf13/cobra"
"github.com/raystack/optimus/client/extension/model"
)
type renameCommand struct {
logger log.Logger
project *model.RepositoryProject
reservedCommandNames []string
}
func newRenameCommand(logger log.Logger, project *model.RepositoryProject, reservedCommandNames []string) *cobra.Command {
rename := &renameCommand{
logger: logger,
project: project,
reservedCommandNames: reservedCommandNames,
}
cmd := &cobra.Command{
Use: "rename NEW-NAME",
Short: "rename is a sub command to allow user to rename an extension command",
RunE: rename.RunE,
}
return cmd
}
func (r *renameCommand) RunE(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("one argument should be specified")
}
targetCommandName := args[0]
verbose, _ := cmd.Flags().GetBool("verbose")
manager, err := getExtensionManager(verbose, r.reservedCommandNames...)
if err != nil {
return err
}
r.logger.Info("Ranaming command [%s] to [%s] ...", r.project.CommandName, targetCommandName)
if err := manager.Rename(r.project.CommandName, targetCommandName); err != nil {
r.logger.Error("... finished with error")
return err
}
r.logger.Info("... finished successfully")
return nil
}