-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'slice-flags' into 'develop'
Implement generated slice flags for VirtualMachineName, GroupName and AccountName See merge request open-source/bytemark-client!302
- Loading branch information
Showing
20 changed files
with
681 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package flags | ||
|
||
// This file was automatically generate using | ||
// cmd/bytemark/app/flags/gen/slice_flags - do not edit it by hand! | ||
// To make your own SliceFlag, see the documentation in | ||
// cmd/bytemark/app/flags/gen/slice_flags/slice_flags.go | ||
// and edit cmd/bytemark/app/flags/slice_flags.go | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" | ||
) | ||
|
||
// AccountNameSliceFlag is used for AccountNameFlags that may be specified more than | ||
// once. It's a slice of AccountNameFlag in order to avoid rewriting parsing | ||
// logic. | ||
type AccountNameSliceFlag []AccountNameFlag | ||
|
||
// Preprocess calls Preprocess on all the underlying AccountNameFlags | ||
func (sf *AccountNameSliceFlag) Preprocess(ctx *app.Context) error { | ||
for i := range *sf { | ||
err := (*sf)[i].Preprocess(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Set appends a AccountNameFlag (created for you) to the slice | ||
func (sf *AccountNameSliceFlag) Set(value string) error { | ||
flag := AccountNameFlag{} | ||
err := flag.Set(value) | ||
if err != nil { | ||
return err | ||
} | ||
*sf = append(*sf, flag) | ||
return nil | ||
} | ||
|
||
// String returns all values in the slice, comma-delimeted | ||
func (sf AccountNameSliceFlag) String() string { | ||
strs := make([]string, len(sf)) | ||
for i, value := range sf { | ||
strs[i] = value.String() | ||
} | ||
return strings.Join(strs, ", ") | ||
} | ||
|
||
// AccountNameSlice returns the named flag as a AccountNameSliceFlag, | ||
// if it was one in the first place. | ||
func AccountNameSlice(ctx *app.Context, name string) AccountNameSliceFlag { | ||
if sf, ok := ctx.Context.Generic(name).(*AccountNameSliceFlag); ok { | ||
return *sf | ||
} | ||
return AccountNameSliceFlag{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package flags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/flags" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/testutil" | ||
"github.com/BytemarkHosting/bytemark-client/lib" | ||
"github.com/BytemarkHosting/bytemark-client/mocks" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func TestAccountNameSliceFlag(t *testing.T) { | ||
sf := flags.AccountNameSliceFlag{} | ||
err := sf.Set("photocopier") | ||
if err != nil { | ||
t.Errorf("got error from Set(): %s", err) | ||
} | ||
if len(sf) != 1 { | ||
t.Errorf("Expected len(AccountNameSliceFLag) to be 1, got %d", | ||
len(sf)) | ||
} | ||
|
||
t.Logf("Value: %s", sf[0].Value) | ||
// it's a Preprocesser so we need to call Preprocess before we can validate | ||
// String() | ||
cfg, client, cliApp := testutil.BaseTestSetup(t, false, []cli.Command{}) | ||
cfg.When("GetIgnoreErr", "account").Return("default-account") | ||
cfg.When("GetGroup").Return(lib.GroupName{Group: "default-group", Account: "default-account"}) | ||
cfg.When("GetVirtualMachine").Return(lib.VirtualMachineName{VirtualMachine: "default-server", Group: "default-group", Account: "default-account"}) | ||
|
||
// now some boilerplate to get a context | ||
// TODO(telyn): this should probably be refactored out since it'll be | ||
// wanted for basically every Preprocesser flag) | ||
client.When("AuthWithToken", "test-token").Return(nil) | ||
cliCtx := mocks.CliContext{} | ||
cliCtx.When("App").Return(cliApp) | ||
ctx := app.Context{ | ||
Context: &cliCtx, | ||
} | ||
|
||
// with a context we may now Preprocess | ||
err = sf.Preprocess(&ctx) | ||
if err != nil { | ||
t.Errorf("Preprocess errored: %s", err) | ||
} | ||
|
||
if sf.String() != "photocopier" { | ||
t.Errorf("Expected %q, got %q", "photocopier", sf.String()) | ||
} | ||
|
||
} |
Oops, something went wrong.