etcdctl/ctlv2: use latest Action interface

This commit is contained in:
Gyu-Ho Lee 2016-06-20 15:54:35 -07:00
parent 0ae9d444f9
commit bdca594495
19 changed files with 73 additions and 37 deletions

View File

@ -44,12 +44,14 @@ func NewAuthCommands() cli.Command {
}
}
func actionAuthEnable(c *cli.Context) {
func actionAuthEnable(c *cli.Context) error {
authEnableDisable(c, true)
return nil
}
func actionAuthDisable(c *cli.Context) {
func actionAuthDisable(c *cli.Context) error {
authEnableDisable(c, false)
return nil
}
func mustNewAuthAPI(c *cli.Context) client.AuthAPI {

View File

@ -46,7 +46,7 @@ func NewBackupCommand() cli.Command {
}
// handleBackup handles a request that intends to do a backup.
func handleBackup(c *cli.Context) {
func handleBackup(c *cli.Context) error {
var srcWAL string
var destWAL string
@ -113,4 +113,6 @@ func handleBackup(c *cli.Context) {
if err := neww.SaveSnapshot(walsnap); err != nil {
log.Fatal(err)
}
return nil
}

View File

@ -40,7 +40,7 @@ func NewClusterHealthCommand() cli.Command {
}
}
func handleClusterHealth(c *cli.Context) {
func handleClusterHealth(c *cli.Context) error {
forever := c.Bool("forever")
if forever {
sigch := make(chan os.Signal, 1)
@ -125,9 +125,10 @@ func handleClusterHealth(c *cli.Context) {
if !forever {
if health {
os.Exit(ExitSuccess)
} else {
os.Exit(ExitClusterNotHealthy)
return nil
}
os.Exit(ExitClusterNotHealthy)
return nil
}
fmt.Printf("\nnext check after 10 second...\n\n")

View File

@ -36,8 +36,9 @@ func NewExecWatchCommand() cli.Command {
cli.IntFlag{Name: "after-index", Value: 0, Usage: "watch after the given index"},
cli.BoolFlag{Name: "recursive, r", Usage: "watch all values for key and child keys"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
execWatchCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -33,8 +33,9 @@ func NewGetCommand() cli.Command {
cli.BoolFlag{Name: "sort", Usage: "returns result in sorted order"},
cli.BoolFlag{Name: "quorum, q", Usage: "require quorum for get request"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
getCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -48,7 +48,7 @@ func NewImportSnapCommand() cli.Command {
}
}
func handleImportSnap(c *cli.Context) {
func handleImportSnap(c *cli.Context) error {
d, err := ioutil.ReadFile(c.String("snap"))
if err != nil {
if c.String("snap") == "" {
@ -87,6 +87,7 @@ func handleImportSnap(c *cli.Context) {
close(setc)
wg.Wait()
fmt.Printf("finished importing %d keys\n", n)
return nil
}
func copyKeys(n *store.NodeExtern, setc chan set) int {

View File

@ -32,8 +32,9 @@ func NewLsCommand() cli.Command {
cli.BoolFlag{Name: "p", Usage: "append slash (/) to directories"},
cli.BoolFlag{Name: "quorum, q", Usage: "require quorum for get request"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
lsCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -55,7 +55,7 @@ func NewMemberCommand() cli.Command {
}
}
func actionMemberList(c *cli.Context) {
func actionMemberList(c *cli.Context) error {
if len(c.Args()) != 0 {
fmt.Fprintln(os.Stderr, "No arguments accepted")
os.Exit(1)
@ -86,9 +86,11 @@ func actionMemberList(c *cli.Context) {
fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s isLeader=%v\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","), isLeader)
}
}
return nil
}
func actionMemberAdd(c *cli.Context) {
func actionMemberAdd(c *cli.Context) error {
args := c.Args()
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "Provide a name and a single member peerURL")
@ -132,9 +134,10 @@ func actionMemberAdd(c *cli.Context) {
fmt.Printf("ETCD_NAME=%q\n", newName)
fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
return nil
}
func actionMemberRemove(c *cli.Context) {
func actionMemberRemove(c *cli.Context) error {
args := c.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "Provide a single member ID")
@ -177,9 +180,10 @@ func actionMemberRemove(c *cli.Context) {
}
fmt.Printf("Removed member %s from cluster\n", removalID)
return nil
}
func actionMemberUpdate(c *cli.Context) {
func actionMemberUpdate(c *cli.Context) error {
args := c.Args()
if len(args) != 2 {
fmt.Fprintln(os.Stderr, "Provide an ID and a list of comma separated peerURL (0xabcd http://example.com,http://example1.com)")
@ -199,4 +203,5 @@ func actionMemberUpdate(c *cli.Context) {
}
fmt.Printf("Updated member with ID %s in cluster\n", mid)
return nil
}

View File

@ -33,8 +33,9 @@ func NewMakeCommand() cli.Command {
cli.BoolFlag{Name: "in-order", Usage: "create in-order key under directory <key>"},
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
mkCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -31,8 +31,9 @@ func NewMakeDirCommand() cli.Command {
Flags: []cli.Flag{
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
mkdirCommandFunc(c, mustNewKeyAPI(c), client.PrevNoExist)
return nil
},
}
}

View File

@ -33,8 +33,9 @@ func NewRemoveCommand() cli.Command {
cli.StringFlag{Name: "with-value", Value: "", Usage: "previous value"},
cli.IntFlag{Name: "with-index", Value: 0, Usage: "previous index"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
rmCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -27,8 +27,9 @@ func NewRemoveDirCommand() cli.Command {
Name: "rmdir",
Usage: "removes the key if it is an empty directory or a key-value pair",
ArgsUsage: "<key>",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
rmdirCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -92,7 +92,7 @@ func mustNewAuthRoleAPI(c *cli.Context) client.AuthRoleAPI {
return client.NewAuthRoleAPI(hc)
}
func actionRoleList(c *cli.Context) {
func actionRoleList(c *cli.Context) error {
if len(c.Args()) != 0 {
fmt.Fprintln(os.Stderr, "No arguments accepted")
os.Exit(1)
@ -109,9 +109,11 @@ func actionRoleList(c *cli.Context) {
for _, role := range roles {
fmt.Printf("%s\n", role)
}
return nil
}
func actionRoleAdd(c *cli.Context) {
func actionRoleAdd(c *cli.Context) error {
api, role := mustRoleAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
defer cancel()
@ -128,9 +130,10 @@ func actionRoleAdd(c *cli.Context) {
}
fmt.Printf("Role %s created\n", role)
return nil
}
func actionRoleRemove(c *cli.Context) {
func actionRoleRemove(c *cli.Context) error {
api, role := mustRoleAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
err := api.RemoveRole(ctx, role)
@ -141,14 +144,17 @@ func actionRoleRemove(c *cli.Context) {
}
fmt.Printf("Role %s removed\n", role)
return nil
}
func actionRoleGrant(c *cli.Context) {
func actionRoleGrant(c *cli.Context) error {
roleGrantRevoke(c, true)
return nil
}
func actionRoleRevoke(c *cli.Context) {
func actionRoleRevoke(c *cli.Context) error {
roleGrantRevoke(c, false)
return nil
}
func roleGrantRevoke(c *cli.Context, grant bool) {
@ -214,7 +220,7 @@ func roleGrantRevoke(c *cli.Context, grant bool) {
fmt.Printf("Role %s updated\n", role)
}
func actionRoleGet(c *cli.Context) {
func actionRoleGet(c *cli.Context) error {
api, rolename := mustRoleAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
@ -233,6 +239,7 @@ func actionRoleGet(c *cli.Context) {
for _, v := range role.Permissions.KV.Write {
fmt.Printf("\t%s\n", v)
}
return nil
}
func mustRoleAPIAndName(c *cli.Context) (client.AuthRoleAPI, string) {

View File

@ -40,8 +40,9 @@ func NewSetCommand() cli.Command {
cli.StringFlag{Name: "swap-with-value", Value: "", Usage: "previous value"},
cli.IntFlag{Name: "swap-with-index", Value: 0, Usage: "previous index"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
setCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -28,8 +28,9 @@ func NewSetDirCommand() cli.Command {
Flags: []cli.Flag{
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
mkdirCommandFunc(c, mustNewKeyAPI(c), client.PrevIgnore)
return nil
},
}
}

View File

@ -32,8 +32,9 @@ func NewUpdateCommand() cli.Command {
Flags: []cli.Flag{
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
updateCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -31,8 +31,9 @@ func NewUpdateDirCommand() cli.Command {
Flags: []cli.Flag{
cli.IntFlag{Name: "ttl", Value: 0, Usage: "key time-to-live"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
updatedirCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}

View File

@ -87,7 +87,7 @@ func mustNewAuthUserAPI(c *cli.Context) client.AuthUserAPI {
return client.NewAuthUserAPI(hc)
}
func actionUserList(c *cli.Context) {
func actionUserList(c *cli.Context) error {
if len(c.Args()) != 0 {
fmt.Fprintln(os.Stderr, "No arguments accepted")
os.Exit(1)
@ -104,9 +104,10 @@ func actionUserList(c *cli.Context) {
for _, user := range users {
fmt.Printf("%s\n", user)
}
return nil
}
func actionUserAdd(c *cli.Context) {
func actionUserAdd(c *cli.Context) error {
api, userarg := mustUserAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
defer cancel()
@ -129,9 +130,10 @@ func actionUserAdd(c *cli.Context) {
}
fmt.Printf("User %s created\n", user)
return nil
}
func actionUserRemove(c *cli.Context) {
func actionUserRemove(c *cli.Context) error {
api, user := mustUserAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
err := api.RemoveUser(ctx, user)
@ -142,9 +144,10 @@ func actionUserRemove(c *cli.Context) {
}
fmt.Printf("User %s removed\n", user)
return nil
}
func actionUserPasswd(c *cli.Context) {
func actionUserPasswd(c *cli.Context) error {
api, user := mustUserAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
defer cancel()
@ -166,14 +169,17 @@ func actionUserPasswd(c *cli.Context) {
}
fmt.Printf("Password updated\n")
return nil
}
func actionUserGrant(c *cli.Context) {
func actionUserGrant(c *cli.Context) error {
userGrantRevoke(c, true)
return nil
}
func actionUserRevoke(c *cli.Context) {
func actionUserRevoke(c *cli.Context) error {
userGrantRevoke(c, false)
return nil
}
func userGrantRevoke(c *cli.Context, grant bool) {
@ -207,7 +213,7 @@ func userGrantRevoke(c *cli.Context, grant bool) {
fmt.Printf("User %s updated\n", user)
}
func actionUserGet(c *cli.Context) {
func actionUserGet(c *cli.Context) error {
api, username := mustUserAPIAndName(c)
ctx, cancel := contextWithTotalTimeout(c)
user, err := api.GetUser(ctx, username)
@ -218,7 +224,7 @@ func actionUserGet(c *cli.Context) {
}
fmt.Printf("User: %s\n", user.User)
fmt.Printf("Roles: %s\n", strings.Join(user.Roles, " "))
return nil
}
func mustUserAPIAndName(c *cli.Context) (client.AuthUserAPI, string) {

View File

@ -36,8 +36,9 @@ func NewWatchCommand() cli.Command {
cli.IntFlag{Name: "after-index", Value: 0, Usage: "watch after the given index"},
cli.BoolFlag{Name: "recursive, r", Usage: "returns all values for key and child keys"},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
watchCommandFunc(c, mustNewKeyAPI(c))
return nil
},
}
}