From 3e6f2986e8a1a629c1d0a89f989c4f787aab1bc7 Mon Sep 17 00:00:00 2001 From: telyn Date: Wed, 5 Dec 2018 15:29:24 +0000 Subject: [PATCH 01/23] split flags.go out into flags package --- cmd/bytemark/app/flags.go | 272 ------------------ cmd/bytemark/app/flags/account_name.go | 35 +++ cmd/bytemark/app/flags/group_name.go | 38 +++ cmd/bytemark/app/flags/preprocessor.go | 6 + cmd/bytemark/app/flags/privilege.go | 120 ++++++++ cmd/bytemark/app/flags/resize.go | 56 ++++ .../app/flags/virtual_machine_name.go | 34 +++ 7 files changed, 289 insertions(+), 272 deletions(-) delete mode 100644 cmd/bytemark/app/flags.go create mode 100644 cmd/bytemark/app/flags/account_name.go create mode 100644 cmd/bytemark/app/flags/group_name.go create mode 100644 cmd/bytemark/app/flags/preprocessor.go create mode 100644 cmd/bytemark/app/flags/privilege.go create mode 100644 cmd/bytemark/app/flags/resize.go create mode 100644 cmd/bytemark/app/flags/virtual_machine_name.go diff --git a/cmd/bytemark/app/flags.go b/cmd/bytemark/app/flags.go deleted file mode 100644 index 593740e4..00000000 --- a/cmd/bytemark/app/flags.go +++ /dev/null @@ -1,272 +0,0 @@ -package app - -import ( - "fmt" - "strings" - - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util/sizespec" - "github.com/BytemarkHosting/bytemark-client/lib" - "github.com/BytemarkHosting/bytemark-client/lib/brain" -) - -// A Preprocesser is a Flag that has a preprocess step that requires a Context -type Preprocesser interface { - Preprocess(c *Context) error -} - -// AccountNameFlag is used for all --account flags, excluding the global one. -type AccountNameFlag struct { - AccountName string - Value string - SetFromCommandLine bool -} - -// Set runs lib.ParseAccountName to make sure we get just the 'pure' account name; no cluster / endpoint details -func (name *AccountNameFlag) Set(value string) error { - name.Value = value - name.SetFromCommandLine = true - return nil -} - -// Preprocess sets the value of this flag to the global account flag if it's unset, -// and then runs lib.ParseAccountName -func (name *AccountNameFlag) Preprocess(c *Context) (err error) { - if name.Value == "" { - name.Value = c.Context.GlobalString("account") - } - name.AccountName = lib.ParseAccountName(name.Value, c.Config().GetIgnoreErr("account")) - return -} - -// String returns the AccountNameFlag as a string. -func (name AccountNameFlag) String() string { - if name.AccountName == "" { - return name.Value - } - return name.AccountName -} - -// GroupNameFlag is used for all --group flags, including the global one. -type GroupNameFlag struct { - GroupName *lib.GroupName - Value string -} - -// Set runs lib.ParseGroupName to make sure we have a valid group name -func (name *GroupNameFlag) Set(value string) error { - name.Value = value - return nil -} - -// Preprocess defaults the value of this flag to the default group from the -// config attached to the context and then runs lib.ParseGroupName -func (name *GroupNameFlag) Preprocess(c *Context) (err error) { - if name.GroupName != nil { - c.Debug("GroupNameFlag.Preprocess before %#v", *name.GroupName) - } - if name.Value == "" { - return - } - groupName := lib.ParseGroupName(name.Value, c.Config().GetGroup()) - name.GroupName = &groupName - c.Debug("GroupNameFlag.Preprocess after %#v", *name.GroupName) - return -} - -// String returns the GroupNameFlag as a string. -func (name GroupNameFlag) String() string { - if name.GroupName != nil { - return name.GroupName.String() - } - return "" -} - -// VirtualMachineNameFlag is used for all --account flags, including the global one. -type VirtualMachineNameFlag struct { - VirtualMachineName *lib.VirtualMachineName - Value string -} - -// Set runs lib.ParseVirtualMachineName using the c.Client() to make sure we have a valid group name -func (name *VirtualMachineNameFlag) Set(value string) error { - name.Value = value - return nil -} - -// Preprocess defaults the value of this flag to the default server from the -// config attached to the context and then runs lib.ParseVirtualMachineName -func (name *VirtualMachineNameFlag) Preprocess(c *Context) (err error) { - if name.Value == "" { - return - } - vmName, err := lib.ParseVirtualMachineName(name.Value, c.Config().GetVirtualMachine()) - name.VirtualMachineName = &vmName - return -} - -// String returns the VirtualMachineNameFlag as a string. -func (name VirtualMachineNameFlag) String() string { - if name.VirtualMachineName != nil { - return name.VirtualMachineName.String() - } - return "" -} - -// ResizeMode represents whether to increment a size or just to set it. -type ResizeMode int - -const ( - // ResizeModeSet will cause resize disk to set the disc size to the one specified - ResizeModeSet = iota - // ResizeModeIncrease will cause resize disk to increase the disc size by the one specified - ResizeModeIncrease -) - -// ResizeFlag is effectively an extension of SizeSpecFlag which has a ResizeMode. The Size stored in the flag is the size to set to or increase by depending on the Mode -type ResizeFlag struct { - Mode ResizeMode - Size int -} - -// Set parses the string into a ResizeFlag. If it starts with +, Mode is set to ResizeModeIncrease. Otherwise, it's set to ResizeModeSet. The rest of the string is parsed as a sizespec using sizespec.Parse -func (rf *ResizeFlag) Set(value string) (err error) { - rf.Mode = ResizeModeSet - if strings.HasPrefix(value, "+") { - rf.Mode = ResizeModeIncrease - value = value[1:] - } - - sz, err := sizespec.Parse(value) - if err != nil { - return - } - rf.Size = sz - return -} - -// String returns the size, in GiB or TiB (if the size is > 1TIB) with the unit used as a suffix. If Mode is ResizeModeIncrease, the string is prefixed with '+' -func (rf ResizeFlag) String() string { - plus := "" - if rf.Mode == ResizeModeIncrease { - plus += "+" - } - sz := rf.Size - units := "GiB" - sz /= 1024 - if sz > 1024 { - sz /= 1024 - units = "TiB" - } - return fmt.Sprintf("%s%d%s", plus, sz, units) -} - -// privArgs is an array of strings which can have an argument shifted off the front. -// there are other (better?) ways to do it - just a shift function which takes a *[]string?) -// there's nothing specific about privileges to it either except the error.. perhaps it should be ShiftableStringSlice? -// but again, a func shift(*[]string) (arg string, ok bool) is probably better. -type privArgs []string - -func (args *privArgs) shift() (arg string, err error) { - if len(*args) > 0 { - arg = (*args)[0] - *args = (*args)[1:] - } else { - err = fmt.Errorf("privileges require 3 parts - level, target and user") - } - return -} - -// PrivilegeFlag is an un-realised brain.Privilege - where the target name has been parsed but hasn't been turned into IDs yet -type PrivilegeFlag struct { - AccountName string - GroupName *lib.GroupName - VirtualMachineName *lib.VirtualMachineName - Username string - Level brain.PrivilegeLevel - Value string -} - -// TargetType returns the prefix of the PrivilegeFlag's Level. Use the brain.PrivilegeTargetType* constants for comparison -func (pf PrivilegeFlag) TargetType() string { - return strings.SplitN(string(pf.Level), "_", 2)[0] -} - -// fillPrivilegeTarget adds the object to the privilege, trying to use it as a VM, Group or Account name depending on what PrivilegeLevel the Privilege is for. The target is expected to be the NextArg at this point in the Context -func (pf *PrivilegeFlag) fillPrivilegeTarget(c *Context, args *privArgs) (err error) { - if pf.TargetType() != brain.PrivilegeTargetTypeCluster { - var target string - target, err = args.shift() - if err != nil { - return - } - if target == "on" { - target, err = args.shift() - if err != nil { - return - } - } - var vmName lib.VirtualMachineName - var groupName lib.GroupName - switch pf.TargetType() { - case brain.PrivilegeTargetTypeVM: - vmName, err = lib.ParseVirtualMachineName(target, c.Config().GetVirtualMachine()) - pf.VirtualMachineName = &vmName - case brain.PrivilegeTargetTypeGroup: - groupName = lib.ParseGroupName(target, c.Config().GetGroup()) - pf.GroupName = &groupName - case brain.PrivilegeTargetTypeAccount: - pf.AccountName = lib.ParseAccountName(target, c.Config().GetIgnoreErr("account")) - } - } - return -} - -// Set sets the privilege given some string (should be in the form " [[on] ] [to|from|for] " -func (pf *PrivilegeFlag) Set(value string) (err error) { - pf.Value = value - return nil -} - -// Preprocess parses the PrivilegeFlag and looks up the target's ID so it can -// be made into a brain.Privilege -func (pf *PrivilegeFlag) Preprocess(c *Context) (err error) { - args := privArgs(strings.Split(pf.Value, " ")) - - level, err := args.shift() - if err != nil { - return - } - pf.Level = brain.PrivilegeLevel(level) - - err = pf.fillPrivilegeTarget(c, &args) - if err != nil { - return - } - - user, err := args.shift() - if err != nil { - return - } - if user == "to" || user == "from" || user == "for" { - user, err = args.shift() - } - pf.Username = user - - if arg, err := args.shift(); err == nil { - return fmt.Errorf("Unexpected '%s' after username '%s'", arg, pf.Username) - } - - return -} - -func (pf PrivilegeFlag) String() string { - switch pf.TargetType() { - case brain.PrivilegeTargetTypeVM: - return fmt.Sprintf("%s on %s for %s", pf.Level, pf.VirtualMachineName, pf.Username) - case brain.PrivilegeTargetTypeGroup: - return fmt.Sprintf("%s on %s for %s", pf.Level, pf.GroupName, pf.Username) - case brain.PrivilegeTargetTypeAccount: - return fmt.Sprintf("%s on %s for %s", pf.Level, pf.AccountName, pf.Username) - } - return fmt.Sprintf("%s for %s", pf.Level, pf.Username) -} diff --git a/cmd/bytemark/app/flags/account_name.go b/cmd/bytemark/app/flags/account_name.go new file mode 100644 index 00000000..7b0bb19f --- /dev/null +++ b/cmd/bytemark/app/flags/account_name.go @@ -0,0 +1,35 @@ +package flags + +import "github.com/BytemarkHosting/bytemark-client/lib" + +// AccountName is used for all --account flags, excluding the global one. +type AccountName struct { + AccountName string + Value string + SetFromCommandLine bool +} + +// Set runs lib.ParseAccountName to make sure we get just the 'pure' account name; no cluster / endpoint details +func (name *AccountName) Set(value string) error { + name.Value = value + name.SetFromCommandLine = true + return nil +} + +// Preprocess sets the value of this flag to the global account flag if it's unset, +// and then runs lib.ParseAccountName +func (name *AccountName) Preprocess(c *Context) (err error) { + if name.Value == "" { + name.Value = c.Context.GlobalString("account") + } + name.AccountName = lib.ParseAccountName(name.Value, c.Config().GetIgnoreErr("account")) + return +} + +// String returns the AccountName as a string. +func (name AccountName) String() string { + if name.AccountName == "" { + return name.Value + } + return name.AccountName +} diff --git a/cmd/bytemark/app/flags/group_name.go b/cmd/bytemark/app/flags/group_name.go new file mode 100644 index 00000000..61b7ec4e --- /dev/null +++ b/cmd/bytemark/app/flags/group_name.go @@ -0,0 +1,38 @@ +package flags + +import "github.com/BytemarkHosting/bytemark-client/lib" + +// GroupName is used for all --group flags, including the global one. +type GroupName struct { + GroupName *lib.GroupName + Value string +} + +// Set runs lib.ParseGroupName to make sure we have a valid group name +func (name *GroupName) Set(value string) error { + name.Value = value + return nil +} + +// Preprocess defaults the value of this flag to the default group from the +// config attached to the context and then runs lib.ParseGroupName +func (name *GroupName) Preprocess(c *Context) (err error) { + if name.GroupName != nil { + c.Debug("GroupName.Preprocess before %#v", *name.GroupName) + } + if name.Value == "" { + return + } + groupName := lib.ParseGroupName(name.Value, c.Config().GetGroup()) + name.GroupName = &groupName + c.Debug("GroupName.Preprocess after %#v", *name.GroupName) + return +} + +// String returns the GroupName as a string. +func (name GroupName) String() string { + if name.GroupName != nil { + return name.GroupName.String() + } + return "" +} diff --git a/cmd/bytemark/app/flags/preprocessor.go b/cmd/bytemark/app/flags/preprocessor.go new file mode 100644 index 00000000..30c15f4b --- /dev/null +++ b/cmd/bytemark/app/flags/preprocessor.go @@ -0,0 +1,6 @@ +package flags + +// A Preprocesser is a Flag that has a preprocess step that requires a Context +type Preprocesser interface { + Preprocess(c *Context) error +} diff --git a/cmd/bytemark/app/flags/privilege.go b/cmd/bytemark/app/flags/privilege.go new file mode 100644 index 00000000..a20781ff --- /dev/null +++ b/cmd/bytemark/app/flags/privilege.go @@ -0,0 +1,120 @@ +package flags + +import ( + "fmt" + "strings" + + "github.com/BytemarkHosting/bytemark-client/lib" + "github.com/BytemarkHosting/bytemark-client/lib/brain" +) + +// privArgs is an array of strings which can have an argument shifted off the front. +// there are other (better?) ways to do it - just a shift function which takes a *[]string?) +// there's nothing specific about privileges to it either except the error.. perhaps it should be ShiftableStringSlice? +// but again, a func shift(*[]string) (arg string, ok bool) is probably better. +type privArgs []string + +func (args *privArgs) shift() (arg string, err error) { + if len(*args) > 0 { + arg = (*args)[0] + *args = (*args)[1:] + } else { + err = fmt.Errorf("privileges require 3 parts - level, target and user") + } + return +} + +// Privilege is an un-realised brain.Privilege - where the target name has been parsed but hasn't been turned into IDs yet +type Privilege struct { + AccountName string + GroupName *lib.GroupName + VirtualMachineName *lib.VirtualMachineName + Username string + Level brain.PrivilegeLevel + Value string +} + +// TargetType returns the prefix of the Privilege's Level. Use the brain.PrivilegeTargetType* constants for comparison +func (pf Privilege) TargetType() string { + return strings.SplitN(string(pf.Level), "_", 2)[0] +} + +// fillPrivilegeTarget adds the object to the privilege, trying to use it as a VM, Group or Account name depending on what PrivilegeLevel the Privilege is for. The target is expected to be the NextArg at this point in the Context +func (pf *Privilege) fillPrivilegeTarget(c *Context, args *privArgs) (err error) { + if pf.TargetType() != brain.PrivilegeTargetTypeCluster { + var target string + target, err = args.shift() + if err != nil { + return + } + if target == "on" { + target, err = args.shift() + if err != nil { + return + } + } + var vmName lib.VirtualMachineName + var groupName lib.GroupName + switch pf.TargetType() { + case brain.PrivilegeTargetTypeVM: + vmName, err = lib.ParseVirtualMachineName(target, c.Config().GetVirtualMachine()) + pf.VirtualMachineName = &vmName + case brain.PrivilegeTargetTypeGroup: + groupName = lib.ParseGroupName(target, c.Config().GetGroup()) + pf.GroupName = &groupName + case brain.PrivilegeTargetTypeAccount: + pf.AccountName = lib.ParseAccountName(target, c.Config().GetIgnoreErr("account")) + } + } + return +} + +// Set sets the privilege given some string (should be in the form " [[on] ] [to|from|for] " +func (pf *Privilege) Set(value string) (err error) { + pf.Value = value + return nil +} + +// Preprocess parses the Privilege and looks up the target's ID so it can +// be made into a brain.Privilege +func (pf *Privilege) Preprocess(c *Context) (err error) { + args := privArgs(strings.Split(pf.Value, " ")) + + level, err := args.shift() + if err != nil { + return + } + pf.Level = brain.PrivilegeLevel(level) + + err = pf.fillPrivilegeTarget(c, &args) + if err != nil { + return + } + + user, err := args.shift() + if err != nil { + return + } + if user == "to" || user == "from" || user == "for" { + user, err = args.shift() + } + pf.Username = user + + if arg, err := args.shift(); err == nil { + return fmt.Errorf("Unexpected '%s' after username '%s'", arg, pf.Username) + } + + return +} + +func (pf Privilege) String() string { + switch pf.TargetType() { + case brain.PrivilegeTargetTypeVM: + return fmt.Sprintf("%s on %s for %s", pf.Level, pf.VirtualMachineName, pf.Username) + case brain.PrivilegeTargetTypeGroup: + return fmt.Sprintf("%s on %s for %s", pf.Level, pf.GroupName, pf.Username) + case brain.PrivilegeTargetTypeAccount: + return fmt.Sprintf("%s on %s for %s", pf.Level, pf.AccountName, pf.Username) + } + return fmt.Sprintf("%s for %s", pf.Level, pf.Username) +} diff --git a/cmd/bytemark/app/flags/resize.go b/cmd/bytemark/app/flags/resize.go new file mode 100644 index 00000000..d242e012 --- /dev/null +++ b/cmd/bytemark/app/flags/resize.go @@ -0,0 +1,56 @@ +package flags + +import ( + "fmt" + "strings" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util/sizespec" +) + +// ResizeMode represents whether to increment a size or just to set it. +type ResizeMode int + +const ( + // ResizeModeSet will cause resize disk to set the disc size to the one specified + ResizeModeSet ResizeMode = iota + // ResizeModeIncrease will cause resize disk to increase the disc size by the one specified + ResizeModeIncrease +) + +// Resize is effectively an extension of SizeSpecFlag which has a ResizeMode. The Size stored in the flag is the size to set to or increase by depending on the Mode +type Resize struct { + Mode ResizeMode + Size int +} + +// Set parses the string into a Resize. If it starts with +, Mode is set to ResizeModeIncrease. Otherwise, it's set to ResizeModeSet. The rest of the string is parsed as a sizespec using sizespec.Parse +func (rf *Resize) Set(value string) (err error) { + rf.Mode = ResizeModeSet + if strings.HasPrefix(value, "+") { + rf.Mode = ResizeModeIncrease + value = value[1:] + } + + sz, err := sizespec.Parse(value) + if err != nil { + return + } + rf.Size = sz + return +} + +// String returns the size, in GiB or TiB (if the size is > 1TIB) with the unit used as a suffix. If Mode is ResizeModeIncrease, the string is prefixed with '+' +func (rf Resize) String() string { + plus := "" + if rf.Mode == ResizeModeIncrease { + plus += "+" + } + sz := rf.Size + units := "GiB" + sz /= 1024 + if sz > 1024 { + sz /= 1024 + units = "TiB" + } + return fmt.Sprintf("%s%d%s", plus, sz, units) +} diff --git a/cmd/bytemark/app/flags/virtual_machine_name.go b/cmd/bytemark/app/flags/virtual_machine_name.go new file mode 100644 index 00000000..f169921e --- /dev/null +++ b/cmd/bytemark/app/flags/virtual_machine_name.go @@ -0,0 +1,34 @@ +package flags + +import "github.com/BytemarkHosting/bytemark-client/lib" + +// VirtualMachineName is used for all --server flags, or should be at least. +type VirtualMachineName struct { + VirtualMachineName *lib.VirtualMachineName + Value string +} + +// Set runs lib.ParseVirtualMachineName using the c.Client() to make sure we have a valid group name +func (name *VirtualMachineName) Set(value string) error { + name.Value = value + return nil +} + +// Preprocess defaults the value of this flag to the default server from the +// config attached to the context and then runs lib.ParseVirtualMachineName +func (name *VirtualMachineName) Preprocess(c *Context) (err error) { + if name.Value == "" { + return + } + vmName, err := lib.ParseVirtualMachineName(name.Value, c.Config().GetVirtualMachine()) + name.VirtualMachineName = &vmName + return +} + +// String returns the VirtualMachineName as a string. +func (name VirtualMachineName) String() string { + if name.VirtualMachineName != nil { + return name.VirtualMachineName.String() + } + return "" +} From 3e4168fbd8c313bd0fd1fa7cbf12aea31db52478 Mon Sep 17 00:00:00 2001 From: telyn Date: Wed, 5 Dec 2018 15:34:58 +0000 Subject: [PATCH 02/23] update references to app.[bla]Flag --- cmd/bytemark/add.go | 2 +- cmd/bytemark/app/with/with.go | 8 ++++---- cmd/bytemark/commands/add/disc.go | 2 +- cmd/bytemark/commands/add/group.go | 2 +- cmd/bytemark/commands/add/server.go | 2 +- cmd/bytemark/commands/admin/add/user.go | 2 +- cmd/bytemark/commands/admin/add/vlan_group.go | 2 +- cmd/bytemark/commands/admin/add/vm_default.go | 4 ++-- cmd/bytemark/commands/admin/migrate/server.go | 2 +- cmd/bytemark/commands/admin/set.go | 2 +- cmd/bytemark/commands/admin/update.go | 2 +- cmd/bytemark/commands/assent.go | 4 ++-- cmd/bytemark/commands/backup.go | 2 +- cmd/bytemark/commands/delete/group.go | 2 +- cmd/bytemark/commands/grant.go | 2 +- cmd/bytemark/commands/reset_server.go | 2 +- cmd/bytemark/commands/restart_server.go | 2 +- cmd/bytemark/commands/revoke.go | 2 +- cmd/bytemark/commands/show/backups.go | 2 +- cmd/bytemark/commands/show/discs.go | 2 +- cmd/bytemark/commands/show/groups.go | 2 +- cmd/bytemark/commands/show/servers.go | 2 +- cmd/bytemark/commands/shutdown_server.go | 2 +- cmd/bytemark/commands/start_server.go | 2 +- cmd/bytemark/commands/stop_server.go | 2 +- cmd/bytemark/commands/update/disc.go | 6 +++--- cmd/bytemark/commands/update/server.go | 4 ++-- cmd/bytemark/console.go | 2 +- cmd/bytemark/delete.go | 6 +++--- cmd/bytemark/reimage.go | 2 +- cmd/bytemark/restore.go | 4 ++-- cmd/bytemark/schedule.go | 2 +- cmd/bytemark/show.go | 12 ++++++------ cmd/bytemark/unschedule.go | 2 +- 34 files changed, 50 insertions(+), 50 deletions(-) diff --git a/cmd/bytemark/add.go b/cmd/bytemark/add.go index e316a0e9..92f6fa54 100644 --- a/cmd/bytemark/add.go +++ b/cmd/bytemark/add.go @@ -99,7 +99,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "The server to add IPs to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) error { diff --git a/cmd/bytemark/app/with/with.go b/cmd/bytemark/app/with/with.go index 4879eb84..4d2ae534 100644 --- a/cmd/bytemark/app/with/with.go +++ b/cmd/bytemark/app/with/with.go @@ -32,15 +32,15 @@ func flagValueIsOK(c *app.Context, flag cli.Flag) bool { switch realFlag := flag.(type) { case cli.GenericFlag: switch value := realFlag.Value.(type) { - case *app.VirtualMachineNameFlag: + case *flags.VirtualMachineName: return value.VirtualMachineName != nil - case *app.GroupNameFlag: + case *flags.GroupName: return value.GroupName != nil - case *app.AccountNameFlag: + case *flags.AccountName: return value.AccountName != "" case *util.SizeSpecFlag: return *value != 0 - case *app.PrivilegeFlag: + case *flags.Privilege: return value.Username != "" && value.Level != "" } case cli.StringFlag: diff --git a/cmd/bytemark/commands/add/disc.go b/cmd/bytemark/commands/add/disc.go index 93997add..707e2498 100644 --- a/cmd/bytemark/commands/add/disc.go +++ b/cmd/bytemark/commands/add/disc.go @@ -24,7 +24,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to add the disc to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Usage: "add virtual discs attached to one of your cloud servers", diff --git a/cmd/bytemark/commands/add/group.go b/cmd/bytemark/commands/add/group.go index f0e18ab9..8bb09ee6 100644 --- a/cmd/bytemark/commands/add/group.go +++ b/cmd/bytemark/commands/add/group.go @@ -19,7 +19,7 @@ func init() { cli.GenericFlag{ Name: "group", Usage: "the name of the group to add", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, }, Action: app.Action(args.Optional("group"), with.RequiredFlags("group"), with.Auth, createGroup), diff --git a/cmd/bytemark/commands/add/server.go b/cmd/bytemark/commands/add/server.go index f414a79d..6abe3644 100644 --- a/cmd/bytemark/commands/add/server.go +++ b/cmd/bytemark/commands/add/server.go @@ -45,7 +45,7 @@ If --hwprofile-locked is set then the cloud server's virtual hardware won't be c cli.GenericFlag{ Name: "name", Usage: "The new server's name", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.GenericFlag{ Name: "ip", diff --git a/cmd/bytemark/commands/admin/add/user.go b/cmd/bytemark/commands/admin/add/user.go index dec06a6e..2c9ccf74 100644 --- a/cmd/bytemark/commands/admin/add/user.go +++ b/cmd/bytemark/commands/admin/add/user.go @@ -25,7 +25,7 @@ func init() { }, }, Action: app.Action(args.Optional("username", "privilege"), with.RequiredFlags("username", "privilege"), with.Auth, func(c *app.Context) error { - // Privilege is just a string and not a app.PrivilegeFlag, since it can only be "cluster_admin" or "cluster_su" + // Privilege is just a string and not a flags.Privilege, since it can only be "cluster_admin" or "cluster_su" if err := c.Client().CreateUser(c.String("username"), c.String("privilege")); err != nil { return err } diff --git a/cmd/bytemark/commands/admin/add/vlan_group.go b/cmd/bytemark/commands/admin/add/vlan_group.go index 356dcd74..3fca1f09 100644 --- a/cmd/bytemark/commands/admin/add/vlan_group.go +++ b/cmd/bytemark/commands/admin/add/vlan_group.go @@ -21,7 +21,7 @@ Used when setting up a private VLAN for a customer.`, cli.GenericFlag{ Name: "group", Usage: "the name of the group to add", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, cli.IntFlag{ Name: "vlan-num", diff --git a/cmd/bytemark/commands/admin/add/vm_default.go b/cmd/bytemark/commands/admin/add/vm_default.go index be1650cf..2c51d5c9 100644 --- a/cmd/bytemark/commands/admin/add/vm_default.go +++ b/cmd/bytemark/commands/admin/add/vm_default.go @@ -28,7 +28,7 @@ Multiple --disc flags can be used to add multiple discs to the VM Default If --backup is set then a backup of the first disk will be taken at the frequency specified - never, daily, weekly or monthly. If not specified the backup will default to weekly.`, - Flags: cliutil.ConcatFlags(app.OutputFlags("vm default", "object"), + Flags: cliutil.ConcatFlags(flags.Outputs("vm default", "object"), flagsets.ImageInstallFlags, flagsets.ServerSpecFlags, []cli.Flag{ cli.StringFlag{ @@ -42,7 +42,7 @@ frequency specified - never, daily, weekly or monthly. If not specified the back cli.GenericFlag{ Name: "account", Usage: "the account to add the default to (will use 'bytemark' if unset)", - Value: new(app.AccountNameFlag), + Value: new(flags.AccountName), }, }), Action: app.Action(args.Optional("default-name"), with.RequiredFlags("default-name"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/admin/migrate/server.go b/cmd/bytemark/commands/admin/migrate/server.go index 4c67fe33..bb017fd0 100644 --- a/cmd/bytemark/commands/admin/migrate/server.go +++ b/cmd/bytemark/commands/admin/migrate/server.go @@ -18,7 +18,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to migrate", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.StringFlag{ Name: "new-head", diff --git a/cmd/bytemark/commands/admin/set.go b/cmd/bytemark/commands/admin/set.go index 093bc3c5..c95e8000 100644 --- a/cmd/bytemark/commands/admin/set.go +++ b/cmd/bytemark/commands/admin/set.go @@ -27,7 +27,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server the disc belongs to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.IntFlag{ Name: "iops-limit", diff --git a/cmd/bytemark/commands/admin/update.go b/cmd/bytemark/commands/admin/update.go index 69b1ab08..34f641c9 100644 --- a/cmd/bytemark/commands/admin/update.go +++ b/cmd/bytemark/commands/admin/update.go @@ -211,7 +211,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to migrate", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.Int64Flag{ Name: "migrate-speed", diff --git a/cmd/bytemark/commands/assent.go b/cmd/bytemark/commands/assent.go index 3542f101..ffb3fa35 100644 --- a/cmd/bytemark/commands/assent.go +++ b/cmd/bytemark/commands/assent.go @@ -29,7 +29,7 @@ func init() { cli.GenericFlag{ Name: "account", Usage: "the account which is assenting", - Value: new(app.AccountNameFlag), + Value: new(flags.AccountName), }, cli.IntFlag{ Name: "accountid", @@ -46,7 +46,7 @@ func init() { }, Action: app.Action(with.RequiredFlags("agreement", "person"), func(ctx *app.Context) error { accountString := ctx.String("account") - accountNameFlag := ctx.Context.Generic("account").(*app.AccountNameFlag) + accountNameFlag := ctx.Context.Generic("account").(*flags.AccountName) accountID := ctx.Int("accountid") var account billing.Account diff --git a/cmd/bytemark/commands/backup.go b/cmd/bytemark/commands/backup.go index 906f2c9d..27af8396 100644 --- a/cmd/bytemark/commands/backup.go +++ b/cmd/bytemark/commands/backup.go @@ -28,7 +28,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server whose disk you wish to backup", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server", "disc"), with.RequiredFlags("server", "disc"), with.Auth, func(c *app.Context) error { diff --git a/cmd/bytemark/commands/delete/group.go b/cmd/bytemark/commands/delete/group.go index 739aa603..e8e1db5f 100644 --- a/cmd/bytemark/commands/delete/group.go +++ b/cmd/bytemark/commands/delete/group.go @@ -28,7 +28,7 @@ If --recursive is specified, all servers in the group will be purged. Otherwise, cli.GenericFlag{ Name: "group", Usage: "the name of the group to delete", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, flagsets.Force, }, diff --git a/cmd/bytemark/commands/grant.go b/cmd/bytemark/commands/grant.go index 4312472c..04d66bfc 100644 --- a/cmd/bytemark/commands/grant.go +++ b/cmd/bytemark/commands/grant.go @@ -43,7 +43,7 @@ func init() { cli.GenericFlag{ Name: "privilege", Usage: "A privilege written out like ' [on] [to] ", - Value: new(app.PrivilegeFlag), + Value: new(flags.Privilege), }, }, Action: app.Action(args.Join("privilege"), with.RequiredFlags("privilege"), with.Privilege("privilege"), func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/reset_server.go b/cmd/bytemark/commands/reset_server.go index dac8ad66..bbfdf2b8 100644 --- a/cmd/bytemark/commands/reset_server.go +++ b/cmd/bytemark/commands/reset_server.go @@ -11,7 +11,7 @@ func init() { serverFlag := cli.GenericFlag{ Name: "server", Usage: "the server to reset", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), } Commands = append(Commands, cli.Command{ diff --git a/cmd/bytemark/commands/restart_server.go b/cmd/bytemark/commands/restart_server.go index 8f1f4a06..f4215035 100644 --- a/cmd/bytemark/commands/restart_server.go +++ b/cmd/bytemark/commands/restart_server.go @@ -26,7 +26,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to restart", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.BoolFlag{ Name: "rescue", diff --git a/cmd/bytemark/commands/revoke.go b/cmd/bytemark/commands/revoke.go index e5c65771..335f9b96 100644 --- a/cmd/bytemark/commands/revoke.go +++ b/cmd/bytemark/commands/revoke.go @@ -30,7 +30,7 @@ func init() { cli.GenericFlag{ Name: "privilege", Usage: "the privilege to revoke", - Value: new(app.PrivilegeFlag), + Value: new(flags.Privilege), }, }, Action: app.Action(args.Join("privilege"), with.RequiredFlags("privilege"), with.Privilege("privilege"), func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/show/backups.go b/cmd/bytemark/commands/show/backups.go index 277b120d..12b2a47f 100644 --- a/cmd/bytemark/commands/show/backups.go +++ b/cmd/bytemark/commands/show/backups.go @@ -23,7 +23,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server you wish to list the backups of", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, ), Action: app.Action(args.Optional("server", "disc"), with.RequiredFlags("server", "disc"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/show/discs.go b/cmd/bytemark/commands/show/discs.go index a52f1196..0553f756 100644 --- a/cmd/bytemark/commands/show/discs.go +++ b/cmd/bytemark/commands/show/discs.go @@ -22,7 +22,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server whose discs you wish to list", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, ), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) error { diff --git a/cmd/bytemark/commands/show/groups.go b/cmd/bytemark/commands/show/groups.go index 64d9060f..6d26b348 100644 --- a/cmd/bytemark/commands/show/groups.go +++ b/cmd/bytemark/commands/show/groups.go @@ -18,7 +18,7 @@ func init() { cli.GenericFlag{ Name: "account", Usage: "the account to list the groups of", - Value: new(app.AccountNameFlag), + Value: new(flags.AccountName), }, ), Action: app.Action(args.Optional("account"), with.RequiredFlags("account"), with.Account("account"), func(c *app.Context) error { diff --git a/cmd/bytemark/commands/show/servers.go b/cmd/bytemark/commands/show/servers.go index a4881e7b..c164b268 100644 --- a/cmd/bytemark/commands/show/servers.go +++ b/cmd/bytemark/commands/show/servers.go @@ -24,7 +24,7 @@ If --group and --account are specified, the group will be displayed and the acco cli.GenericFlag{ Name: "group", Usage: "the group to list the servers of", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, // TODO: change to AccountNameFlag cli.StringFlag{ diff --git a/cmd/bytemark/commands/shutdown_server.go b/cmd/bytemark/commands/shutdown_server.go index 1618b783..0fbee23b 100644 --- a/cmd/bytemark/commands/shutdown_server.go +++ b/cmd/bytemark/commands/shutdown_server.go @@ -24,7 +24,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to shutdown", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/start_server.go b/cmd/bytemark/commands/start_server.go index bde573fb..72958f9d 100644 --- a/cmd/bytemark/commands/start_server.go +++ b/cmd/bytemark/commands/start_server.go @@ -23,7 +23,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to start", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/stop_server.go b/cmd/bytemark/commands/stop_server.go index 34116f84..d7a75b8e 100644 --- a/cmd/bytemark/commands/stop_server.go +++ b/cmd/bytemark/commands/stop_server.go @@ -24,7 +24,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to stop", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/update/disc.go b/cmd/bytemark/commands/update/disc.go index a0d1e286..9bbb325d 100644 --- a/cmd/bytemark/commands/update/disc.go +++ b/cmd/bytemark/commands/update/disc.go @@ -33,17 +33,17 @@ Moving the disc to another server may require you to update your operating syste cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.GenericFlag{ Name: "new-size", Usage: "the new size for the disc. Prefix with + to indicate 'increase by'", - Value: new(app.ResizeFlag), + Value: new(flags.Resize), }, cli.GenericFlag{ Name: "new-server", Usage: "the server that the disc should be moved to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server", "disc", "new-size", "new-server"), with.RequiredFlags("server", "disc"), with.Disc("server", "disc"), updateDisc), diff --git a/cmd/bytemark/commands/update/server.go b/cmd/bytemark/commands/update/server.go index 31ca615a..6e81f16a 100644 --- a/cmd/bytemark/commands/update/server.go +++ b/cmd/bytemark/commands/update/server.go @@ -65,7 +65,7 @@ EXAMPLES cli.GenericFlag{ Name: "new-name", Usage: "A new name for the server", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.IntFlag{ Name: "cores", @@ -82,7 +82,7 @@ EXAMPLES cli.GenericFlag{ Name: "server", Usage: "The server to update", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, ), Action: app.Action(args.Optional("new-name", "hwprofile", "memory"), with.RequiredFlags("server"), with.VirtualMachine("server"), with.Auth, updateServer), diff --git a/cmd/bytemark/console.go b/cmd/bytemark/console.go index 7129f0c3..191d9e45 100644 --- a/cmd/bytemark/console.go +++ b/cmd/bytemark/console.go @@ -48,7 +48,7 @@ Defaults to connecting to the serial console for the given server.`, cli.GenericFlag{ Name: "server", Usage: "The server whose console will be connected to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(ctx *app.Context) (err error) { diff --git a/cmd/bytemark/delete.go b/cmd/bytemark/delete.go index 2f6f9503..c642167d 100644 --- a/cmd/bytemark/delete.go +++ b/cmd/bytemark/delete.go @@ -38,7 +38,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server whose disc you wish to delete, must provide a label too", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.StringFlag{ Name: "id", @@ -108,7 +108,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to delete", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), deleteServer), @@ -125,7 +125,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to delete a backup from", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.StringFlag{ Name: "backup", diff --git a/cmd/bytemark/reimage.go b/cmd/bytemark/reimage.go index b224ec5c..89191a7e 100644 --- a/cmd/bytemark/reimage.go +++ b/cmd/bytemark/reimage.go @@ -39,7 +39,7 @@ The root password will be output on stdout if the imaging succeeded, otherwise n forceFlag, cli.GenericFlag{ Name: "server", Usage: "the server to reimage", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/restore.go b/cmd/bytemark/restore.go index 5587c5e7..ae9318db 100644 --- a/cmd/bytemark/restore.go +++ b/cmd/bytemark/restore.go @@ -26,7 +26,7 @@ Note that it cannot be used to restore a server that has been permanently delete cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) (err error) { @@ -57,7 +57,7 @@ Note that it cannot be used to restore a server that has been permanently delete cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.StringFlag{ Name: "backup", diff --git a/cmd/bytemark/schedule.go b/cmd/bytemark/schedule.go index 5512471f..5d968f94 100644 --- a/cmd/bytemark/schedule.go +++ b/cmd/bytemark/schedule.go @@ -40,7 +40,7 @@ bytemark schedule backups --start "2017-04-05T14:37:00+02:00" fileserver very-im cli.GenericFlag{ Name: "server", Usage: "the server the disc belongs to", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.IntFlag{ Name: "interval", diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index 8021a304..f4ecc42c 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -32,7 +32,7 @@ If the --json flag is specified, prints a complete overview of the account in JS cli.GenericFlag{ Name: "account", Usage: "The account to view", - Value: new(app.AccountNameFlag), + Value: new(flags.AccountName), }), Action: app.Action(args.Optional("account"), with.Account("account"), func(c *app.Context) error { c.Debug("show account command output") @@ -48,7 +48,7 @@ If the --json flag is specified, prints a complete overview of the account in JS cli.GenericFlag{ Name: "server", Usage: "the server to display", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, cli.StringFlag{ Name: "disc", @@ -68,7 +68,7 @@ If the --json flag is specified, prints a complete overview of the group in JSON cli.GenericFlag{ Name: "group", Usage: "The name of the group to show", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, ), Action: app.Action(args.Optional("group"), with.Group("group"), func(c *app.Context) error { @@ -83,7 +83,7 @@ If the --json flag is specified, prints a complete overview of the group in JSON cli.GenericFlag{ Name: "server", Usage: "the server to display", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, ), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) error { @@ -132,12 +132,12 @@ Privileges will be output in no particular order.`, cli.GenericFlag{ Name: "group", Usage: "The group to show the privileges of", - Value: new(app.GroupNameFlag), + Value: new(flags.GroupName), }, cli.GenericFlag{ Name: "server", Usage: "The server to show the privileges of", - Value: new(app.VirtualMachineNameFlag), + Value: new(flags.VirtualMachineName), }, ), Action: app.Action(with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/unschedule.go b/cmd/bytemark/unschedule.go index 8e7bb633..4acbe94a 100644 --- a/cmd/bytemark/unschedule.go +++ b/cmd/bytemark/unschedule.go @@ -33,7 +33,7 @@ The is a number that can be found out using 'bytemark show disc Date: Wed, 5 Dec 2018 15:36:02 +0000 Subject: [PATCH 03/23] run goimports over cmd/bytemark --- cmd/bytemark/add.go | 1 + cmd/bytemark/app/with/with.go | 1 + cmd/bytemark/commands/add/disc.go | 1 + cmd/bytemark/commands/add/group.go | 1 + cmd/bytemark/commands/add/server.go | 1 + cmd/bytemark/commands/admin/add/vlan_group.go | 1 + cmd/bytemark/commands/admin/migrate/server.go | 1 + cmd/bytemark/commands/admin/set.go | 1 + cmd/bytemark/commands/admin/update.go | 1 + cmd/bytemark/commands/assent.go | 1 + cmd/bytemark/commands/backup.go | 1 + cmd/bytemark/commands/delete/group.go | 1 + cmd/bytemark/commands/grant.go | 1 + cmd/bytemark/commands/reset_server.go | 1 + cmd/bytemark/commands/restart_server.go | 1 + cmd/bytemark/commands/revoke.go | 1 + cmd/bytemark/commands/show/backups.go | 1 + cmd/bytemark/commands/show/discs.go | 1 + cmd/bytemark/commands/show/groups.go | 1 + cmd/bytemark/commands/show/servers.go | 1 + cmd/bytemark/commands/shutdown_server.go | 1 + cmd/bytemark/commands/start_server.go | 1 + cmd/bytemark/commands/stop_server.go | 1 + cmd/bytemark/commands/update/disc.go | 1 + cmd/bytemark/commands/update/server.go | 1 + cmd/bytemark/console.go | 1 + cmd/bytemark/console_test.go | 4 ++-- cmd/bytemark/delete.go | 1 + cmd/bytemark/reimage.go | 1 + cmd/bytemark/restore.go | 1 + cmd/bytemark/schedule.go | 1 + cmd/bytemark/show.go | 1 + cmd/bytemark/unschedule.go | 1 + 33 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cmd/bytemark/add.go b/cmd/bytemark/add.go index 92f6fa54..0fa8f64f 100644 --- a/cmd/bytemark/add.go +++ b/cmd/bytemark/add.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/brain" diff --git a/cmd/bytemark/app/with/with.go b/cmd/bytemark/app/with/with.go index 4d2ae534..dfb88a8f 100644 --- a/cmd/bytemark/app/with/with.go +++ b/cmd/bytemark/app/with/with.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/add/disc.go b/cmd/bytemark/commands/add/disc.go index 707e2498..e2ef62fc 100644 --- a/cmd/bytemark/commands/add/disc.go +++ b/cmd/bytemark/commands/add/disc.go @@ -3,6 +3,7 @@ package add import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" diff --git a/cmd/bytemark/commands/add/group.go b/cmd/bytemark/commands/add/group.go index 8bb09ee6..303f3915 100644 --- a/cmd/bytemark/commands/add/group.go +++ b/cmd/bytemark/commands/add/group.go @@ -3,6 +3,7 @@ package add import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" diff --git a/cmd/bytemark/commands/add/server.go b/cmd/bytemark/commands/add/server.go index 6abe3644..7c562953 100644 --- a/cmd/bytemark/commands/add/server.go +++ b/cmd/bytemark/commands/add/server.go @@ -6,6 +6,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/cliutil" diff --git a/cmd/bytemark/commands/admin/add/vlan_group.go b/cmd/bytemark/commands/admin/add/vlan_group.go index 3fca1f09..f682998a 100644 --- a/cmd/bytemark/commands/admin/add/vlan_group.go +++ b/cmd/bytemark/commands/admin/add/vlan_group.go @@ -3,6 +3,7 @@ package add import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/admin/migrate/server.go b/cmd/bytemark/commands/admin/migrate/server.go index bb017fd0..a0b77705 100644 --- a/cmd/bytemark/commands/admin/migrate/server.go +++ b/cmd/bytemark/commands/admin/migrate/server.go @@ -3,6 +3,7 @@ package migrate import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/admin/set.go b/cmd/bytemark/commands/admin/set.go index c95e8000..2a2dfe15 100644 --- a/cmd/bytemark/commands/admin/set.go +++ b/cmd/bytemark/commands/admin/set.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/admin/update.go b/cmd/bytemark/commands/admin/update.go index 34f641c9..e65190a2 100644 --- a/cmd/bytemark/commands/admin/update.go +++ b/cmd/bytemark/commands/admin/update.go @@ -6,6 +6,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib" "github.com/BytemarkHosting/bytemark-client/lib/billing" diff --git a/cmd/bytemark/commands/assent.go b/cmd/bytemark/commands/assent.go index ffb3fa35..238d13dd 100644 --- a/cmd/bytemark/commands/assent.go +++ b/cmd/bytemark/commands/assent.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/billing" diff --git a/cmd/bytemark/commands/backup.go b/cmd/bytemark/commands/backup.go index 27af8396..a18b41fa 100644 --- a/cmd/bytemark/commands/backup.go +++ b/cmd/bytemark/commands/backup.go @@ -3,6 +3,7 @@ package commands import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/delete/group.go b/cmd/bytemark/commands/delete/group.go index e8e1db5f..3a59d99d 100644 --- a/cmd/bytemark/commands/delete/group.go +++ b/cmd/bytemark/commands/delete/group.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" diff --git a/cmd/bytemark/commands/grant.go b/cmd/bytemark/commands/grant.go index 04d66bfc..f97519af 100644 --- a/cmd/bytemark/commands/grant.go +++ b/cmd/bytemark/commands/grant.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/reset_server.go b/cmd/bytemark/commands/reset_server.go index bbfdf2b8..a26bcd11 100644 --- a/cmd/bytemark/commands/reset_server.go +++ b/cmd/bytemark/commands/reset_server.go @@ -3,6 +3,7 @@ package commands import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/restart_server.go b/cmd/bytemark/commands/restart_server.go index f4215035..bfabfa11 100644 --- a/cmd/bytemark/commands/restart_server.go +++ b/cmd/bytemark/commands/restart_server.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/wait" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" diff --git a/cmd/bytemark/commands/revoke.go b/cmd/bytemark/commands/revoke.go index 335f9b96..af01e5eb 100644 --- a/cmd/bytemark/commands/revoke.go +++ b/cmd/bytemark/commands/revoke.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/BytemarkHosting/bytemark-client/util/log" diff --git a/cmd/bytemark/commands/show/backups.go b/cmd/bytemark/commands/show/backups.go index 12b2a47f..9654e69c 100644 --- a/cmd/bytemark/commands/show/backups.go +++ b/cmd/bytemark/commands/show/backups.go @@ -3,6 +3,7 @@ package show import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/BytemarkHosting/bytemark-client/lib/output" diff --git a/cmd/bytemark/commands/show/discs.go b/cmd/bytemark/commands/show/discs.go index 0553f756..fdbbe4de 100644 --- a/cmd/bytemark/commands/show/discs.go +++ b/cmd/bytemark/commands/show/discs.go @@ -3,6 +3,7 @@ package show import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib/output" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/show/groups.go b/cmd/bytemark/commands/show/groups.go index 6d26b348..c82f4fae 100644 --- a/cmd/bytemark/commands/show/groups.go +++ b/cmd/bytemark/commands/show/groups.go @@ -3,6 +3,7 @@ package show import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib/output" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/show/servers.go b/cmd/bytemark/commands/show/servers.go index c164b268..a0ce93f7 100644 --- a/cmd/bytemark/commands/show/servers.go +++ b/cmd/bytemark/commands/show/servers.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/BytemarkHosting/bytemark-client/lib/output" diff --git a/cmd/bytemark/commands/shutdown_server.go b/cmd/bytemark/commands/shutdown_server.go index 0fbee23b..7217817e 100644 --- a/cmd/bytemark/commands/shutdown_server.go +++ b/cmd/bytemark/commands/shutdown_server.go @@ -3,6 +3,7 @@ package commands import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/wait" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" diff --git a/cmd/bytemark/commands/start_server.go b/cmd/bytemark/commands/start_server.go index 72958f9d..7f1f7d5d 100644 --- a/cmd/bytemark/commands/start_server.go +++ b/cmd/bytemark/commands/start_server.go @@ -3,6 +3,7 @@ package commands import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/stop_server.go b/cmd/bytemark/commands/stop_server.go index d7a75b8e..100af3e5 100644 --- a/cmd/bytemark/commands/stop_server.go +++ b/cmd/bytemark/commands/stop_server.go @@ -3,6 +3,7 @@ package commands import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/urfave/cli" ) diff --git a/cmd/bytemark/commands/update/disc.go b/cmd/bytemark/commands/update/disc.go index 9bbb325d..c766d328 100644 --- a/cmd/bytemark/commands/update/disc.go +++ b/cmd/bytemark/commands/update/disc.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" diff --git a/cmd/bytemark/commands/update/server.go b/cmd/bytemark/commands/update/server.go index 6e81f16a..9309f749 100644 --- a/cmd/bytemark/commands/update/server.go +++ b/cmd/bytemark/commands/update/server.go @@ -6,6 +6,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" diff --git a/cmd/bytemark/console.go b/cmd/bytemark/console.go index 191d9e45..ee0ed42c 100644 --- a/cmd/bytemark/console.go +++ b/cmd/bytemark/console.go @@ -9,6 +9,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/brain" diff --git a/cmd/bytemark/console_test.go b/cmd/bytemark/console_test.go index ecd5b08e..b754a04f 100644 --- a/cmd/bytemark/console_test.go +++ b/cmd/bytemark/console_test.go @@ -9,8 +9,8 @@ import ( func TestCollectArgs(t *testing.T) { is := is.New(t) tests := map[string][]string{ - "": {}, - "-tt": {"-tt"}, + "": {}, + "-tt": {"-tt"}, "-i 'muh_identities.rsa'": {"-i", "muh_identities.rsa"}, "-i 'a long path/id_rsa'": {"-i", "a long path/id_rsa"}, "-i \"a long path/id_rsa\"": {"-i", "a long path/id_rsa"}, diff --git a/cmd/bytemark/delete.go b/cmd/bytemark/delete.go index c642167d..f9f26f53 100644 --- a/cmd/bytemark/delete.go +++ b/cmd/bytemark/delete.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" diff --git a/cmd/bytemark/reimage.go b/cmd/bytemark/reimage.go index 89191a7e..d5ddd55f 100644 --- a/cmd/bytemark/reimage.go +++ b/cmd/bytemark/reimage.go @@ -6,6 +6,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/cliutil" diff --git a/cmd/bytemark/restore.go b/cmd/bytemark/restore.go index ae9318db..d6c568d6 100644 --- a/cmd/bytemark/restore.go +++ b/cmd/bytemark/restore.go @@ -3,6 +3,7 @@ package main import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" diff --git a/cmd/bytemark/schedule.go b/cmd/bytemark/schedule.go index 5d968f94..44e6efd3 100644 --- a/cmd/bytemark/schedule.go +++ b/cmd/bytemark/schedule.go @@ -3,6 +3,7 @@ package main import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index f4ecc42c..4555b70d 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -3,6 +3,7 @@ package main import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/lib" diff --git a/cmd/bytemark/unschedule.go b/cmd/bytemark/unschedule.go index 4acbe94a..8c425fb7 100644 --- a/cmd/bytemark/unschedule.go +++ b/cmd/bytemark/unschedule.go @@ -5,6 +5,7 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" From 0d904567c592f5d1aa74cf4c7b62c98cc0305461 Mon Sep 17 00:00:00 2001 From: telyn Date: Wed, 5 Dec 2018 15:39:32 +0000 Subject: [PATCH 04/23] update HACKING --- HACKING | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/HACKING b/HACKING index 3c3a6940..e47dd94a 100644 --- a/HACKING +++ b/HACKING @@ -89,8 +89,11 @@ Here's a tree of the folders in this repo with descriptions of what each is for │   │ │ arguments into flags │   │   ├── auth - Authentication routine for the client, called via │   │ │ with.EnsureAuth -│   │   ├── flagsets - common flags & code to support reading complex objects -│   │ │ from them. e.g. VirtualMachineSpec & ImageInstall +│   │   ├── flags - new flag types that are used in the client (e.g. +│   │ │ VirtualMachineName, SizeSpec, ResizeSpec, AccountName, +│   │ │ Privilege) +│   │   ├── flagsets - common sets of flags & code to support reading complex +│   │ │ objects from them. e.g. VirtualMachineSpec & ImageInstall │   │ ├── wait - functions which wait for a condition to be true before │   │ │ returning │   │   └── with - functions for chaining together using app.Action to @@ -225,4 +228,4 @@ To generate the hash, use the following command. find . -type d \! -path './.*' \! -path './vendor/*' | sort | shasum -a 256 -# sha256sum: 86aedb8be233ceb404fcca0d1cf3ffa41099983441936e9c0622ae4f02727f6e +# sha256sum: 2a2e8078c018b791fb8f4749ae2a3afec81a6eacec6caf36cfaa094ede7d82c5 From 5f6af0a34c75090f5b05654da8f2bf8e66eda1b6 Mon Sep 17 00:00:00 2001 From: telyn Date: Wed, 5 Dec 2018 18:10:07 +0000 Subject: [PATCH 05/23] Huge giant refactor to combine all the flags into the flags package. There's some breakage (looks like some flags aren't getting reset properly) but otherwise it's going good --- cmd/bytemark/add.go | 11 +- cmd/bytemark/add_test.go | 20 ++ cmd/bytemark/app/context.go | 93 +-------- cmd/bytemark/app/flags/accessors.go | 106 ++++++++++ cmd/bytemark/app/flags/account_name.go | 17 +- .../{util => app/flags}/date_time_flag.go | 2 +- .../flags}/date_time_flag_test.go | 5 +- cmd/bytemark/app/flags/disc_spec.go | 38 ++++ cmd/bytemark/{util => app/flags}/fileflag.go | 2 +- .../{util => app/flags}/fileflag_test.go | 5 +- cmd/bytemark/app/flags/group_name.go | 15 +- cmd/bytemark/app/flags/ip.go | 26 +++ .../flags_test.go => app/flags/ip_test.go} | 5 +- cmd/bytemark/app/flags/preprocessor.go | 4 +- cmd/bytemark/app/flags/privilege.go | 13 +- cmd/bytemark/app/flags/resize.go | 8 +- cmd/bytemark/app/flags/sizespec.go | 28 +++ .../app/flags/virtual_machine_name.go | 15 +- cmd/bytemark/app/flagsets/create_server.go | 10 +- cmd/bytemark/app/flagsets/image_install.go | 9 +- cmd/bytemark/app/with.go | 53 ++--- cmd/bytemark/app/with/privilege.go | 5 +- cmd/bytemark/app/with/with.go | 15 +- cmd/bytemark/commands/add/disc.go | 9 +- cmd/bytemark/commands/add/group.go | 4 +- cmd/bytemark/commands/add/server.go | 8 +- cmd/bytemark/commands/admin/add/vlan_group.go | 4 +- cmd/bytemark/commands/admin/add/vm_default.go | 5 +- cmd/bytemark/commands/admin/migrate/server.go | 4 +- cmd/bytemark/commands/admin/set.go | 4 +- cmd/bytemark/commands/admin/show.go | 6 +- cmd/bytemark/commands/admin/update.go | 4 +- cmd/bytemark/commands/assent.go | 4 +- cmd/bytemark/commands/backup.go | 4 +- cmd/bytemark/{ => commands}/console.go | 8 +- cmd/bytemark/{ => commands}/console_test.go | 2 +- cmd/bytemark/commands/delete/backup.go | 41 ++++ cmd/bytemark/commands/delete/disc.go | 56 ++++++ cmd/bytemark/commands/delete/group.go | 4 +- cmd/bytemark/commands/delete/key.go | 45 +++++ cmd/bytemark/commands/delete/server.go | 66 +++++++ cmd/bytemark/commands/grant.go | 4 +- cmd/bytemark/commands/reset_server.go | 20 +- cmd/bytemark/commands/restart_server.go | 4 +- cmd/bytemark/commands/revoke.go | 6 +- cmd/bytemark/commands/show/backups.go | 4 +- cmd/bytemark/commands/show/discs.go | 2 +- cmd/bytemark/commands/show/groups.go | 2 +- cmd/bytemark/commands/show/servers.go | 4 +- cmd/bytemark/commands/shutdown_server.go | 4 +- cmd/bytemark/commands/start_server.go | 4 +- cmd/bytemark/commands/stop_server.go | 4 +- cmd/bytemark/commands/update/disc.go | 16 +- cmd/bytemark/commands/update/server.go | 22 +-- cmd/bytemark/delete.go | 181 ------------------ cmd/bytemark/delete_test.go | 14 +- cmd/bytemark/reimage.go | 4 +- cmd/bytemark/restore.go | 8 +- cmd/bytemark/schedule.go | 4 +- cmd/bytemark/show.go | 16 +- cmd/bytemark/unschedule.go | 4 +- cmd/bytemark/util/flags.go | 80 -------- 62 files changed, 630 insertions(+), 560 deletions(-) create mode 100644 cmd/bytemark/app/flags/accessors.go rename cmd/bytemark/{util => app/flags}/date_time_flag.go (97%) rename cmd/bytemark/{util => app/flags}/date_time_flag_test.go (87%) create mode 100644 cmd/bytemark/app/flags/disc_spec.go rename cmd/bytemark/{util => app/flags}/fileflag.go (98%) rename cmd/bytemark/{util => app/flags}/fileflag_test.go (77%) create mode 100644 cmd/bytemark/app/flags/ip.go rename cmd/bytemark/{util/flags_test.go => app/flags/ip_test.go} (77%) create mode 100644 cmd/bytemark/app/flags/sizespec.go rename cmd/bytemark/{ => commands}/console.go (97%) rename cmd/bytemark/{ => commands}/console_test.go (97%) create mode 100644 cmd/bytemark/commands/delete/backup.go create mode 100644 cmd/bytemark/commands/delete/disc.go create mode 100644 cmd/bytemark/commands/delete/key.go create mode 100644 cmd/bytemark/commands/delete/server.go delete mode 100644 cmd/bytemark/delete.go delete mode 100644 cmd/bytemark/util/flags.go diff --git a/cmd/bytemark/add.go b/cmd/bytemark/add.go index 0fa8f64f..9077648a 100644 --- a/cmd/bytemark/add.go +++ b/cmd/bytemark/add.go @@ -7,7 +7,6 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/brain" brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" "github.com/BytemarkHosting/bytemark-client/util/log" @@ -15,7 +14,7 @@ import ( ) func init() { - publicKeyFile := util.FileFlag{} + publicKeyFile := flags.FileFlag{} commands = append(commands, cli.Command{ Name: "add", Usage: "add SSH keys to a user / IPs to a server", @@ -39,7 +38,7 @@ func init() { cli.GenericFlag{ Name: "public-key-file", Usage: "The public key file to add to the account", - Value: &publicKeyFile, + Value: &flags.FileFlag{}, }, }, Action: app.Action(args.Join("public-key"), with.Auth, func(ctx *app.Context) (err error) { @@ -57,7 +56,7 @@ func init() { } else { // if public-key is not blank, try to use it as a filename // FileFlag does some nice ~-substitution which is why we use it rather than the infinitely more normal-looking ioutil.ReadFile - publicKeyFile = util.FileFlag{FileName: key} + publicKeyFile = flags.FileFlag{FileName: key} if err := publicKeyFile.Set(key); err == nil { key = publicKeyFile.Value } @@ -100,7 +99,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "The server to add IPs to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) error { @@ -129,7 +128,7 @@ func init() { Reason: reason, Contiguous: c.Bool("contiguous"), } - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") ips, err := c.Client().AddIP(vmName, ipcr) if err != nil { return err diff --git a/cmd/bytemark/add_test.go b/cmd/bytemark/add_test.go index 5b6d6926..c6c50d58 100644 --- a/cmd/bytemark/add_test.go +++ b/cmd/bytemark/add_test.go @@ -25,6 +25,11 @@ func TestAddKeyCommand(t *testing.T) { } t.Run("Key in command line", func(t *testing.T) { + defer func() { + if err := recover(); err != nil { + t.Error(err) + } + }() _, c, app := testutil.BaseTestAuthSetup(t, false, commands) c.When("GetUser", "test-user").Return(brain.User{Username: "test-user"}).Times(1) c.MockRequest = &mocks.Request{ @@ -46,6 +51,11 @@ func TestAddKeyCommand(t *testing.T) { }) t.Run("Key in file", func(t *testing.T) { + defer func() { + if err := recover(); err != nil { + t.Error(err) + } + }() _, c, app := testutil.BaseTestAuthSetup(t, false, commands) c.When("GetUser", "test-user").Return(brain.User{Username: "test-user"}).Times(1) c.MockRequest = &mocks.Request{ @@ -68,6 +78,11 @@ func TestAddKeyCommand(t *testing.T) { }) t.Run("Key in file using flag", func(t *testing.T) { + defer func() { + if err := recover(); err != nil { + t.Error(err) + } + }() _, c, app := testutil.BaseTestAuthSetup(t, false, commands) c.When("GetUser", "test-user").Return(brain.User{Username: "test-user"}).Times(1) c.MockRequest = &mocks.Request{ @@ -89,6 +104,11 @@ func TestAddKeyCommand(t *testing.T) { }) t.Run("dont allow private key", func(t *testing.T) { + defer func() { + if err := recover(); err != nil { + t.Error(err) + } + }() _, c, app := testutil.BaseTestAuthSetup(t, false, commands) err = app.Run([]string{"bytemark", "add", "key", "--user", "test-user", "--public-key-file", "testkey"}) if err == nil { diff --git a/cmd/bytemark/app/context.go b/cmd/bytemark/app/context.go index d161c518..41152618 100644 --- a/cmd/bytemark/app/context.go +++ b/cmd/bytemark/app/context.go @@ -2,7 +2,6 @@ package app import ( "io" - "net" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/config" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" @@ -120,53 +119,14 @@ func (c *Context) IsSet(flagName string) bool { return c.Context.IsSet(flagName) } -// flags below +// Flags provided by urfave/cli below. +// For all the flags in the flags package, see flags/accessors.go // Bool returns the value of the named flag as a bool func (c *Context) Bool(flagname string) bool { return c.Context.Bool(flagname) } -// Discs returns the discs passed along as the named flag. -// I can't imagine why I'd ever name a disc flag anything other than --disc, but the flexibility is there just in case. -func (c *Context) Discs(flagname string) []brain.Disc { - disc, ok := c.Context.Generic(flagname).(*util.DiscSpecFlag) - if ok { - return []brain.Disc(*disc) - } - return []brain.Disc{} -} - -// FileName returns the name of the file given by the named flag -func (c *Context) FileName(flagname string) string { - file, ok := c.Context.Generic(flagname).(*util.FileFlag) - if ok { - return file.FileName - } - return "" -} - -// FileContents returns the contents of the file given by the named flag. -func (c *Context) FileContents(flagname string) string { - file, ok := c.Context.Generic(flagname).(*util.FileFlag) - if ok { - return file.Value - } - return "" -} - -// GroupName returns the named flag as a lib.GroupName -func (c *Context) GroupName(flagname string) (gp lib.GroupName) { - gpNameFlag, ok := c.Context.Generic(flagname).(*GroupNameFlag) - if !ok { - return lib.GroupName{} - } - if gpNameFlag.GroupName == nil { - return lib.GroupName{} - } - return *gpNameFlag.GroupName -} - // Int returns the value of the named flag as an int func (c *Context) Int(flagname string) int { return c.Context.Int(flagname) @@ -177,24 +137,6 @@ func (c *Context) Int64(flagname string) int64 { return c.Context.Int64(flagname) } -// IPs returns the ips passed along as the named flag. -func (c *Context) IPs(flagname string) []net.IP { - ips, ok := c.Context.Generic(flagname).(*util.IPFlag) - if ok { - return []net.IP(*ips) - } - return []net.IP{} -} - -// PrivilegeFlag returns the named flag as a PrivilegeFlag -func (c *Context) PrivilegeFlag(flagname string) PrivilegeFlag { - priv, ok := c.Context.Generic(flagname).(*PrivilegeFlag) - if ok { - return *priv - } - return PrivilegeFlag{} -} - // String returns the value of the named flag as a string func (c *Context) String(flagname string) string { if c.Context.IsSet(flagname) || c.Context.String(flagname) != "" { @@ -211,34 +153,3 @@ func (c *Context) StringSlice(flagname string) []string { } return c.Context.GlobalStringSlice(flagname) } - -// Size returns the value of the named SizeSpecFlag as an int in megabytes -func (c *Context) Size(flagname string) int { - size, ok := c.Context.Generic(flagname).(*util.SizeSpecFlag) - if ok { - return int(*size) - } - return 0 -} - -// ResizeFlag returns the named ResizeFlag -func (c *Context) ResizeFlag(flagname string) ResizeFlag { - size, ok := c.Context.Generic(flagname).(*ResizeFlag) - if ok { - return *size - } - return ResizeFlag{} -} - -// VirtualMachineName returns the named flag as a lib.VirtualMachineName -func (c *Context) VirtualMachineName(flagname string) (vm lib.VirtualMachineName) { - vmNameFlag, ok := c.Context.Generic(flagname).(*VirtualMachineNameFlag) - if !ok { - return c.Config().GetVirtualMachine() - } - if vmNameFlag.VirtualMachineName == nil { - return lib.VirtualMachineName{} - } - - return *vmNameFlag.VirtualMachineName -} diff --git a/cmd/bytemark/app/flags/accessors.go b/cmd/bytemark/app/flags/accessors.go new file mode 100644 index 00000000..97447c36 --- /dev/null +++ b/cmd/bytemark/app/flags/accessors.go @@ -0,0 +1,106 @@ +package flags + +import ( + "net" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/lib" + "github.com/BytemarkHosting/bytemark-client/lib/brain" +) + +func AccountName(c *app.Context, flagname string) AccountNameFlag { + accountName, ok := c.Context.Generic(flagname).(*AccountNameFlag) + if ok { + return AccountNameFlag(*accountName) + } + return AccountNameFlag{} +} + +// Discs returns the discs passed along as the named flag. +// I can't imagine why I'd ever name a disc flag anything other than --disc, but the flexibility is there just in case. +func Discs(c *app.Context, flagname string) []brain.Disc { + disc, ok := c.Context.Generic(flagname).(*DiscSpecFlag) + if ok { + return []brain.Disc(*disc) + } + return []brain.Disc{} +} + +// FileName returns the name of the file given by the named flag +func FileName(c *app.Context, flagname string) string { + file, ok := c.Context.Generic(flagname).(*FileFlag) + if ok { + return file.FileName + } + return "" +} + +// FileContents returns the contents of the file given by the named flag. +func FileContents(c *app.Context, flagname string) string { + file, ok := c.Context.Generic(flagname).(*FileFlag) + if ok { + return file.Value + } + return "" +} + +// GroupName returns the named flag as a lib.GroupName +func GroupName(c *app.Context, flagname string) (gp lib.GroupName) { + gpNameFlag, ok := c.Context.Generic(flagname).(*GroupNameFlag) + if !ok { + return lib.GroupName{} + } + if gpNameFlag.GroupName == nil { + return lib.GroupName{} + } + return *gpNameFlag.GroupName +} + +// IPs returns the ips passed along as the named flag. +func IPs(c *app.Context, flagname string) []net.IP { + ips, ok := c.Context.Generic(flagname).(*IPFlag) + if ok { + return []net.IP(*ips) + } + return []net.IP{} +} + +// Privilege returns the named flag as a PrivilegeFlag +func Privilege(c *app.Context, flagname string) PrivilegeFlag { + priv, ok := c.Context.Generic(flagname).(*PrivilegeFlag) + if ok { + return *priv + } + return PrivilegeFlag{} +} + +// Resize returns the named ResizeFlag +func Resize(c *app.Context, flagname string) ResizeFlag { + size, ok := c.Context.Generic(flagname).(*ResizeFlag) + if ok { + return *size + } + return ResizeFlag{} +} + +// Size returns the value of the named SizeSpecFlag as an int in megabytes +func Size(c *app.Context, flagname string) int { + size, ok := c.Context.Generic(flagname).(*SizeSpecFlag) + if ok { + return int(*size) + } + return 0 +} + +// VirtualMachineName returns the named flag as a lib.VirtualMachineName +func VirtualMachineName(c *app.Context, flagname string) (vm lib.VirtualMachineName) { + vmNameFlag, ok := c.Context.Generic(flagname).(*VirtualMachineNameFlag) + if !ok { + return c.Config().GetVirtualMachine() + } + if vmNameFlag.VirtualMachineName == nil { + return lib.VirtualMachineName{} + } + + return *vmNameFlag.VirtualMachineName +} diff --git a/cmd/bytemark/app/flags/account_name.go b/cmd/bytemark/app/flags/account_name.go index 7b0bb19f..5f468af1 100644 --- a/cmd/bytemark/app/flags/account_name.go +++ b/cmd/bytemark/app/flags/account_name.go @@ -1,16 +1,19 @@ package flags -import "github.com/BytemarkHosting/bytemark-client/lib" +import ( + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/lib" +) -// AccountName is used for all --account flags, excluding the global one. -type AccountName struct { +// AccountNameFlag is used for all --account flags, excluding the global one. +type AccountNameFlag struct { AccountName string Value string SetFromCommandLine bool } // Set runs lib.ParseAccountName to make sure we get just the 'pure' account name; no cluster / endpoint details -func (name *AccountName) Set(value string) error { +func (name *AccountNameFlag) Set(value string) error { name.Value = value name.SetFromCommandLine = true return nil @@ -18,7 +21,7 @@ func (name *AccountName) Set(value string) error { // Preprocess sets the value of this flag to the global account flag if it's unset, // and then runs lib.ParseAccountName -func (name *AccountName) Preprocess(c *Context) (err error) { +func (name *AccountNameFlag) Preprocess(c *app.Context) (err error) { if name.Value == "" { name.Value = c.Context.GlobalString("account") } @@ -26,8 +29,8 @@ func (name *AccountName) Preprocess(c *Context) (err error) { return } -// String returns the AccountName as a string. -func (name AccountName) String() string { +// String returns the AccountNameFlag as a string. +func (name AccountNameFlag) String() string { if name.AccountName == "" { return name.Value } diff --git a/cmd/bytemark/util/date_time_flag.go b/cmd/bytemark/app/flags/date_time_flag.go similarity index 97% rename from cmd/bytemark/util/date_time_flag.go rename to cmd/bytemark/app/flags/date_time_flag.go index 95a27993..85898134 100644 --- a/cmd/bytemark/util/date_time_flag.go +++ b/cmd/bytemark/app/flags/date_time_flag.go @@ -1,4 +1,4 @@ -package util +package flags import ( "github.com/bcampbell/fuzzytime" diff --git a/cmd/bytemark/util/date_time_flag_test.go b/cmd/bytemark/app/flags/date_time_flag_test.go similarity index 87% rename from cmd/bytemark/util/date_time_flag_test.go rename to cmd/bytemark/app/flags/date_time_flag_test.go index de6efc25..af933190 100644 --- a/cmd/bytemark/util/date_time_flag_test.go +++ b/cmd/bytemark/app/flags/date_time_flag_test.go @@ -1,9 +1,10 @@ -package util +package flags_test import ( "testing" "time" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/cheekybits/is" ) @@ -41,7 +42,7 @@ func TestDateTimeFlag(t *testing.T) { for _, test := range tests { t.Run("datetime flag", func(t *testing.T) { - var flag DateTimeFlag + var flag flags.DateTimeFlag err := flag.Set(test.input) diff --git a/cmd/bytemark/app/flags/disc_spec.go b/cmd/bytemark/app/flags/disc_spec.go new file mode 100644 index 00000000..e6e6b0d3 --- /dev/null +++ b/cmd/bytemark/app/flags/disc_spec.go @@ -0,0 +1,38 @@ +package flags + +import ( + "fmt" + "strings" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" + "github.com/BytemarkHosting/bytemark-client/lib/brain" +) + +// DiscSpecFlag is a flag which reads its argument as a disc spec. It can be specified multiple times to add multiple discs. +type DiscSpecFlag []brain.Disc + +// Set adds all the defined discs to this flag's value +func (discsFlag *DiscSpecFlag) Set(value string) error { + for _, val := range strings.Split(value, " ") { + disc, err := util.ParseDiscSpec(val) + if err != nil { + return err + } + + *discsFlag = append(*discsFlag, *disc) + + } + return nil +} + +func (discsFlag *DiscSpecFlag) String() string { + var discs []string + for _, d := range *discsFlag { + if d.Label == "" { + discs = append(discs, fmt.Sprintf("%s:%dGiB", d.StorageGrade, d.Size/1024)) + } else { + discs = append(discs, fmt.Sprintf("%s:%s:%dGiB", d.Label, d.StorageGrade, d.Size/1024)) + } + } + return strings.Join(discs, ",") +} diff --git a/cmd/bytemark/util/fileflag.go b/cmd/bytemark/app/flags/fileflag.go similarity index 98% rename from cmd/bytemark/util/fileflag.go rename to cmd/bytemark/app/flags/fileflag.go index 01689ec1..298238af 100644 --- a/cmd/bytemark/util/fileflag.go +++ b/cmd/bytemark/app/flags/fileflag.go @@ -1,4 +1,4 @@ -package util +package flags import ( "io/ioutil" diff --git a/cmd/bytemark/util/fileflag_test.go b/cmd/bytemark/app/flags/fileflag_test.go similarity index 77% rename from cmd/bytemark/util/fileflag_test.go rename to cmd/bytemark/app/flags/fileflag_test.go index 5b5fd94f..9cfbd04b 100644 --- a/cmd/bytemark/util/fileflag_test.go +++ b/cmd/bytemark/app/flags/fileflag_test.go @@ -1,10 +1,11 @@ -package util +package flags_test import ( "io/ioutil" "os" "testing" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/cheekybits/is" ) @@ -15,7 +16,7 @@ func TestFileFlag(t *testing.T) { t.Fatal(err) } - flag := FileFlag{} + flag := flags.FileFlag{} err = flag.Set("test-fileflag") if err != nil { diff --git a/cmd/bytemark/app/flags/group_name.go b/cmd/bytemark/app/flags/group_name.go index 61b7ec4e..9f63d82c 100644 --- a/cmd/bytemark/app/flags/group_name.go +++ b/cmd/bytemark/app/flags/group_name.go @@ -1,22 +1,25 @@ package flags -import "github.com/BytemarkHosting/bytemark-client/lib" +import ( + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/lib" +) -// GroupName is used for all --group flags, including the global one. -type GroupName struct { +// GroupNameFlag is used for all --group flags, including the global one. +type GroupNameFlag struct { GroupName *lib.GroupName Value string } // Set runs lib.ParseGroupName to make sure we have a valid group name -func (name *GroupName) Set(value string) error { +func (name *GroupNameFlag) Set(value string) error { name.Value = value return nil } // Preprocess defaults the value of this flag to the default group from the // config attached to the context and then runs lib.ParseGroupName -func (name *GroupName) Preprocess(c *Context) (err error) { +func (name *GroupNameFlag) Preprocess(c *app.Context) (err error) { if name.GroupName != nil { c.Debug("GroupName.Preprocess before %#v", *name.GroupName) } @@ -30,7 +33,7 @@ func (name *GroupName) Preprocess(c *Context) (err error) { } // String returns the GroupName as a string. -func (name GroupName) String() string { +func (name GroupNameFlag) String() string { if name.GroupName != nil { return name.GroupName.String() } diff --git a/cmd/bytemark/app/flags/ip.go b/cmd/bytemark/app/flags/ip.go new file mode 100644 index 00000000..b638bae6 --- /dev/null +++ b/cmd/bytemark/app/flags/ip.go @@ -0,0 +1,26 @@ +package flags + +import ( + "net" + "strings" +) + +// IPFlag is a flag.Value used to provide an array of net.IPs +type IPFlag []net.IP + +// Set sets the IPFlag given the space-separated string of IPs +func (ips *IPFlag) Set(value string) error { + for _, val := range strings.Split(value, " ") { + ip := net.ParseIP(val) + *ips = append(*ips, ip) + } + return nil +} + +func (ips *IPFlag) String() string { + var val []string + for _, ip := range *ips { + val = append(val, ip.String()) + } + return strings.Join(val, ", ") +} diff --git a/cmd/bytemark/util/flags_test.go b/cmd/bytemark/app/flags/ip_test.go similarity index 77% rename from cmd/bytemark/util/flags_test.go rename to cmd/bytemark/app/flags/ip_test.go index ab176cc6..ddcfd777 100644 --- a/cmd/bytemark/util/flags_test.go +++ b/cmd/bytemark/app/flags/ip_test.go @@ -1,15 +1,16 @@ -package util +package flags_test import ( "testing" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/cheekybits/is" ) func TestIPFlag(t *testing.T) { is := is.New(t) - flag := IPFlag{} + flag := flags.IPFlag{} // IPFlag.Set never returns an error _ = flag.Set("192.168.1.1") _ = flag.Set("2000::1") diff --git a/cmd/bytemark/app/flags/preprocessor.go b/cmd/bytemark/app/flags/preprocessor.go index 30c15f4b..2e0e7acb 100644 --- a/cmd/bytemark/app/flags/preprocessor.go +++ b/cmd/bytemark/app/flags/preprocessor.go @@ -1,6 +1,8 @@ package flags +import "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + // A Preprocesser is a Flag that has a preprocess step that requires a Context type Preprocesser interface { - Preprocess(c *Context) error + Preprocess(c *app.Context) error } diff --git a/cmd/bytemark/app/flags/privilege.go b/cmd/bytemark/app/flags/privilege.go index a20781ff..0e1affca 100644 --- a/cmd/bytemark/app/flags/privilege.go +++ b/cmd/bytemark/app/flags/privilege.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/lib" "github.com/BytemarkHosting/bytemark-client/lib/brain" ) @@ -25,7 +26,7 @@ func (args *privArgs) shift() (arg string, err error) { } // Privilege is an un-realised brain.Privilege - where the target name has been parsed but hasn't been turned into IDs yet -type Privilege struct { +type PrivilegeFlag struct { AccountName string GroupName *lib.GroupName VirtualMachineName *lib.VirtualMachineName @@ -35,12 +36,12 @@ type Privilege struct { } // TargetType returns the prefix of the Privilege's Level. Use the brain.PrivilegeTargetType* constants for comparison -func (pf Privilege) TargetType() string { +func (pf PrivilegeFlag) TargetType() string { return strings.SplitN(string(pf.Level), "_", 2)[0] } // fillPrivilegeTarget adds the object to the privilege, trying to use it as a VM, Group or Account name depending on what PrivilegeLevel the Privilege is for. The target is expected to be the NextArg at this point in the Context -func (pf *Privilege) fillPrivilegeTarget(c *Context, args *privArgs) (err error) { +func (pf *PrivilegeFlag) fillPrivilegeTarget(c *app.Context, args *privArgs) (err error) { if pf.TargetType() != brain.PrivilegeTargetTypeCluster { var target string target, err = args.shift() @@ -70,14 +71,14 @@ func (pf *Privilege) fillPrivilegeTarget(c *Context, args *privArgs) (err error) } // Set sets the privilege given some string (should be in the form " [[on] ] [to|from|for] " -func (pf *Privilege) Set(value string) (err error) { +func (pf *PrivilegeFlag) Set(value string) (err error) { pf.Value = value return nil } // Preprocess parses the Privilege and looks up the target's ID so it can // be made into a brain.Privilege -func (pf *Privilege) Preprocess(c *Context) (err error) { +func (pf *PrivilegeFlag) Preprocess(c *app.Context) (err error) { args := privArgs(strings.Split(pf.Value, " ")) level, err := args.shift() @@ -107,7 +108,7 @@ func (pf *Privilege) Preprocess(c *Context) (err error) { return } -func (pf Privilege) String() string { +func (pf PrivilegeFlag) String() string { switch pf.TargetType() { case brain.PrivilegeTargetTypeVM: return fmt.Sprintf("%s on %s for %s", pf.Level, pf.VirtualMachineName, pf.Username) diff --git a/cmd/bytemark/app/flags/resize.go b/cmd/bytemark/app/flags/resize.go index d242e012..10bb4337 100644 --- a/cmd/bytemark/app/flags/resize.go +++ b/cmd/bytemark/app/flags/resize.go @@ -17,14 +17,14 @@ const ( ResizeModeIncrease ) -// Resize is effectively an extension of SizeSpecFlag which has a ResizeMode. The Size stored in the flag is the size to set to or increase by depending on the Mode -type Resize struct { +// ResizeFlag is effectively an extension of SizeSpecFlag which has a ResizeMode. The Size stored in the flag is the size to set to or increase by depending on the Mode +type ResizeFlag struct { Mode ResizeMode Size int } // Set parses the string into a Resize. If it starts with +, Mode is set to ResizeModeIncrease. Otherwise, it's set to ResizeModeSet. The rest of the string is parsed as a sizespec using sizespec.Parse -func (rf *Resize) Set(value string) (err error) { +func (rf *ResizeFlag) Set(value string) (err error) { rf.Mode = ResizeModeSet if strings.HasPrefix(value, "+") { rf.Mode = ResizeModeIncrease @@ -40,7 +40,7 @@ func (rf *Resize) Set(value string) (err error) { } // String returns the size, in GiB or TiB (if the size is > 1TIB) with the unit used as a suffix. If Mode is ResizeModeIncrease, the string is prefixed with '+' -func (rf Resize) String() string { +func (rf ResizeFlag) String() string { plus := "" if rf.Mode == ResizeModeIncrease { plus += "+" diff --git a/cmd/bytemark/app/flags/sizespec.go b/cmd/bytemark/app/flags/sizespec.go new file mode 100644 index 00000000..9911c6f4 --- /dev/null +++ b/cmd/bytemark/app/flags/sizespec.go @@ -0,0 +1,28 @@ +package flags + +import ( + "fmt" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util/sizespec" +) + +// SizeSpecFlag represents a capacity as an integer number of megabytes. +type SizeSpecFlag int + +// Set sets the value to the size specified. Users can add "M" or "G" as a suffix to specify that they are talking about megabytes/gigabytes. Gigabytes are assumed by default. +func (ssf *SizeSpecFlag) Set(spec string) error { + sz, err := sizespec.Parse(spec) + if err != nil { + return err + } + *ssf = SizeSpecFlag(sz) + return nil +} + +func (ssf *SizeSpecFlag) String() string { + // default value is 0, but is checked for in code that uses SizeSpecFlag and changed to 1 + if *ssf == 0 { + return "" + } + return fmt.Sprintf("%d", *ssf) +} diff --git a/cmd/bytemark/app/flags/virtual_machine_name.go b/cmd/bytemark/app/flags/virtual_machine_name.go index f169921e..a2d07d28 100644 --- a/cmd/bytemark/app/flags/virtual_machine_name.go +++ b/cmd/bytemark/app/flags/virtual_machine_name.go @@ -1,22 +1,25 @@ package flags -import "github.com/BytemarkHosting/bytemark-client/lib" +import ( + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/lib" +) -// VirtualMachineName is used for all --server flags, or should be at least. -type VirtualMachineName struct { +// VirtualMachineNameFlag is used for all --server flags, or should be at least. +type VirtualMachineNameFlag struct { VirtualMachineName *lib.VirtualMachineName Value string } // Set runs lib.ParseVirtualMachineName using the c.Client() to make sure we have a valid group name -func (name *VirtualMachineName) Set(value string) error { +func (name *VirtualMachineNameFlag) Set(value string) error { name.Value = value return nil } // Preprocess defaults the value of this flag to the default server from the // config attached to the context and then runs lib.ParseVirtualMachineName -func (name *VirtualMachineName) Preprocess(c *Context) (err error) { +func (name *VirtualMachineNameFlag) Preprocess(c *app.Context) (err error) { if name.Value == "" { return } @@ -26,7 +29,7 @@ func (name *VirtualMachineName) Preprocess(c *Context) (err error) { } // String returns the VirtualMachineName as a string. -func (name VirtualMachineName) String() string { +func (name VirtualMachineNameFlag) String() string { if name.VirtualMachineName != nil { return name.VirtualMachineName.String() } diff --git a/cmd/bytemark/app/flagsets/create_server.go b/cmd/bytemark/app/flagsets/create_server.go index eab70975..dc56a355 100644 --- a/cmd/bytemark/app/flagsets/create_server.go +++ b/cmd/bytemark/app/flagsets/create_server.go @@ -5,7 +5,7 @@ import ( "time" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/urfave/cli" ) @@ -25,7 +25,7 @@ var ServerSpecFlags = []cli.Flag{ cli.GenericFlag{ Name: "disc", Usage: "One or more disc specifications. Defaults to a single 25GiB sata-grade disc", - Value: new(util.DiscSpecFlag), + Value: new(flags.DiscSpecFlag), }, Force, cli.StringFlag{ @@ -38,7 +38,7 @@ var ServerSpecFlags = []cli.Flag{ }, cli.GenericFlag{ Name: "memory", - Value: new(util.SizeSpecFlag), + Value: new(flags.SizeSpecFlag), Usage: "How much memory the server will have available, specified in GiB or with GiB/MiB units. Defaults to 1GiB.", }, cli.BoolFlag{ @@ -137,9 +137,9 @@ func prepareDiscs(backupFrequency string, discs []brain.Disc) ([]brain.Disc, err // prepareServerReadArgs sets up the initial defaults, reads in the --disc, --cores and --memory flags func prepareServerReadArgs(c *app.Context) (discs []brain.Disc, cores, memory int, err error) { - discs = c.Discs("disc") + discs = flags.Discs(c, "disc") cores = c.Int("cores") - memory = c.Size("memory") + memory = flags.Size(c, "memory") if memory == 0 { memory = 1024 } diff --git a/cmd/bytemark/app/flagsets/image_install.go b/cmd/bytemark/app/flagsets/image_install.go index 0bb91270..8ce09e3f 100644 --- a/cmd/bytemark/app/flagsets/image_install.go +++ b/cmd/bytemark/app/flagsets/image_install.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/urfave/cli" @@ -19,7 +20,7 @@ var ImageInstallFlags = []cli.Flag{ cli.GenericFlag{ Name: "firstboot-script-file", Usage: "Local file to read the firstboot script from.", - Value: new(util.FileFlag), + Value: new(flags.FileFlag), }, cli.StringFlag{ Name: "image", @@ -37,7 +38,7 @@ var ImageInstallAuthFlags = []cli.Flag{ cli.GenericFlag{ Name: "authorized-keys-file", Usage: "Local file to read the --authorized-keys from", - Value: new(util.FileFlag), + Value: new(flags.FileFlag), }, cli.StringFlag{ Name: "root-password", @@ -47,7 +48,7 @@ var ImageInstallAuthFlags = []cli.Flag{ func readAuthentication(c *app.Context) (pubkeys, rootPassword string) { pubkeys = c.String("authorized-keys") - pubkeysFile := c.FileContents("authorized-keys-file") + pubkeysFile := flags.FileContents(c, "authorized-keys-file") rootPassword = c.String("root-password") if pubkeysFile != "" { if pubkeys != "" { @@ -67,7 +68,7 @@ func readAuthentication(c *app.Context) (pubkeys, rootPassword string) { func PrepareImageInstall(c *app.Context, authentication bool) (imageInstall brain.ImageInstall, defaulted bool, err error) { image := c.String("image") firstbootScript := c.String("firstboot-script") - firstbootScriptFile := c.FileContents("firstboot-script-file") + firstbootScriptFile := flags.FileContents(c, "firstboot-script-file") pubkeys := "" rootPassword := "" diff --git a/cmd/bytemark/app/with.go b/cmd/bytemark/app/with.go index 91dc6c39..8f9673bb 100644 --- a/cmd/bytemark/app/with.go +++ b/cmd/bytemark/app/with.go @@ -1,16 +1,19 @@ package app import ( - "net" + "reflect" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" - "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/urfave/cli" ) // ProviderFunc is the function type that can be passed to Action() type ProviderFunc func(*Context) error +// A Preprocesser is a flag.Flag that has a preprocess step that requires a Context +type Preprocesser interface { + Preprocess(c *Context) error +} + // Action is a convenience function for making cli.Command.Actions that sets up a Context, runs all the providers, cleans up afterward and returns errors from the actions if there is one func Action(providers ...ProviderFunc) func(c *cli.Context) error { providers = append(providers, providers[len(providers)-1]) @@ -50,42 +53,16 @@ func (c *Context) Preprocess() error { // This is needed because the init() functions are only executed once during the testing cycle. // Outside of the tests, global.App.Run is only called once before the program closes. func cleanup(c *Context) { - ips, ok := c.Context.Generic("ip").(*util.IPFlag) - if ok { - *ips = make([]net.IP, 0) - } - disc, ok := c.Context.Generic("disc").(*util.DiscSpecFlag) - if ok { - *disc = make([]brain.Disc, 0) - } - size, ok := c.Context.Generic("memory").(*util.SizeSpecFlag) - if ok { - *size = 0 - } - server, ok := c.Context.Generic("server").(*VirtualMachineNameFlag) - if ok { - *server = VirtualMachineNameFlag{} - } - server, ok = c.Context.Generic("new-name").(*VirtualMachineNameFlag) - if ok { - *server = VirtualMachineNameFlag{} - } - server, ok = c.Context.Generic("from").(*VirtualMachineNameFlag) - if ok { - *server = VirtualMachineNameFlag{} - } - server, ok = c.Context.Generic("to").(*VirtualMachineNameFlag) - if ok { - *server = VirtualMachineNameFlag{} - } - group, ok := c.Context.Generic("group").(*GroupNameFlag) - if ok { - *group = GroupNameFlag{} - } + allFlags := append(c.Command().Flags, c.App().Flags...) + for _, flag := range allFlags { + if genericFlag, ok := flag.(cli.GenericFlag); ok { + flagValue := reflect.ValueOf(genericFlag.Value) + if flagValue.Kind() == reflect.Ptr { + flagValue = flagValue.Elem() + } - account, ok := c.Context.Generic("account").(*AccountNameFlag) - if ok { - *account = AccountNameFlag{} + flagValue.Set(reflect.Zero(flagValue.Type())) + } } } diff --git a/cmd/bytemark/app/with/privilege.go b/cmd/bytemark/app/with/privilege.go index 219b6b30..f662a238 100644 --- a/cmd/bytemark/app/with/privilege.go +++ b/cmd/bytemark/app/with/privilege.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/lib" "github.com/BytemarkHosting/bytemark-client/lib/brain" ) @@ -28,7 +29,7 @@ func normalisePrivilegeLevel(l brain.PrivilegeLevel) (level brain.PrivilegeLevel // Privilege gets the named PrivilegeFlag from the context, then resolves its target to an ID if needed to create a brain.Privilege, then attaches that to the context func Privilege(flagName string) func(*app.Context) error { return func(c *app.Context) (err error) { - pf := c.PrivilegeFlag(flagName) + pf := flags.Privilege(c, flagName) level, ok := normalisePrivilegeLevel(pf.Level) if !ok && !c.Bool("force") { return fmt.Errorf("Unexpected privilege level '%s' - expecting account_admin, group_admin, vm_admin or vm_console", pf.Level) @@ -97,7 +98,7 @@ func VirtualMachine(flagName string) func(*app.Context) error { if err != nil { return } - vmName := c.VirtualMachineName(flagName) + vmName := flags.VirtualMachineName(c, flagName) vm, err := c.Client().GetVirtualMachine(vmName) if err != nil { return diff --git a/cmd/bytemark/app/with/with.go b/cmd/bytemark/app/with/with.go index dfb88a8f..d786e07f 100644 --- a/cmd/bytemark/app/with/with.go +++ b/cmd/bytemark/app/with/with.go @@ -5,7 +5,6 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/urfave/cli" ) @@ -33,15 +32,15 @@ func flagValueIsOK(c *app.Context, flag cli.Flag) bool { switch realFlag := flag.(type) { case cli.GenericFlag: switch value := realFlag.Value.(type) { - case *flags.VirtualMachineName: + case *flags.VirtualMachineNameFlag: return value.VirtualMachineName != nil - case *flags.GroupName: + case *flags.GroupNameFlag: return value.GroupName != nil - case *flags.AccountName: + case *flags.AccountNameFlag: return value.AccountName != "" - case *util.SizeSpecFlag: + case *flags.SizeSpecFlag: return *value != 0 - case *flags.Privilege: + case *flags.PrivilegeFlag: return value.Username != "" && value.Level != "" } case cli.StringFlag: @@ -92,7 +91,7 @@ func Disc(vmFlagName, discFlagName string) func(*app.Context) error { return } - vmName := c.VirtualMachineName(vmFlagName) + vmName := flags.VirtualMachineName(c, vmFlagName) discLabel := c.String(discFlagName) disc, err := c.Client().GetDisc(vmName, discLabel) if err != nil { @@ -127,7 +126,7 @@ func Group(flagName string) func(*app.Context) error { return } - groupName := c.GroupName(flagName) + groupName := flags.GroupName(c, flagName) if groupName.Account == "" { groupName.Account = c.Config().GetIgnoreErr("account") } diff --git a/cmd/bytemark/commands/add/disc.go b/cmd/bytemark/commands/add/disc.go index e2ef62fc..752d017f 100644 --- a/cmd/bytemark/commands/add/disc.go +++ b/cmd/bytemark/commands/add/disc.go @@ -6,7 +6,6 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/util/log" "github.com/urfave/cli" ) @@ -19,13 +18,13 @@ func init() { cli.GenericFlag{ Name: "disc", Usage: "A disc to add. You can specify as many discs as you like by adding more --disc flags.", - Value: new(util.DiscSpecFlag), + Value: new(flags.DiscSpecFlag), }, flagsets.Force, cli.GenericFlag{ Name: "server", Usage: "the server to add the disc to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Usage: "add virtual discs attached to one of your cloud servers", @@ -40,7 +39,7 @@ Multiple --disc flags can be used to add multiple discs`, // createDiscs adds the disc(s) to the speicified server func createDiscs(c *app.Context) (err error) { - discs := c.Discs("disc") + discs := flags.Discs(c, "disc") for i := range discs { d, err := discs[i].Validate() @@ -49,7 +48,7 @@ func createDiscs(c *app.Context) (err error) { } discs[i] = *d } - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") log.Logf("Adding %d discs to %s:\r\n", len(discs), vmName) for _, d := range discs { diff --git a/cmd/bytemark/commands/add/group.go b/cmd/bytemark/commands/add/group.go index 303f3915..5eced350 100644 --- a/cmd/bytemark/commands/add/group.go +++ b/cmd/bytemark/commands/add/group.go @@ -20,7 +20,7 @@ func init() { cli.GenericFlag{ Name: "group", Usage: "the name of the group to add", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, }, Action: app.Action(args.Optional("group"), with.RequiredFlags("group"), with.Auth, createGroup), @@ -28,7 +28,7 @@ func init() { } func createGroup(c *app.Context) (err error) { - gp := c.GroupName("group") + gp := flags.GroupName(c, "group") err = c.Client().CreateGroup(gp) if err == nil { log.Logf("Group %s was created under account %s\r\n", gp.Group, gp.Account) diff --git a/cmd/bytemark/commands/add/server.go b/cmd/bytemark/commands/add/server.go index 7c562953..c880b873 100644 --- a/cmd/bytemark/commands/add/server.go +++ b/cmd/bytemark/commands/add/server.go @@ -46,11 +46,11 @@ If --hwprofile-locked is set then the cloud server's virtual hardware won't be c cli.GenericFlag{ Name: "name", Usage: "The new server's name", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.GenericFlag{ Name: "ip", - Value: new(util.IPFlag), + Value: new(flags.IPFlag), Usage: "Specify an IPv4 or IPv6 address to use. This will only be useful if you are creating the machine in a private VLAN.", }, }), @@ -61,7 +61,7 @@ If --hwprofile-locked is set then the cloud server's virtual hardware won't be c // createServer creates a server objec to be created by the brain and sends it. func createServer(c *app.Context) (err error) { - name := c.VirtualMachineName("name") + name := flags.VirtualMachineName(c, "name") spec, err := flagsets.PrepareServerSpec(c, true) if err != nil { return @@ -110,7 +110,7 @@ func createServer(c *app.Context) (err error) { // createServerReadIPs reads the IP flags and creates an IPSpec func createServerReadIPs(c *app.Context) (ipspec *brain.IPSpec, err error) { - ips := c.IPs("ip") + ips := flags.IPs(c, "ip") if len(ips) > 2 { err = c.Help("A maximum of one IPv4 and one IPv6 address may be specified") diff --git a/cmd/bytemark/commands/admin/add/vlan_group.go b/cmd/bytemark/commands/admin/add/vlan_group.go index f682998a..c7e40dd4 100644 --- a/cmd/bytemark/commands/admin/add/vlan_group.go +++ b/cmd/bytemark/commands/admin/add/vlan_group.go @@ -22,7 +22,7 @@ Used when setting up a private VLAN for a customer.`, cli.GenericFlag{ Name: "group", Usage: "the name of the group to add", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, cli.IntFlag{ Name: "vlan-num", @@ -30,7 +30,7 @@ Used when setting up a private VLAN for a customer.`, }, }, Action: app.Action(args.Optional("group", "vlan-num"), with.RequiredFlags("group"), with.Auth, func(c *app.Context) error { - gp := c.GroupName("group") + gp := flags.GroupName(c, "group") if err := c.Client().AdminCreateGroup(gp, c.Int("vlan-num")); err != nil { return err } diff --git a/cmd/bytemark/commands/admin/add/vm_default.go b/cmd/bytemark/commands/admin/add/vm_default.go index 2c51d5c9..942f7966 100644 --- a/cmd/bytemark/commands/admin/add/vm_default.go +++ b/cmd/bytemark/commands/admin/add/vm_default.go @@ -3,6 +3,7 @@ package add import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/cliutil" @@ -28,7 +29,7 @@ Multiple --disc flags can be used to add multiple discs to the VM Default If --backup is set then a backup of the first disk will be taken at the frequency specified - never, daily, weekly or monthly. If not specified the backup will default to weekly.`, - Flags: cliutil.ConcatFlags(flags.Outputs("vm default", "object"), + Flags: cliutil.ConcatFlags(app.OutputFlags("vm default", "object"), flagsets.ImageInstallFlags, flagsets.ServerSpecFlags, []cli.Flag{ cli.StringFlag{ @@ -42,7 +43,7 @@ frequency specified - never, daily, weekly or monthly. If not specified the back cli.GenericFlag{ Name: "account", Usage: "the account to add the default to (will use 'bytemark' if unset)", - Value: new(flags.AccountName), + Value: new(flags.AccountNameFlag), }, }), Action: app.Action(args.Optional("default-name"), with.RequiredFlags("default-name"), with.Auth, func(c *app.Context) (err error) { diff --git a/cmd/bytemark/commands/admin/migrate/server.go b/cmd/bytemark/commands/admin/migrate/server.go index a0b77705..fc28ea66 100644 --- a/cmd/bytemark/commands/admin/migrate/server.go +++ b/cmd/bytemark/commands/admin/migrate/server.go @@ -19,7 +19,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to migrate", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.StringFlag{ Name: "new-head", @@ -27,7 +27,7 @@ func init() { }, }, Action: app.Action(args.Optional("server", "new-head"), with.RequiredFlags("server"), with.Auth, func(ctx *app.Context) (err error) { - vmName := ctx.VirtualMachineName("server") + vmName := flags.VirtualMachineName(ctx, "server") head := ctx.String("new-head") vm, err := ctx.Client().GetVirtualMachine(vmName) diff --git a/cmd/bytemark/commands/admin/set.go b/cmd/bytemark/commands/admin/set.go index 2a2dfe15..c5d9f22a 100644 --- a/cmd/bytemark/commands/admin/set.go +++ b/cmd/bytemark/commands/admin/set.go @@ -28,7 +28,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server the disc belongs to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.IntFlag{ Name: "iops-limit", @@ -40,7 +40,7 @@ func init() { if iopsLimit < 1 { return fmt.Errorf("IOPS limit must be at least 1") } - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") return c.Client().SetDiscIopsLimit(vmName, c.String("disc"), iopsLimit) }), diff --git a/cmd/bytemark/commands/admin/show.go b/cmd/bytemark/commands/admin/show.go index 5aa70e16..7817c19e 100644 --- a/cmd/bytemark/commands/admin/show.go +++ b/cmd/bytemark/commands/admin/show.go @@ -5,9 +5,9 @@ import ( "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/commands/admin/show" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" "github.com/BytemarkHosting/bytemark-client/lib/brain" "github.com/BytemarkHosting/bytemark-client/lib/output" brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" @@ -276,7 +276,7 @@ var showCommands = []cli.Command{ }, cli.GenericFlag{ Name: "at", - Value: new(util.DateTimeFlag), + Value: new(flags.DateTimeFlag), Usage: "the date and time in history to check the dependant servers, defaults to now if unset. The flag will take most formats, e.g. '20/01/18', '15:30', '31/02/2018 18:20:33'", }, ), @@ -319,7 +319,7 @@ var showCommands = []cli.Command{ }, cli.GenericFlag{ Name: "at", - Value: new(util.DateTimeFlag), + Value: new(flags.DateTimeFlag), Usage: "the date and time in history to check the dependant discs, defaults to now if unset. The flag will take most formats, e.g. '20/01/18', '15:30', '31/02/2018 18:20:33'", }, ), diff --git a/cmd/bytemark/commands/admin/update.go b/cmd/bytemark/commands/admin/update.go index e65190a2..70f6c200 100644 --- a/cmd/bytemark/commands/admin/update.go +++ b/cmd/bytemark/commands/admin/update.go @@ -212,7 +212,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to migrate", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.Int64Flag{ Name: "migrate-speed", @@ -224,7 +224,7 @@ func init() { }, }, Action: app.Action(args.Optional("server", "migrate-speed", "migrate-downtime"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) error { - vm := c.VirtualMachineName("server") + vm := flags.VirtualMachineName(c, "server") var speed *int64 var downtime *int diff --git a/cmd/bytemark/commands/assent.go b/cmd/bytemark/commands/assent.go index 238d13dd..719e044d 100644 --- a/cmd/bytemark/commands/assent.go +++ b/cmd/bytemark/commands/assent.go @@ -30,7 +30,7 @@ func init() { cli.GenericFlag{ Name: "account", Usage: "the account which is assenting", - Value: new(flags.AccountName), + Value: new(flags.AccountNameFlag), }, cli.IntFlag{ Name: "accountid", @@ -47,7 +47,7 @@ func init() { }, Action: app.Action(with.RequiredFlags("agreement", "person"), func(ctx *app.Context) error { accountString := ctx.String("account") - accountNameFlag := ctx.Context.Generic("account").(*flags.AccountName) + accountNameFlag := flags.AccountName(ctx, "account") accountID := ctx.Int("accountid") var account billing.Account diff --git a/cmd/bytemark/commands/backup.go b/cmd/bytemark/commands/backup.go index a18b41fa..e59f550c 100644 --- a/cmd/bytemark/commands/backup.go +++ b/cmd/bytemark/commands/backup.go @@ -29,11 +29,11 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server whose disk you wish to backup", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server", "disc"), with.RequiredFlags("server", "disc"), with.Auth, func(c *app.Context) error { - backup, err := c.Client().CreateBackup(c.VirtualMachineName("server"), c.String("disc")) + backup, err := c.Client().CreateBackup(flags.VirtualMachineName(c, "server"), c.String("disc")) if err != nil { return err } diff --git a/cmd/bytemark/console.go b/cmd/bytemark/commands/console.go similarity index 97% rename from cmd/bytemark/console.go rename to cmd/bytemark/commands/console.go index ee0ed42c..97921d26 100644 --- a/cmd/bytemark/console.go +++ b/cmd/bytemark/commands/console.go @@ -1,4 +1,4 @@ -package main +package commands import ( "fmt" @@ -18,7 +18,7 @@ import ( ) func init() { - commands = append(commands, cli.Command{ + Commands = append(Commands, cli.Command{ Name: "console", Usage: "connect to a server's serial or graphical console - as though physically plugging in", UsageText: "console [--serial | --vnc | --panel] [--no-connect] ", @@ -49,11 +49,11 @@ Defaults to connecting to the serial console for the given server.`, cli.GenericFlag{ Name: "server", Usage: "The server whose console will be connected to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(ctx *app.Context) (err error) { - vmName := ctx.VirtualMachineName("server") + vmName := flags.VirtualMachineName(ctx, "server") if ctx.Bool("serial") && ctx.Bool("panel") { return ctx.Help("You must only specify one of --serial and --panel!") } diff --git a/cmd/bytemark/console_test.go b/cmd/bytemark/commands/console_test.go similarity index 97% rename from cmd/bytemark/console_test.go rename to cmd/bytemark/commands/console_test.go index b754a04f..15adc114 100644 --- a/cmd/bytemark/console_test.go +++ b/cmd/bytemark/commands/console_test.go @@ -1,4 +1,4 @@ -package main +package commands import ( "testing" diff --git a/cmd/bytemark/commands/delete/backup.go b/cmd/bytemark/commands/delete/backup.go new file mode 100644 index 00000000..b5acb6f6 --- /dev/null +++ b/cmd/bytemark/commands/delete/backup.go @@ -0,0 +1,41 @@ +package delete + +import ( + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" + "github.com/urfave/cli" +) + +func init() { + Commands = append(Commands, cli.Command{ + Name: "backup", + Usage: "delete the given backup", + UsageText: `delete backup `, + Description: "Deletes the given backup. Backups cannot be recovered after deletion.", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "disc", + Usage: "the disc to delete a backup of", + }, + cli.GenericFlag{ + Name: "server", + Usage: "the server to delete a backup from", + Value: new(flags.VirtualMachineNameFlag), + }, + cli.StringFlag{ + Name: "backup", + Usage: "the name or ID of the backup to delete", + }, + }, + Action: app.Action(args.Optional("server", "disc", "backup"), with.RequiredFlags("server", "disc", "backup"), with.Auth, func(c *app.Context) (err error) { + err = c.Client().DeleteBackup(flags.VirtualMachineName(c, "server"), c.String("disc"), c.String("backup")) + if err != nil { + return + } + c.Log("Backup '%s' deleted successfully", c.String("backup")) + return + }), + }) +} diff --git a/cmd/bytemark/commands/delete/disc.go b/cmd/bytemark/commands/delete/disc.go new file mode 100644 index 00000000..f0d1aead --- /dev/null +++ b/cmd/bytemark/commands/delete/disc.go @@ -0,0 +1,56 @@ +package delete + +import ( + "fmt" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" + brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" + "github.com/urfave/cli" +) + +func init() { + Commands = append(Commands, cli.Command{ + Name: "disc", + Usage: "delete the given disc", + UsageText: "delete disc [--server --label ] | [--id ]", + Description: "Deletes the given disc. To find out a disc's label you can use the `bytemark show server` command or `bytemark list discs` command.", + Flags: []cli.Flag{ + flagsets.Force, + cli.StringFlag{ + Name: "label", + Usage: "the disc to delete, must provide a server too", + }, + cli.GenericFlag{ + Name: "server", + Usage: "the server whose disc you wish to delete, must provide a label too", + Value: new(flags.VirtualMachineNameFlag), + }, + cli.StringFlag{ + Name: "id", + Usage: "the ID of the disc to delete", + }, + }, + Aliases: []string{"disk"}, + Action: app.Action(args.Optional("server", "label", "id"), with.Auth, func(c *app.Context) (err error) { + if !c.Bool("force") && !util.PromptYesNo(c.Prompter(), "Are you sure you wish to delete this disc? It is impossible to recover.") { + return util.UserRequestedExit{} + } + vmName := flags.VirtualMachineName(c, "server") + discLabel := c.String("label") + discID := c.String("id") + + if discID != "" { + return brainRequests.DeleteDiscByID(c.Client(), discID) + } else if vmName.String() != "" && discLabel != "" { + return c.Client().DeleteDisc(vmName, discLabel) + } else { + return fmt.Errorf("Please include both --server and --label flags or provide --id") + } + }), + }) +} diff --git a/cmd/bytemark/commands/delete/group.go b/cmd/bytemark/commands/delete/group.go index 3a59d99d..031b8f46 100644 --- a/cmd/bytemark/commands/delete/group.go +++ b/cmd/bytemark/commands/delete/group.go @@ -29,13 +29,13 @@ If --recursive is specified, all servers in the group will be purged. Otherwise, cli.GenericFlag{ Name: "group", Usage: "the name of the group to delete", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, flagsets.Force, }, Action: app.Action(args.Optional("group"), with.RequiredFlags("group"), with.Group("group"), func(ctx *app.Context) (err error) { recursive := ctx.Bool("recursive") - groupName := ctx.GroupName("group") + groupName := flags.GroupName(ctx, "group") if len(ctx.Group.VirtualMachines) > 0 { if !recursive { err = util.WontDeleteGroupWithVMsError{Group: groupName} diff --git a/cmd/bytemark/commands/delete/key.go b/cmd/bytemark/commands/delete/key.go new file mode 100644 index 00000000..13a0b794 --- /dev/null +++ b/cmd/bytemark/commands/delete/key.go @@ -0,0 +1,45 @@ +package delete + +import ( + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" + brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" + "github.com/urfave/cli" +) + +func init() { + Commands = append(Commands, cli.Command{ + Name: "key", + Usage: "deletes the specified key", + UsageText: "delete key [--user ] ", + Description: "Keys may be specified as just the comment part or as the whole key. If there are multiple keys with the comment given, an error will be returned", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "user", + Usage: "Which user to delete the key from. Defaults to the username you log in as.", + }, + cli.StringFlag{ + Name: "public-key", + Usage: "The public key to delete. Can be the comment part or the whole public key", + }, + }, + Action: app.Action(args.Join("public-key"), with.RequiredFlags("public-key"), with.Auth, func(c *app.Context) (err error) { + user := c.String("user") + if user == "" { + user = c.Config().GetIgnoreErr("user") + } + + key := c.String("public-key") + if key == "" { + return c.Help("You must specify a key to delete.\r\n") + } + + err = brainRequests.DeleteUserAuthorizedKey(c.Client(), user, key) + if err == nil { + c.Log("Key deleted successfully") + } + return + }), + }) +} diff --git a/cmd/bytemark/commands/delete/server.go b/cmd/bytemark/commands/delete/server.go new file mode 100644 index 00000000..96556c2a --- /dev/null +++ b/cmd/bytemark/commands/delete/server.go @@ -0,0 +1,66 @@ +package delete + +import ( + "fmt" + + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flagsets" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" + "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" + "github.com/urfave/cli" +) + +func init() { + Commands = append(Commands, cli.Command{ + Name: "server", + Usage: "delete the given server", + UsageText: `delete server [--purge] `, + Description: "Deletes the given server. Deleted servers still exist and can be restored. To ensure a server is fully deleted, use the --purge flag.", + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "purge", + Usage: "If set, the server will be irrevocably deleted.", + }, + flagsets.Force, + cli.GenericFlag{ + Name: "server", + Usage: "the server to delete", + Value: new(flags.VirtualMachineNameFlag), + }, + }, + Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) (err error) { + purge := c.Bool("purge") + vm := c.VirtualMachine + + if vm.Deleted && !purge { + c.LogErr("Server %s has already been deleted.\r\nIf you wish to permanently delete it, add --purge", vm.Hostname) + // we don't return an error because we want a 0 exit code - the deletion request has happened, just not now. + return + } + fstr := fmt.Sprintf("Are you certain you wish to delete %s?", vm.Hostname) + if purge { + fstr = fmt.Sprintf("Are you certain you wish to permanently delete %s? You will not be able to un-delete it.", vm.Hostname) + + } + + if !c.Bool("force") && !util.PromptYesNo(c.Prompter(), fstr) { + err = util.UserRequestedExit{} + return + } + + vmName := flags.VirtualMachineName(c, "server") + err = c.Client().DeleteVirtualMachine(vmName, purge) + if err != nil { + return + } + if purge { + c.Log("Server %s purged successfully.", vm.Hostname) + } else { + c.Log("Server %s deleted successfully.", vm.Hostname) + } + return + }), + }) +} diff --git a/cmd/bytemark/commands/grant.go b/cmd/bytemark/commands/grant.go index f97519af..5e204b89 100644 --- a/cmd/bytemark/commands/grant.go +++ b/cmd/bytemark/commands/grant.go @@ -44,7 +44,7 @@ func init() { cli.GenericFlag{ Name: "privilege", Usage: "A privilege written out like ' [on] [to] ", - Value: new(flags.Privilege), + Value: new(flags.PrivilegeFlag), }, }, Action: app.Action(args.Join("privilege"), with.RequiredFlags("privilege"), with.Privilege("privilege"), func(c *app.Context) (err error) { @@ -59,7 +59,7 @@ func init() { err = c.Client().GrantPrivilege(c.Privilege) if err == nil { - log.Outputf("Granted %s\r\n", c.PrivilegeFlag("privilege").String()) + log.Outputf("Granted %s\r\n", flags.Privilege(c, "privilege").String()) } return }), diff --git a/cmd/bytemark/commands/reset_server.go b/cmd/bytemark/commands/reset_server.go index a26bcd11..8ecc4a85 100644 --- a/cmd/bytemark/commands/reset_server.go +++ b/cmd/bytemark/commands/reset_server.go @@ -9,12 +9,6 @@ import ( ) func init() { - serverFlag := cli.GenericFlag{ - Name: "server", - Usage: "the server to reset", - Value: new(flags.VirtualMachineName), - } - Commands = append(Commands, cli.Command{ Name: "reset", Usage: "restart a server as though the reset button had been pushed", @@ -22,7 +16,11 @@ func init() { Description: "For cloud servers, this does not cause the qemu process to be restarted. This means that the server will remain on the same head and will not notice hardware changes.", Action: cli.ShowSubcommandHelp, Flags: []cli.Flag{ - serverFlag, + cli.GenericFlag{ + Name: "server", + Usage: "the server to reset", + Value: new(flags.VirtualMachineNameFlag), + }, }, Subcommands: []cli.Command{{ Name: "server", @@ -30,10 +28,14 @@ func init() { UsageText: "reset server ", Description: "For cloud servers, this does not cause the qemu process to be restarted. This means that the server will remain on the same head and will not notice hardware changes.", Flags: []cli.Flag{ - serverFlag, + cli.GenericFlag{ + Name: "server", + Usage: "the server to reset", + Value: new(flags.VirtualMachineNameFlag), + }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") c.LogErr("Attempting to reset %v...\r\n", vmName) err = c.Client().ResetVirtualMachine(vmName) if err != nil { diff --git a/cmd/bytemark/commands/restart_server.go b/cmd/bytemark/commands/restart_server.go index bfabfa11..4964ccb4 100644 --- a/cmd/bytemark/commands/restart_server.go +++ b/cmd/bytemark/commands/restart_server.go @@ -27,7 +27,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to restart", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.BoolFlag{ Name: "rescue", @@ -39,7 +39,7 @@ func init() { }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") appliance := c.String("appliance") if appliance != "" && c.Bool("rescue") { diff --git a/cmd/bytemark/commands/revoke.go b/cmd/bytemark/commands/revoke.go index af01e5eb..7d29ad24 100644 --- a/cmd/bytemark/commands/revoke.go +++ b/cmd/bytemark/commands/revoke.go @@ -31,11 +31,11 @@ func init() { cli.GenericFlag{ Name: "privilege", Usage: "the privilege to revoke", - Value: new(flags.Privilege), + Value: new(flags.PrivilegeFlag), }, }, Action: app.Action(args.Join("privilege"), with.RequiredFlags("privilege"), with.Privilege("privilege"), func(c *app.Context) (err error) { - pf := c.PrivilegeFlag("privilege") + pf := flags.Privilege(c, "privilege") c.Privilege.YubikeyRequired = c.Bool("yubikey-required") var privs brain.Privileges @@ -68,7 +68,7 @@ func init() { err = c.Client().RevokePrivilege(privs[i]) if err == nil { - log.Outputf("Revoked %s\r\n", c.PrivilegeFlag("privilege").String()) + log.Outputf("Revoked %s\r\n", pf.String()) } return diff --git a/cmd/bytemark/commands/show/backups.go b/cmd/bytemark/commands/show/backups.go index 9654e69c..74da69dd 100644 --- a/cmd/bytemark/commands/show/backups.go +++ b/cmd/bytemark/commands/show/backups.go @@ -24,11 +24,11 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server you wish to list the backups of", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, ), Action: app.Action(args.Optional("server", "disc"), with.RequiredFlags("server", "disc"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") label := c.String("disc") var backups brain.Backups diff --git a/cmd/bytemark/commands/show/discs.go b/cmd/bytemark/commands/show/discs.go index fdbbe4de..14f57674 100644 --- a/cmd/bytemark/commands/show/discs.go +++ b/cmd/bytemark/commands/show/discs.go @@ -23,7 +23,7 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server whose discs you wish to list", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, ), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) error { diff --git a/cmd/bytemark/commands/show/groups.go b/cmd/bytemark/commands/show/groups.go index c82f4fae..4f2131fe 100644 --- a/cmd/bytemark/commands/show/groups.go +++ b/cmd/bytemark/commands/show/groups.go @@ -19,7 +19,7 @@ func init() { cli.GenericFlag{ Name: "account", Usage: "the account to list the groups of", - Value: new(flags.AccountName), + Value: new(flags.AccountNameFlag), }, ), Action: app.Action(args.Optional("account"), with.RequiredFlags("account"), with.Account("account"), func(c *app.Context) error { diff --git a/cmd/bytemark/commands/show/servers.go b/cmd/bytemark/commands/show/servers.go index a0ce93f7..8a5a19aa 100644 --- a/cmd/bytemark/commands/show/servers.go +++ b/cmd/bytemark/commands/show/servers.go @@ -25,7 +25,7 @@ If --group and --account are specified, the group will be displayed and the acco cli.GenericFlag{ Name: "group", Usage: "the group to list the servers of", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, // TODO: change to AccountNameFlag cli.StringFlag{ @@ -36,7 +36,7 @@ If --group and --account are specified, the group will be displayed and the acco Action: app.Action(args.Optional("group"), with.Auth, func(c *app.Context) error { servers := brain.VirtualMachines{} if c.IsSet("group") { - groupName := c.GroupName("group") + groupName := flags.GroupName(c, "group") group, err := c.Client().GetGroup(groupName) if err != nil { return err diff --git a/cmd/bytemark/commands/shutdown_server.go b/cmd/bytemark/commands/shutdown_server.go index 7217817e..1075b056 100644 --- a/cmd/bytemark/commands/shutdown_server.go +++ b/cmd/bytemark/commands/shutdown_server.go @@ -25,11 +25,11 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to shutdown", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") c.Log("Shutting down %v...", vmName) err = c.Client().ShutdownVirtualMachine(vmName, true) if err != nil { diff --git a/cmd/bytemark/commands/start_server.go b/cmd/bytemark/commands/start_server.go index 7f1f7d5d..3a14f357 100644 --- a/cmd/bytemark/commands/start_server.go +++ b/cmd/bytemark/commands/start_server.go @@ -24,11 +24,11 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to start", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") c.Log("Attempting to start %s...", vmName) err = c.Client().StartVirtualMachine(vmName) if err != nil { diff --git a/cmd/bytemark/commands/stop_server.go b/cmd/bytemark/commands/stop_server.go index 100af3e5..930e1faa 100644 --- a/cmd/bytemark/commands/stop_server.go +++ b/cmd/bytemark/commands/stop_server.go @@ -25,11 +25,11 @@ func init() { cli.GenericFlag{ Name: "server", Usage: "the server to stop", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") c.Log("Attempting to stop %s...", vmName) err = c.Client().StopVirtualMachine(vmName) if err != nil { diff --git a/cmd/bytemark/commands/update/disc.go b/cmd/bytemark/commands/update/disc.go index c766d328..abe1265d 100644 --- a/cmd/bytemark/commands/update/disc.go +++ b/cmd/bytemark/commands/update/disc.go @@ -34,17 +34,17 @@ Moving the disc to another server may require you to update your operating syste cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.GenericFlag{ Name: "new-size", Usage: "the new size for the disc. Prefix with + to indicate 'increase by'", - Value: new(flags.Resize), + Value: new(flags.ResizeFlag), }, cli.GenericFlag{ Name: "new-server", Usage: "the server that the disc should be moved to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server", "disc", "new-size", "new-server"), with.RequiredFlags("server", "disc"), with.Disc("server", "disc"), updateDisc), @@ -70,10 +70,10 @@ func updateDisc(c *app.Context) (err error) { } func resizeDisc(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") - size := c.ResizeFlag("new-size") + vmName := flags.VirtualMachineName(c, "server") + size := flags.Resize(c, "new-size") newSize := size.Size - if size.Mode == app.ResizeModeIncrease { + if size.Mode == flags.ResizeModeIncrease { newSize += c.Disc.Size } @@ -93,8 +93,8 @@ func resizeDisc(c *app.Context) (err error) { } func moveDisc(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") - newVM := c.VirtualMachineName("new-server") + vmName := flags.VirtualMachineName(c, "server") + newVM := flags.VirtualMachineName(c, "new-server") log.Logf("This may require an update to the operating system configuration, please find documentation for moving discs at https://docs.bytemark.co.uk/article/moving-a-disc\r\n") diff --git a/cmd/bytemark/commands/update/server.go b/cmd/bytemark/commands/update/server.go index 9309f749..267646ce 100644 --- a/cmd/bytemark/commands/update/server.go +++ b/cmd/bytemark/commands/update/server.go @@ -48,7 +48,7 @@ EXAMPLES flagsets.Force, cli.GenericFlag{ Name: "memory", - Value: new(util.SizeSpecFlag), + Value: new(flags.SizeSpecFlag), Usage: "How much memory the server will have available, specified in GiB or with GiB/MiB units.", }, cli.StringFlag{ @@ -66,7 +66,7 @@ EXAMPLES cli.GenericFlag{ Name: "new-name", Usage: "A new name for the server", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.IntFlag{ Name: "cores", @@ -83,7 +83,7 @@ EXAMPLES cli.GenericFlag{ Name: "server", Usage: "The server to update", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, ), Action: app.Action(args.Optional("new-name", "hwprofile", "memory"), with.RequiredFlags("server"), with.VirtualMachine("server"), with.Auth, updateServer), @@ -91,8 +91,8 @@ EXAMPLES } func updateMemory(c *app.Context) error { - vmName := c.VirtualMachineName("server") - memory := c.Size("memory") + vmName := flags.VirtualMachineName(c, "server") + memory := flags.Size(c, "memory") if memory == 0 { return nil @@ -106,7 +106,7 @@ func updateMemory(c *app.Context) error { } func updateHwProfile(c *app.Context) error { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") hwProfile := c.String("hwprofile") if hwProfile == "" { return nil @@ -116,7 +116,7 @@ func updateHwProfile(c *app.Context) error { } func updateLock(c *app.Context) error { - server := c.VirtualMachineName("server") + server := flags.VirtualMachineName(c, "server") lockProfile := c.Bool("lock-hwprofile") unlockProfile := c.Bool("unlock-hwprofile") @@ -131,7 +131,7 @@ func updateLock(c *app.Context) error { } func updateCores(c *app.Context) error { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") cores := c.Int("cores") if cores == 0 { @@ -146,8 +146,8 @@ func updateCores(c *app.Context) error { } func updateName(c *app.Context) error { - vmName := c.VirtualMachineName("server") - newName := c.VirtualMachineName("new-name") + vmName := flags.VirtualMachineName(c, "server") + newName := flags.VirtualMachineName(c, "new-name") if newName.VirtualMachine == "" { return nil @@ -156,7 +156,7 @@ func updateName(c *app.Context) error { } func updateCdrom(c *app.Context) error { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") cdURL := c.String("cd-url") removeCD := c.Bool("remove-cd") diff --git a/cmd/bytemark/delete.go b/cmd/bytemark/delete.go deleted file mode 100644 index f9f26f53..00000000 --- a/cmd/bytemark/delete.go +++ /dev/null @@ -1,181 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" - "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util" - brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" - "github.com/BytemarkHosting/bytemark-client/util/log" - "github.com/urfave/cli" -) - -func init() { - - commands = append(commands, cli.Command{ - Name: "delete", - Usage: "delete a given server, disc, group, account or key", - UsageText: "delete account|disc|group|key|server", - Description: `delete a given server, disc, group, account or key - - Only empty groups and accounts can be deleted. - - The restore server command may be used to restore a deleted (but not purged) server to its state prior to deletion.`, - Action: cli.ShowSubcommandHelp, - Subcommands: []cli.Command{{ - Name: "disc", - Usage: "delete the given disc", - UsageText: "delete disc [--server --label ] | [--id ]", - Description: "Deletes the given disc. To find out a disc's label you can use the `bytemark show server` command or `bytemark list discs` command.", - Flags: []cli.Flag{ - forceFlag, - cli.StringFlag{ - Name: "label", - Usage: "the disc to delete, must provide a server too", - }, - cli.GenericFlag{ - Name: "server", - Usage: "the server whose disc you wish to delete, must provide a label too", - Value: new(flags.VirtualMachineName), - }, - cli.StringFlag{ - Name: "id", - Usage: "the ID of the disc to delete", - }, - }, - Aliases: []string{"disk"}, - Action: app.Action(args.Optional("server", "label", "id"), with.Auth, func(c *app.Context) (err error) { - if !c.Bool("force") && !util.PromptYesNo(c.Prompter(), "Are you sure you wish to delete this disc? It is impossible to recover.") { - return util.UserRequestedExit{} - } - vmName := c.VirtualMachineName("server") - discLabel := c.String("label") - discID := c.String("id") - - if discID != "" { - return brainRequests.DeleteDiscByID(c.Client(), discID) - } else if vmName.String() != "" && discLabel != "" { - return c.Client().DeleteDisc(vmName, discLabel) - } else { - return fmt.Errorf("Please include both --server and --label flags or provide --id") - } - }), - }, { - Name: "key", - Usage: "deletes the specified key", - UsageText: "delete key [--user ] ", - Description: "Keys may be specified as just the comment part or as the whole key. If there are multiple keys with the comment given, an error will be returned", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "user", - Usage: "Which user to delete the key from. Defaults to the username you log in as.", - }, - cli.StringFlag{ - Name: "public-key", - Usage: "The public key to delete. Can be the comment part or the whole public key", - }, - }, - Action: app.Action(args.Join("public-key"), with.RequiredFlags("public-key"), with.Auth, func(c *app.Context) (err error) { - user := c.String("user") - if user == "" { - user = c.Config().GetIgnoreErr("user") - } - - key := c.String("public-key") - if key == "" { - return c.Help("You must specify a key to delete.\r\n") - } - - err = brainRequests.DeleteUserAuthorizedKey(c.Client(), user, key) - if err == nil { - log.Log("Key deleted successfully") - } - return - }), - }, { - Name: "server", - Usage: "delete the given server", - UsageText: `delete server [--purge] `, - Description: "Deletes the given server. Deleted servers still exist and can be restored. To ensure a server is fully deleted, use the --purge flag.", - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "purge", - Usage: "If set, the server will be irrevocably deleted.", - }, - forceFlag, - cli.GenericFlag{ - Name: "server", - Usage: "the server to delete", - Value: new(flags.VirtualMachineName), - }, - }, - Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), deleteServer), - }, { - Name: "backup", - Usage: "delete the given backup", - UsageText: `delete backup `, - Description: "Deletes the given backup. Backups cannot be recovered after deletion.", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "disc", - Usage: "the disc to delete a backup of", - }, - cli.GenericFlag{ - Name: "server", - Usage: "the server to delete a backup from", - Value: new(flags.VirtualMachineName), - }, - cli.StringFlag{ - Name: "backup", - Usage: "the name or ID of the backup to delete", - }, - }, - Action: app.Action(args.Optional("server", "disc", "backup"), with.RequiredFlags("server", "disc", "backup"), with.Auth, deleteBackup), - }}, - }) -} - -func deleteServer(c *app.Context) (err error) { - purge := c.Bool("purge") - vm := c.VirtualMachine - - if vm.Deleted && !purge { - log.Errorf("Server %s has already been deleted.\r\nIf you wish to permanently delete it, add --purge\r\n", vm.Hostname) - // we don't return an error because we want a 0 exit code - the deletion request has happened, just not now. - return - } - fstr := fmt.Sprintf("Are you certain you wish to delete %s?", vm.Hostname) - if purge { - fstr = fmt.Sprintf("Are you certain you wish to permanently delete %s? You will not be able to un-delete it.", vm.Hostname) - - } - - if !c.Bool("force") && !util.PromptYesNo(c.Prompter(), fstr) { - err = util.UserRequestedExit{} - return - } - - vmName := c.VirtualMachineName("server") - err = c.Client().DeleteVirtualMachine(vmName, purge) - if err != nil { - return - } - if purge { - log.Logf("Server %s purged successfully.\r\n", vm.Hostname) - } else { - log.Logf("Server %s deleted successfully.\r\n", vm.Hostname) - } - return -} - -func deleteBackup(c *app.Context) (err error) { - err = c.Client().DeleteBackup(c.VirtualMachineName("server"), c.String("disc"), c.String("backup")) - if err != nil { - return - } - log.Logf("Backup '%s' deleted successfully", c.String("backup")) - return -} diff --git a/cmd/bytemark/delete_test.go b/cmd/bytemark/delete_test.go index e5acc850..ed6dcc20 100644 --- a/cmd/bytemark/delete_test.go +++ b/cmd/bytemark/delete_test.go @@ -23,7 +23,7 @@ func TestDeleteServer(t *testing.T) { vm := getFixtureVM() t.Run("force delete", func(t *testing.T) { - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) config.When("GetVirtualMachine").Return(defVM) @@ -38,7 +38,7 @@ func TestDeleteServer(t *testing.T) { } }) t.Run("force purge", func(t *testing.T) { - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) config.When("GetVirtualMachine").Return(defVM) @@ -56,7 +56,7 @@ func TestDeleteServer(t *testing.T) { func TestDeleteDisc(t *testing.T) { t.Run("server and label", func(t *testing.T) { is := is.New(t) - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) config.When("GetVirtualMachine").Return(defVM) @@ -77,7 +77,7 @@ func TestDeleteDisc(t *testing.T) { }) t.Run("disc ID", func(t *testing.T) { is := is.New(t) - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) @@ -107,7 +107,7 @@ func TestDeleteKey(t *testing.T) { } t.Run("full key", func(t *testing.T) { is := is.New(t) - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) c.When("GetUser", usr.Username).Return(usr) @@ -125,7 +125,7 @@ func TestDeleteKey(t *testing.T) { }) t.Run("delete by ambiguous comment is err", func(t *testing.T) { - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) config.When("Force").Return(true) config.When("GetIgnoreErr", "user").Return("test-user") @@ -147,7 +147,7 @@ func TestDeleteKey(t *testing.T) { func TestDeleteBackup(t *testing.T) { is := is.New(t) - config, c, app := testutil.BaseTestAuthSetup(t, false, commands) + config, c, app := testutil.BaseTestAuthSetup(t, false, Commands(false)) vmname := lib.VirtualMachineName{ VirtualMachine: "test-server", diff --git a/cmd/bytemark/reimage.go b/cmd/bytemark/reimage.go index d5ddd55f..014bebbd 100644 --- a/cmd/bytemark/reimage.go +++ b/cmd/bytemark/reimage.go @@ -40,11 +40,11 @@ The root password will be output on stdout if the imaging succeeded, otherwise n forceFlag, cli.GenericFlag{ Name: "server", Usage: "the server to reimage", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") imageInstall, defaulted, err := flagsets.PrepareImageInstall(c, true) if err != nil { return diff --git a/cmd/bytemark/restore.go b/cmd/bytemark/restore.go index d6c568d6..160e723f 100644 --- a/cmd/bytemark/restore.go +++ b/cmd/bytemark/restore.go @@ -27,11 +27,11 @@ Note that it cannot be used to restore a server that has been permanently delete cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, }, Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) (err error) { - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") if !c.VirtualMachine.Deleted { log.Errorf("%s was already restored\r\n", c.VirtualMachine.Hostname) return @@ -58,7 +58,7 @@ Note that it cannot be used to restore a server that has been permanently delete cli.GenericFlag{ Name: "server", Usage: "the server that the disc is attached to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.StringFlag{ Name: "backup", @@ -67,7 +67,7 @@ Note that it cannot be used to restore a server that has been permanently delete }, Action: app.Action(args.Optional("server", "disc", "backup"), with.RequiredFlags("server", "disc", "backup"), with.Auth, func(c *app.Context) (err error) { // TODO(telyn): eventually RestoreBackup will return backups as the first argument. We should process that and output info :) - _, err = c.Client().RestoreBackup(c.VirtualMachineName("server"), c.String("disc"), c.String("backup")) + _, err = c.Client().RestoreBackup(flags.VirtualMachineName(c, "server"), c.String("disc"), c.String("backup")) if err != nil { return } diff --git a/cmd/bytemark/schedule.go b/cmd/bytemark/schedule.go index 44e6efd3..183903c6 100644 --- a/cmd/bytemark/schedule.go +++ b/cmd/bytemark/schedule.go @@ -41,7 +41,7 @@ bytemark schedule backups --start "2017-04-05T14:37:00+02:00" fileserver very-im cli.GenericFlag{ Name: "server", Usage: "the server the disc belongs to", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.IntFlag{ Name: "interval", @@ -64,7 +64,7 @@ bytemark schedule backups --start "2017-04-05T14:37:00+02:00" fileserver very-im start = "00:00" } - vmName := c.VirtualMachineName("server") + vmName := flags.VirtualMachineName(c, "server") sched, err := c.Client().CreateBackupSchedule(vmName, c.String("disc"), start, c.Int("interval")) if err == nil { log.Logf("Schedule set. Backups will be taken every %d seconds.", sched.Interval) diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index 4555b70d..96187c2a 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -33,7 +33,7 @@ If the --json flag is specified, prints a complete overview of the account in JS cli.GenericFlag{ Name: "account", Usage: "The account to view", - Value: new(flags.AccountName), + Value: new(flags.AccountNameFlag), }), Action: app.Action(args.Optional("account"), with.Account("account"), func(c *app.Context) error { c.Debug("show account command output") @@ -49,7 +49,7 @@ If the --json flag is specified, prints a complete overview of the account in JS cli.GenericFlag{ Name: "server", Usage: "the server to display", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, cli.StringFlag{ Name: "disc", @@ -69,7 +69,7 @@ If the --json flag is specified, prints a complete overview of the group in JSON cli.GenericFlag{ Name: "group", Usage: "The name of the group to show", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, ), Action: app.Action(args.Optional("group"), with.Group("group"), func(c *app.Context) error { @@ -84,7 +84,7 @@ If the --json flag is specified, prints a complete overview of the group in JSON cli.GenericFlag{ Name: "server", Usage: "the server to display", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, ), Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.VirtualMachine("server"), func(c *app.Context) error { @@ -133,18 +133,18 @@ Privileges will be output in no particular order.`, cli.GenericFlag{ Name: "group", Usage: "The group to show the privileges of", - Value: new(flags.GroupName), + Value: new(flags.GroupNameFlag), }, cli.GenericFlag{ Name: "server", Usage: "The server to show the privileges of", - Value: new(flags.VirtualMachineName), + Value: new(flags.VirtualMachineNameFlag), }, ), Action: app.Action(with.Auth, func(c *app.Context) (err error) { account := c.String("account") - group := c.GroupName("group") - server := c.VirtualMachineName("server") + group := flags.GroupName(c, "group") + server := flags.VirtualMachineName(c, "server") privs := make(brain.Privileges, 0) if account != "" { diff --git a/cmd/bytemark/unschedule.go b/cmd/bytemark/unschedule.go index 8c425fb7..98f10923 100644 --- a/cmd/bytemark/unschedule.go +++ b/cmd/bytemark/unschedule.go @@ -34,7 +34,7 @@ The is a number that can be found out using 'bytemark show disc is a number that can be found out using 'bytemark show disc Date: Thu, 6 Dec 2018 10:48:45 +0000 Subject: [PATCH 06/23] fix bug introduced in add key command --- cmd/bytemark/add.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/bytemark/add.go b/cmd/bytemark/add.go index 9077648a..476ff642 100644 --- a/cmd/bytemark/add.go +++ b/cmd/bytemark/add.go @@ -14,7 +14,6 @@ import ( ) func init() { - publicKeyFile := flags.FileFlag{} commands = append(commands, cli.Command{ Name: "add", Usage: "add SSH keys to a user / IPs to a server", @@ -46,17 +45,19 @@ func init() { if user == "" { user = ctx.Config().GetIgnoreErr("user") } + publicKeyFileContents := flags.FileContents(ctx, "public-key-file") key := strings.TrimSpace(ctx.String("public-key")) if key == "" { - if publicKeyFile.Value == "" { + + if publicKeyFileContents == "" { return ctx.Help("Please specify a key") } - key = publicKeyFile.Value + key = publicKeyFileContents } else { // if public-key is not blank, try to use it as a filename // FileFlag does some nice ~-substitution which is why we use it rather than the infinitely more normal-looking ioutil.ReadFile - publicKeyFile = flags.FileFlag{FileName: key} + publicKeyFile := flags.FileFlag{FileName: key} if err := publicKeyFile.Set(key); err == nil { key = publicKeyFile.Value } From b94566a2e862309b277afd27ab85d4ea7ee97d0b Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 11:22:53 +0000 Subject: [PATCH 07/23] add documentation comment for AccountName --- cmd/bytemark/app/flags/accessors.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/bytemark/app/flags/accessors.go b/cmd/bytemark/app/flags/accessors.go index 97447c36..736ed0f3 100644 --- a/cmd/bytemark/app/flags/accessors.go +++ b/cmd/bytemark/app/flags/accessors.go @@ -8,6 +8,8 @@ import ( "github.com/BytemarkHosting/bytemark-client/lib/brain" ) +// AccountName returns the named AccountNameFlag. Why does it return the +// flag and not simply the account name? I don't know func AccountName(c *app.Context, flagname string) AccountNameFlag { accountName, ok := c.Context.Generic(flagname).(*AccountNameFlag) if ok { From 65aaf711992a19ca27d759ef0e23dae25fc8df56 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 12:55:06 +0000 Subject: [PATCH 08/23] fix PrivilegeFlag documentation coment --- cmd/bytemark/app/flags/privilege.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bytemark/app/flags/privilege.go b/cmd/bytemark/app/flags/privilege.go index 0e1affca..d0967553 100644 --- a/cmd/bytemark/app/flags/privilege.go +++ b/cmd/bytemark/app/flags/privilege.go @@ -25,7 +25,7 @@ func (args *privArgs) shift() (arg string, err error) { return } -// Privilege is an un-realised brain.Privilege - where the target name has been parsed but hasn't been turned into IDs yet +// PrivilegeFlag is an un-realised brain.Privilege - where the target name has been parsed but hasn't been turned into IDs yet type PrivilegeFlag struct { AccountName string GroupName *lib.GroupName From 6c366d0e2181b634bf0af3eb87b2ce96a31c17c2 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 15:56:40 +0000 Subject: [PATCH 09/23] bump go to version 1.11 --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5a290909..ec439a05 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ variables: GO15VENDOREXPERIMENT: "1" GOPATH: /go - GOVERSION: "1.8" + GOVERSION: "1.11" stages: - test From 90a085361d481289edb291d2f8f8c43c0ebf773d Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:01:55 +0000 Subject: [PATCH 10/23] prevent some variable shadowing --- cmd/bytemark/add.go | 2 +- cmd/bytemark/add_test.go | 8 ++++---- cmd/bytemark/debug.go | 2 +- cmd/bytemark/show.go | 7 ++++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/bytemark/add.go b/cmd/bytemark/add.go index 476ff642..6c977990 100644 --- a/cmd/bytemark/add.go +++ b/cmd/bytemark/add.go @@ -58,7 +58,7 @@ func init() { // if public-key is not blank, try to use it as a filename // FileFlag does some nice ~-substitution which is why we use it rather than the infinitely more normal-looking ioutil.ReadFile publicKeyFile := flags.FileFlag{FileName: key} - if err := publicKeyFile.Set(key); err == nil { + if err = publicKeyFile.Set(key); err == nil { key = publicKeyFile.Value } } diff --git a/cmd/bytemark/add_test.go b/cmd/bytemark/add_test.go index c6c50d58..261bfad9 100644 --- a/cmd/bytemark/add_test.go +++ b/cmd/bytemark/add_test.go @@ -45,7 +45,7 @@ func TestAddKeyCommand(t *testing.T) { Username: "test-user", AuthorizedKeys: brain.Keys{brain.Key{Key: "ssh-rsa aaaaawhartevervAsde fake key"}}, }) - if ok, err := c.Verify(); !ok { + if ok, err = c.Verify(); !ok { t.Fatal(err) } @@ -71,7 +71,7 @@ func TestAddKeyCommand(t *testing.T) { Username: "test-user", AuthorizedKeys: brain.Keys{brain.Key{Key: "ssh-rsa aaaaawhartevervAsde fake key"}}, }) - if ok, err := c.Verify(); !ok { + if ok, err = c.Verify(); !ok { t.Fatal(err) } @@ -97,8 +97,8 @@ func TestAddKeyCommand(t *testing.T) { Username: "test-user", AuthorizedKeys: brain.Keys{brain.Key{Key: "ssh-rsa aaaaawhartevervAsde fake key"}}, }) - if ok, err := c.Verify(); !ok { - t.Fatal(err) + if ok, vErr := c.Verify(); !ok { + t.Fatal(vErr) } }) diff --git a/cmd/bytemark/debug.go b/cmd/bytemark/debug.go index c659942c..b7004e89 100644 --- a/cmd/bytemark/debug.go +++ b/cmd/bytemark/debug.go @@ -63,7 +63,7 @@ The rest do similar, but PUT and POST both wait for input from stdin after authe url = "/" + url } if c.Bool("auth") { - err := with.Auth(c) + err = with.Auth(c) if err != nil { return err } diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index 96187c2a..f45f3cb7 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -147,8 +147,9 @@ Privileges will be output in no particular order.`, server := flags.VirtualMachineName(c, "server") privs := make(brain.Privileges, 0) + newPrivs := make(brain.Privileges, 0) if account != "" { - newPrivs, err := findPrivilegesForAccount(c, account, c.Bool("recursive")) + newPrivs, err = findPrivilegesForAccount(c, account, c.Bool("recursive")) if err != nil { return err } @@ -156,7 +157,7 @@ Privileges will be output in no particular order.`, } if group.Group != "" { - newPrivs, err := findPrivilegesForGroup(c, group, c.Bool("recursive")) + newPrivs, err = findPrivilegesForGroup(c, group, c.Bool("recursive")) if err != nil { return err } @@ -164,7 +165,7 @@ Privileges will be output in no particular order.`, } if server.VirtualMachine != "" { - newPrivs, err := c.Client().GetPrivilegesForVirtualMachine(server) + newPrivs, err = c.Client().GetPrivilegesForVirtualMachine(server) if err != nil { return err } From 1015859151b0f6e24479c73186fc883433575330 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:05:01 +0000 Subject: [PATCH 11/23] fix govet issues - CliContext was being passed by-value to functions defined on it, making it unhappy because mock.Mock uses sync.Mutex --- cmd/bytemark/add_test.go | 8 ++--- mocks/context.go | 76 ++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/cmd/bytemark/add_test.go b/cmd/bytemark/add_test.go index 261bfad9..39908eb0 100644 --- a/cmd/bytemark/add_test.go +++ b/cmd/bytemark/add_test.go @@ -45,8 +45,8 @@ func TestAddKeyCommand(t *testing.T) { Username: "test-user", AuthorizedKeys: brain.Keys{brain.Key{Key: "ssh-rsa aaaaawhartevervAsde fake key"}}, }) - if ok, err = c.Verify(); !ok { - t.Fatal(err) + if ok, vErr := c.Verify(); !ok { + t.Fatal(vErr) } }) @@ -71,8 +71,8 @@ func TestAddKeyCommand(t *testing.T) { Username: "test-user", AuthorizedKeys: brain.Keys{brain.Key{Key: "ssh-rsa aaaaawhartevervAsde fake key"}}, }) - if ok, err = c.Verify(); !ok { - t.Fatal(err) + if ok, vErr := c.Verify(); !ok { + t.Fatal(vErr) } }) diff --git a/mocks/context.go b/mocks/context.go index 67b7c465..83ad8c9c 100644 --- a/mocks/context.go +++ b/mocks/context.go @@ -11,156 +11,156 @@ type CliContext struct { mock.Mock } -func (c CliContext) Args() cli.Args { +func (c *CliContext) Args() cli.Args { r := c.Called() return r.Get(0).(cli.Args) } -func (c CliContext) Bool(name string) bool { +func (c *CliContext) Bool(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) BoolT(name string) bool { +func (c *CliContext) BoolT(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) Duration(name string) time.Duration { +func (c *CliContext) Duration(name string) time.Duration { r := c.Called(name) return r.Get(0).(time.Duration) } -func (c CliContext) FlagNames() (names []string) { +func (c *CliContext) FlagNames() (names []string) { r := c.Called() return r.Get(0).([]string) } -func (c CliContext) Float64(name string) float64 { +func (c *CliContext) Float64(name string) float64 { r := c.Called(name) return r.Float64(0) } -func (c CliContext) Generic(name string) interface{} { +func (c *CliContext) Generic(name string) interface{} { r := c.Called(name) return r.Get(0) } -func (c CliContext) GlobalBool(name string) bool { +func (c *CliContext) GlobalBool(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) GlobalBoolT(name string) bool { +func (c *CliContext) GlobalBoolT(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) GlobalDuration(name string) time.Duration { +func (c *CliContext) GlobalDuration(name string) time.Duration { r := c.Called(name) return r.Get(0).(time.Duration) } -func (c CliContext) GlobalFlagNames() (names []string) { +func (c *CliContext) GlobalFlagNames() (names []string) { r := c.Called() return r.Get(0).([]string) } -func (c CliContext) GlobalFloat64(name string) float64 { +func (c *CliContext) GlobalFloat64(name string) float64 { r := c.Called(name) return r.Float64(0) } -func (c CliContext) GlobalGeneric(name string) interface{} { +func (c *CliContext) GlobalGeneric(name string) interface{} { r := c.Called(name) return r.Get(0) } -func (c CliContext) GlobalInt(name string) int { +func (c *CliContext) GlobalInt(name string) int { r := c.Called(name) return r.Int(0) } -func (c CliContext) GlobalInt64(name string) int64 { +func (c *CliContext) GlobalInt64(name string) int64 { r := c.Called(name) return r.Int64(0) } -func (c CliContext) GlobalInt64Slice(name string) []int64 { +func (c *CliContext) GlobalInt64Slice(name string) []int64 { r := c.Called(name) return r.Get(0).([]int64) } -func (c CliContext) GlobalIntSlice(name string) []int { +func (c *CliContext) GlobalIntSlice(name string) []int { r := c.Called(name) return r.Get(0).([]int) } -func (c CliContext) GlobalIsSet(name string) bool { +func (c *CliContext) GlobalIsSet(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) GlobalSet(name, value string) error { +func (c *CliContext) GlobalSet(name, value string) error { r := c.Called(name, value) return r.Error(0) } -func (c CliContext) GlobalString(name string) string { +func (c *CliContext) GlobalString(name string) string { r := c.Called(name) return r.String(0) } -func (c CliContext) GlobalStringSlice(name string) []string { +func (c *CliContext) GlobalStringSlice(name string) []string { r := c.Called(name) return r.Get(0).([]string) } -func (c CliContext) GlobalUint(name string) uint { +func (c *CliContext) GlobalUint(name string) uint { r := c.Called(name) return r.Get(0).(uint) } -func (c CliContext) GlobalUint64(name string) uint64 { +func (c *CliContext) GlobalUint64(name string) uint64 { r := c.Called(name) return r.Get(0).(uint64) } -func (c CliContext) Int(name string) int { +func (c *CliContext) Int(name string) int { r := c.Called(name) return r.Int(0) } -func (c CliContext) Int64(name string) int64 { +func (c *CliContext) Int64(name string) int64 { r := c.Called(name) return r.Int64(0) } -func (c CliContext) Int64Slice(name string) []int64 { +func (c *CliContext) Int64Slice(name string) []int64 { r := c.Called(name) return r.Get(0).([]int64) } -func (c CliContext) IntSlice(name string) []int { +func (c *CliContext) IntSlice(name string) []int { r := c.Called(name) return r.Get(0).([]int) } -func (c CliContext) IsSet(name string) bool { +func (c *CliContext) IsSet(name string) bool { r := c.Called(name) return r.Bool(0) } -func (c CliContext) NArg() int { +func (c *CliContext) NArg() int { r := c.Called() return r.Int(0) } -func (c CliContext) NumFlags() int { +func (c *CliContext) NumFlags() int { r := c.Called() return r.Int(0) } -func (c CliContext) Parent() *cli.Context { +func (c *CliContext) Parent() *cli.Context { r := c.Called() return r.Get(0).(*cli.Context) } -func (c CliContext) Set(name, value string) error { +func (c *CliContext) Set(name, value string) error { r := c.Called(name, value) return r.Error(0) } -func (c CliContext) String(name string) string { +func (c *CliContext) String(name string) string { r := c.Called(name) return r.String(0) } -func (c CliContext) StringSlice(name string) []string { +func (c *CliContext) StringSlice(name string) []string { r := c.Called(name) return r.Get(0).([]string) } -func (c CliContext) Uint(name string) uint { +func (c *CliContext) Uint(name string) uint { r := c.Called(name) return r.Get(0).(uint) } -func (c CliContext) Uint64(name string) uint64 { +func (c *CliContext) Uint64(name string) uint64 { r := c.Called(name) return r.Get(0).(uint64) } -func (c CliContext) App() *cli.App { +func (c *CliContext) App() *cli.App { r := c.Called() return r.Get(0).(*cli.App) } -func (c CliContext) Command() cli.Command { +func (c *CliContext) Command() cli.Command { r := c.Called() return r.Get(0).(cli.Command) } From e6838f0c5d8c6440a668eaaec451cec49a3ded21 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:07:10 +0000 Subject: [PATCH 12/23] don't assign newPrivs to start with --- cmd/bytemark/show.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index f45f3cb7..857871b7 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -147,7 +147,7 @@ Privileges will be output in no particular order.`, server := flags.VirtualMachineName(c, "server") privs := make(brain.Privileges, 0) - newPrivs := make(brain.Privileges, 0) + var newPrivs []brain.Privileges if account != "" { newPrivs, err = findPrivilegesForAccount(c, account, c.Bool("recursive")) if err != nil { From 9e580d25d2ed7522ffbcd0e71ce15afa813b9d51 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:07:48 +0000 Subject: [PATCH 13/23] pass Prompter to its methods by-reference --- mocks/prompter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mocks/prompter.go b/mocks/prompter.go index c423296a..7331f6a5 100644 --- a/mocks/prompter.go +++ b/mocks/prompter.go @@ -9,12 +9,12 @@ type Prompter struct { mock.Mock } -func (tp Prompter) Prompt(prompt string) (response string) { +func (tp *Prompter) Prompt(prompt string) (response string) { r := tp.Called(prompt) return r.String(0) } -func (tp Prompter) Ask(prompt string) (password string, err error) { +func (tp *Prompter) Ask(prompt string) (password string, err error) { r := tp.Called(prompt) return r.String(0), r.Error(1) } From f1c3b00363b0ff87d1373eb3140e330d49a17a64 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:08:22 +0000 Subject: [PATCH 14/23] fix space in LastNote struct tag in Head --- lib/brain/head.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/brain/head.go b/lib/brain/head.go index c7617f65..67a49035 100644 --- a/lib/brain/head.go +++ b/lib/brain/head.go @@ -19,7 +19,7 @@ type Head struct { Architecture string `json:"arch,omitempty"` CCAddress *net.IP `json:"cnc_address,omitempty"` - LastNote string `json:"last_note, omitempty"` + LastNote string `json:"last_note,omitempty"` TotalMemory int `json:"total_memory,omitempty"` UsageStrategy string `json:"usage_strategy,omitempty"` Models []string `json:"models,omitempty"` From 9bdcffc9557c9700cbd5ad65d2f2b28078758d29 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:09:49 +0000 Subject: [PATCH 15/23] fix newPrivs type --- cmd/bytemark/show.go | 2 +- lib/requests/brain/add_user_authorized_key.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/bytemark/show.go b/cmd/bytemark/show.go index 857871b7..b1c61e4c 100644 --- a/cmd/bytemark/show.go +++ b/cmd/bytemark/show.go @@ -147,7 +147,7 @@ Privileges will be output in no particular order.`, server := flags.VirtualMachineName(c, "server") privs := make(brain.Privileges, 0) - var newPrivs []brain.Privileges + var newPrivs brain.Privileges if account != "" { newPrivs, err = findPrivilegesForAccount(c, account, c.Bool("recursive")) if err != nil { diff --git a/lib/requests/brain/add_user_authorized_key.go b/lib/requests/brain/add_user_authorized_key.go index 5863b0f4..a2bcde9c 100644 --- a/lib/requests/brain/add_user_authorized_key.go +++ b/lib/requests/brain/add_user_authorized_key.go @@ -14,7 +14,7 @@ func AddUserAuthorizedKey(client lib.Client, username string, key string) error return err } key = strings.TrimSpace(key) - user.AuthorizedKeys = append(user.AuthorizedKeys, brain.Key{key}) + user.AuthorizedKeys = append(user.AuthorizedKeys, brain.Key{Key: key}) r, err := client.BuildRequest("PUT", lib.BrainEndpoint, "/users/%s", username) if err != nil { From a774a899b59f0f348b2cb0a8320aafa4b5b34fc7 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:12:18 +0000 Subject: [PATCH 16/23] lessen usage of unkeyed fields --- cmd/bytemark/commands/update/config_test.go | 6 +++--- cmd/bytemark/schedule_test.go | 6 +++--- cmd/bytemark/unschedule_test.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/bytemark/commands/update/config_test.go b/cmd/bytemark/commands/update/config_test.go index 1602966d..f909e8de 100644 --- a/cmd/bytemark/commands/update/config_test.go +++ b/cmd/bytemark/commands/update/config_test.go @@ -22,7 +22,7 @@ func TestUpdateConfig(t *testing.T) { name: "SetUser", args: "--user fred", expectation: func(config *mocks.Config, _ *mocks.Client) { - before := cf.Var{"user", "joan", ""} + before := cf.Var{Name: "user", Value: "joan", Source: ""} config.When("GetV", "user").Return(before).Times(1) config.When("SetPersistent", "user", "fred", "CMD set") }, @@ -38,7 +38,7 @@ func TestUpdateConfig(t *testing.T) { name: "SetAccount", args: "--account smythe", expectation: func(config *mocks.Config, client *mocks.Client) { - before := cf.Var{"account", "not-smythe", ""} + before := cf.Var{Name: "account", Value: "not-smythe", Source: ""} smythe := lib.Account{} config.When("GetV", "account").Return(before).Times(1) config.When("GetIgnoreErr", "account").Return(before.Value).Times(1) @@ -50,7 +50,7 @@ func TestUpdateConfig(t *testing.T) { name: "SetAccountNoBilling", args: "--account smythe", expectation: func(config *mocks.Config, client *mocks.Client) { - before := cf.Var{"account", "not-smythe", ""} + before := cf.Var{Name: "account", Value: "not-smythe", Source: ""} smythe := lib.Account{} config.When("GetV", "account").Return(before).Times(1) config.When("GetIgnoreErr", "account").Return(before.Value).Times(1) diff --git a/cmd/bytemark/schedule_test.go b/cmd/bytemark/schedule_test.go index 82c0813f..5fd3fc34 100644 --- a/cmd/bytemark/schedule_test.go +++ b/cmd/bytemark/schedule_test.go @@ -40,7 +40,7 @@ func TestScheduleBackups(t *testing.T) { }, { Args: []string{"vm-name", "disc-label"}, - Name: lib.VirtualMachineName{"vm-name", "default", "default-account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "default", Account: "default-account"}, DiscLabel: "disc-label", Start: "00:00", Interval: 86400, @@ -51,7 +51,7 @@ func TestScheduleBackups(t *testing.T) { { ShouldCall: true, Args: []string{"vm-name.group.account", "disc-label", "3600"}, - Name: lib.VirtualMachineName{"vm-name", "group", "account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "group", Account: "account"}, DiscLabel: "disc-label", Start: "00:00", Interval: 3600, @@ -59,7 +59,7 @@ func TestScheduleBackups(t *testing.T) { }, { Args: []string{"--start", "thursday", "vm-name", "disc-label", "3235"}, - Name: lib.VirtualMachineName{"vm-name", "default", "default-account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "default", Account: "default-account"}, DiscLabel: "disc-label", Start: "thursday", Interval: 3235, diff --git a/cmd/bytemark/unschedule_test.go b/cmd/bytemark/unschedule_test.go index 94c99913..151b5471 100644 --- a/cmd/bytemark/unschedule_test.go +++ b/cmd/bytemark/unschedule_test.go @@ -31,14 +31,14 @@ func TestUnscheduleBackups(t *testing.T) { }, { Args: []string{"vm-name"}, - Name: lib.VirtualMachineName{"vm-name", "default", "default-account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "default", Account: "default-account"}, ShouldCall: false, ShouldErr: true, BaseTestFn: testutil.BaseTestSetup, }, { Args: []string{"vm-name", "disc-label"}, - Name: lib.VirtualMachineName{"vm-name", "default", "default-account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "default", Account: "default-account"}, ShouldCall: false, ShouldErr: true, BaseTestFn: testutil.BaseTestSetup, @@ -46,7 +46,7 @@ func TestUnscheduleBackups(t *testing.T) { { ShouldCall: true, Args: []string{"vm-name", "disc-label", "324"}, - Name: lib.VirtualMachineName{"vm-name", "default", "default-account"}, + Name: lib.VirtualMachineName{VirtualMachine: "vm-name", Group: "default", Account: "default-account"}, DiscLabel: "disc-label", ID: 324, BaseTestFn: testutil.BaseTestAuthSetup, From 5b6c0c44f07cbb151922ade4ba82a27708c68dcd Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:22:54 +0000 Subject: [PATCH 17/23] fix non-pointer usage of Prompter --- cmd/bytemark/app/auth/authenticator_test.go | 4 ++-- cmd/bytemark/commands/delete/group_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/bytemark/app/auth/authenticator_test.go b/cmd/bytemark/app/auth/authenticator_test.go index 715768db..137319a0 100644 --- a/cmd/bytemark/app/auth/authenticator_test.go +++ b/cmd/bytemark/app/auth/authenticator_test.go @@ -1220,8 +1220,8 @@ func TestAuthenticate(t *testing.T) { err := Authenticator{ client: client, config: config, - prompter: prompter, - passPrompter: prompter, + prompter: &prompter, + passPrompter: &prompter, }.Authenticate() if test.expectingError && err == nil { t.Error("Expecting Authenticate to error, but it didn't") diff --git a/cmd/bytemark/commands/delete/group_test.go b/cmd/bytemark/commands/delete/group_test.go index 9daf14c3..208162c6 100644 --- a/cmd/bytemark/commands/delete/group_test.go +++ b/cmd/bytemark/commands/delete/group_test.go @@ -104,7 +104,7 @@ func TestDeleteGroup(t *testing.T) { } config, client, app := testutil.BaseTestAuthSetup(t, false, commands.Commands) - appPkg.SetPrompter(app, testPrompter) + appPkg.SetPrompter(app, &testPrompter) group := brain.Group{ Name: "test-group", From 79dcad580526ce8853c83f138bd0884b5b52a2ed Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:26:56 +0000 Subject: [PATCH 18/23] use keyed fields for config.Vars --- cmd/bytemark/app/base_test.go | 2 +- cmd/bytemark/app/context_test.go | 14 +++++++------- cmd/bytemark/show_test.go | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/bytemark/app/base_test.go b/cmd/bytemark/app/base_test.go index 345007ff..f62b146f 100644 --- a/cmd/bytemark/app/base_test.go +++ b/cmd/bytemark/app/base_test.go @@ -14,7 +14,7 @@ func baseTestSetup(t *testing.T, admin bool, commands []cli.Command) (conf *mock conf = new(mocks.Config) client = new(mocks.Client) conf.When("GetBool", "admin").Return(admin, nil) - conf.When("GetV", "output-format").Return(config.Var{"output-format", "human", "CODE"}) + conf.When("GetV", "output-format").Return(config.Var{Name: "output-format", Value: "human", Source: "CODE"}) app, err := BaseAppSetup(GlobalFlags(), commands) if err != nil { diff --git a/cmd/bytemark/app/context_test.go b/cmd/bytemark/app/context_test.go index 5a8c238f..a2f52959 100644 --- a/cmd/bytemark/app/context_test.go +++ b/cmd/bytemark/app/context_test.go @@ -25,7 +25,7 @@ func TestOutput(t *testing.T) { }{ { // 0 // default to human output - ConfigFormat: config.Var{"output-format", "human", "CODE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "human", Source: "CODE"}, Object: brain.Disc{ Label: "disk-1", StorageGrade: "sata", @@ -35,13 +35,13 @@ func TestOutput(t *testing.T) { Expected: "disk-1 - 25GiB, sata grade\n", }, { // 1 // default to human output with an error - ConfigFormat: config.Var{"output-format", "human", "CODE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "human", Source: "CODE"}, Object: nil, Expected: "", ShouldErr: true, }, { // 2 // when there's a default format specific to the command, use that instead of the uber-default - ConfigFormat: config.Var{"output-format", "human", "CODE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "human", Source: "CODE"}, DefaultFormat: []output.Format{output.Table}, Object: brain.Disc{ StorageGrade: "sata", @@ -52,7 +52,7 @@ func TestOutput(t *testing.T) { Expected: "+-----+\n| ID |\n+-----+\n| 123 |\n+-----+\n", }, { // 3 // except when the JSON flag is set, then output JSON - ConfigFormat: config.Var{"output-format", "human", "CODE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "human", Source: "CODE"}, DefaultFormat: []output.Format{output.Table}, JSONFlag: true, Object: brain.Group{ @@ -62,7 +62,7 @@ func TestOutput(t *testing.T) { Expected: "{\n \"name\": \"my-cool-group\",\n \"account_id\": 0,\n \"id\": 11323,\n \"virtual_machines\": null\n}\n", }, { // 4 // or if output-format is set by a FILE - ConfigFormat: config.Var{"output-format", "json", "FILE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "json", Source: "FILE"}, DefaultFormat: []output.Format{output.Table}, Object: brain.Group{ Name: "my-cool-group", @@ -71,7 +71,7 @@ func TestOutput(t *testing.T) { Expected: "{\n \"name\": \"my-cool-group\",\n \"account_id\": 0,\n \"id\": 11323,\n \"virtual_machines\": null\n}\n", // but the table and json flags should have precedence in every situation }, { // 5 - ConfigFormat: config.Var{"output-format", "json", "FILE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "json", Source: "FILE"}, DefaultFormat: []output.Format{output.Human}, TableFlag: true, Object: brain.Group{ @@ -81,7 +81,7 @@ func TestOutput(t *testing.T) { Expected: "+---------------+-----------------+\n| Name | VirtualMachines |\n+---------------+-----------------+\n| my-cool-group | |\n+---------------+-----------------+\n", // also, --table-fields being non-empty should imply --table and be case insensitive }, { // 6 - ConfigFormat: config.Var{"output-format", "json", "FILE"}, + ConfigFormat: config.Var{Name: "output-format", Value: "json", Source: "FILE"}, DefaultFormat: []output.Format{output.Human}, TableFlag: false, TableFields: "AccountID,ID,Name,VirtualMachines", diff --git a/cmd/bytemark/show_test.go b/cmd/bytemark/show_test.go index 15804ca0..8035f3ca 100644 --- a/cmd/bytemark/show_test.go +++ b/cmd/bytemark/show_test.go @@ -49,7 +49,7 @@ var showAccountTestOutput = map[string]string{ func TestShowAccount(t *testing.T) { baseShowAccountSetup := func(c *mocks.Client, conf *mocks.Config, configAccount, outputFormat string) { conf.Mock.Functions = resetOneMockedFunction(conf.Mock.Functions, "GetV", "output-format") - conf.When("GetV", "output-format").Return(config.Var{"output-format", outputFormat, "FLAG output-format"}) + conf.When("GetV", "output-format").Return(config.Var{Name: "output-format", Value: outputFormat, Source: "FLAG output-format"}) conf.When("GetIgnoreErr", "account").Return(configAccount) } From 10331d305a6d6d912e15acfcb0da115f2313642f Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:29:39 +0000 Subject: [PATCH 19/23] use keyed fields for GroupName --- cmd/bytemark/commands/show/servers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bytemark/commands/show/servers_test.go b/cmd/bytemark/commands/show/servers_test.go index 2b80831e..8d8c598d 100644 --- a/cmd/bytemark/commands/show/servers_test.go +++ b/cmd/bytemark/commands/show/servers_test.go @@ -42,7 +42,7 @@ func TestShowServers(t *testing.T) { config.When("GetIgnoreErr", "account").Return("spokny-stevn") config.When("GetGroup").Return(testutil.DefGroup) - c.When("GetGroup", lib.GroupName{"ghosts", "spooky-steve"}).Return(brain.Group{ + c.When("GetGroup", lib.GroupName{Group: "ghosts", Account: "spooky-steve"}).Return(brain.Group{ Name: "default", VirtualMachines: []brain.VirtualMachine{ {ID: 1, Name: "old-man-crumbles"}, From b9bef2dd37be19ef172d99b7f6ac79f64825b587 Mon Sep 17 00:00:00 2001 From: telyn Date: Thu, 6 Dec 2018 16:32:20 +0000 Subject: [PATCH 20/23] Fix the last unkeyed struct --- cmd/bytemark/testutil/base.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bytemark/testutil/base.go b/cmd/bytemark/testutil/base.go index 73c67d4e..8489b690 100644 --- a/cmd/bytemark/testutil/base.go +++ b/cmd/bytemark/testutil/base.go @@ -65,7 +65,7 @@ func BaseTestSetup(t *testing.T, admin bool, commands []cli.Command) (conf *mock conf = new(mocks.Config) client = new(mocks.Client) conf.When("GetBool", "admin").Return(admin, nil) - conf.When("GetV", "output-format").Return(config.Var{"output-format", "human", "CODE"}) + conf.When("GetV", "output-format").Return(config.Var{Name: "output-format", Value: "human", Source: "CODE"}) cliapp, err := app.BaseAppSetup(app.GlobalFlags(), commands) if err != nil { From 218b2129e366927d54c07718f57574eded21086a Mon Sep 17 00:00:00 2001 From: telyn Date: Fri, 7 Dec 2018 10:35:20 +0000 Subject: [PATCH 21/23] remove Preprocessor interface from flags package - it's of no use there --- cmd/bytemark/app/flags/preprocessor.go | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 cmd/bytemark/app/flags/preprocessor.go diff --git a/cmd/bytemark/app/flags/preprocessor.go b/cmd/bytemark/app/flags/preprocessor.go deleted file mode 100644 index 2e0e7acb..00000000 --- a/cmd/bytemark/app/flags/preprocessor.go +++ /dev/null @@ -1,8 +0,0 @@ -package flags - -import "github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" - -// A Preprocesser is a Flag that has a preprocess step that requires a Context -type Preprocesser interface { - Preprocess(c *app.Context) error -} From 3f03a55d1d3965891fe1d9f89825ae28ee82dab9 Mon Sep 17 00:00:00 2001 From: telyn Date: Fri, 7 Dec 2018 10:39:28 +0000 Subject: [PATCH 22/23] mention app.Preprocessor in implementations of it --- cmd/bytemark/app/flags/account_name.go | 3 ++- cmd/bytemark/app/flags/group_name.go | 3 +++ cmd/bytemark/app/flags/privilege.go | 2 ++ cmd/bytemark/app/flags/virtual_machine_name.go | 2 ++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/bytemark/app/flags/account_name.go b/cmd/bytemark/app/flags/account_name.go index 5f468af1..7f2b43cc 100644 --- a/cmd/bytemark/app/flags/account_name.go +++ b/cmd/bytemark/app/flags/account_name.go @@ -20,7 +20,8 @@ func (name *AccountNameFlag) Set(value string) error { } // Preprocess sets the value of this flag to the global account flag if it's unset, -// and then runs lib.ParseAccountName +// and then runs lib.ParseAccountName. This is an implementation of `app.Preprocessor`, +// which is detected and called automatically by actions created with `app.Action` func (name *AccountNameFlag) Preprocess(c *app.Context) (err error) { if name.Value == "" { name.Value = c.Context.GlobalString("account") diff --git a/cmd/bytemark/app/flags/group_name.go b/cmd/bytemark/app/flags/group_name.go index 9f63d82c..5acfb766 100644 --- a/cmd/bytemark/app/flags/group_name.go +++ b/cmd/bytemark/app/flags/group_name.go @@ -19,6 +19,9 @@ func (name *GroupNameFlag) Set(value string) error { // Preprocess defaults the value of this flag to the default group from the // config attached to the context and then runs lib.ParseGroupName +// This is an implementation of `app.Preprocessor`, which is detected and +// called automatically by actions created with `app.Action` + func (name *GroupNameFlag) Preprocess(c *app.Context) (err error) { if name.GroupName != nil { c.Debug("GroupName.Preprocess before %#v", *name.GroupName) diff --git a/cmd/bytemark/app/flags/privilege.go b/cmd/bytemark/app/flags/privilege.go index d0967553..41ff51ba 100644 --- a/cmd/bytemark/app/flags/privilege.go +++ b/cmd/bytemark/app/flags/privilege.go @@ -78,6 +78,8 @@ func (pf *PrivilegeFlag) Set(value string) (err error) { // Preprocess parses the Privilege and looks up the target's ID so it can // be made into a brain.Privilege +// This is an implementation of `app.Preprocessor`, which is detected and +// called automatically by actions created with `app.Action` func (pf *PrivilegeFlag) Preprocess(c *app.Context) (err error) { args := privArgs(strings.Split(pf.Value, " ")) diff --git a/cmd/bytemark/app/flags/virtual_machine_name.go b/cmd/bytemark/app/flags/virtual_machine_name.go index a2d07d28..68ee804f 100644 --- a/cmd/bytemark/app/flags/virtual_machine_name.go +++ b/cmd/bytemark/app/flags/virtual_machine_name.go @@ -19,6 +19,8 @@ func (name *VirtualMachineNameFlag) Set(value string) error { // Preprocess defaults the value of this flag to the default server from the // config attached to the context and then runs lib.ParseVirtualMachineName +// This is an implementation of `app.Preprocessor`, which is detected and +// called automatically by actions created with `app.Action` func (name *VirtualMachineNameFlag) Preprocess(c *app.Context) (err error) { if name.Value == "" { return From e3ddc671f417574df5749668807f744110ded780 Mon Sep 17 00:00:00 2001 From: telyn Date: Fri, 7 Dec 2018 12:56:37 +0000 Subject: [PATCH 23/23] remove accidental newline --- cmd/bytemark/app/flags/group_name.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/bytemark/app/flags/group_name.go b/cmd/bytemark/app/flags/group_name.go index 5acfb766..662a9f71 100644 --- a/cmd/bytemark/app/flags/group_name.go +++ b/cmd/bytemark/app/flags/group_name.go @@ -21,7 +21,6 @@ func (name *GroupNameFlag) Set(value string) error { // config attached to the context and then runs lib.ParseGroupName // This is an implementation of `app.Preprocessor`, which is detected and // called automatically by actions created with `app.Action` - func (name *GroupNameFlag) Preprocess(c *app.Context) (err error) { if name.GroupName != nil { c.Debug("GroupName.Preprocess before %#v", *name.GroupName)