Skip to content

Commit

Permalink
Finish making sure Context is named 'ctx' as receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
telyn committed Jan 24, 2019
1 parent 81cd1c2 commit 06c0233
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions cmd/bytemark/app/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
Preprocess(ctx *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
Expand All @@ -28,32 +28,32 @@ func Action(providers ...ProviderFunc) func(c *cli.Context) error {
}

// Preprocess runs the Preprocess methods on all flags that implement Preprocessor
func (c *Context) Preprocess() error {
if c.preprocessHasRun {
func (ctx *Context) Preprocess() error {
if ctx.preprocessHasRun {
return nil
}
c.Debug("Preprocessing")
for _, flag := range c.Command().Flags {
ctx.Debug("Preprocessing")
for _, flag := range ctx.Command().Flags {
if gf, ok := flag.(cli.GenericFlag); ok {
if pp, ok := gf.Value.(Preprocesser); ok {
c.Debug("--%s b4: %#v", gf.Name, gf.Value)
err := pp.Preprocess(c)
ctx.Debug("--%s b4: %#v", gf.Name, gf.Value)
err := pp.Preprocess(ctx)
if err != nil {
return err
}
c.Debug("after: %#v\n", gf.Value)
ctx.Debug("after: %#v\n", gf.Value)
}
}
}
c.preprocessHasRun = true
ctx.preprocessHasRun = true
return nil
}

// cleanup resets the value of special flags between invocations of global.App.Run so that the tests pass.
// 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) {
allFlags := append(c.Command().Flags, c.App().Flags...)
func cleanup(ctx *Context) {
allFlags := append(ctx.Command().Flags, ctx.App().Flags...)
for _, flag := range allFlags {
if genericFlag, ok := flag.(cli.GenericFlag); ok {
flagValue := reflect.ValueOf(genericFlag.Value)
Expand All @@ -67,10 +67,10 @@ func cleanup(c *Context) {
}

// foldProviders runs all the providers with the given context, stopping if there's an error
func foldProviders(c *Context, providers ...ProviderFunc) (err error) {
func foldProviders(ctx *Context, providers ...ProviderFunc) (err error) {
for i, provider := range providers {
c.Debug("Running provider #%d (%v)\n", i, provider)
err = provider(c)
ctx.Debug("Running provider #%d (%v)\n", i, provider)
err = provider(ctx)
if err != nil {
return
}
Expand Down

0 comments on commit 06c0233

Please sign in to comment.