-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathrename.go
69 lines (54 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"errors"
"strings"
"github.com/spf13/cobra"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdRename struct {
global *cmdGlobal
}
func (c *cmdRename) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("rename", i18n.G("[<remote>:]<instance>[/<snapshot>] <instance>[/<snapshot>]"))
cmd.Short = i18n.G("Rename instances and snapshots")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Rename instances and snapshots`))
cmd.RunE = c.run
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpInstances(toComplete)
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
return cmd
}
func (c *cmdRename) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Check the remotes
sourceRemote, _, err := conf.ParseRemote(args[0])
if err != nil {
return err
}
destRemote, _, err := conf.ParseRemote(args[1])
if err != nil {
return err
}
if sourceRemote != destRemote {
// We just do renames
if strings.Contains(args[1], ":") {
return errors.New(i18n.G("Can't specify a different remote for rename"))
}
// Don't require the remote to be passed as both source and target
args[1] = sourceRemote + ":" + args[1]
}
// Call move
move := cmdMove{global: c.global}
return move.run(cmd, args)
}