Skip to content

Commit

Permalink
chore: add post run hook (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow authored Aug 23, 2023
1 parent 189155e commit c42d666
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
27 changes: 25 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

type Initializer func(*State) error

type PostRun func(*State, error)

type postConstruct func(*application)

type Application interface {
Expand Down Expand Up @@ -112,16 +114,37 @@ func (a *application) runInitializers() error {
return nil
}

func (a *application) runPostRuns(err error) {
for _, postRun := range a.setupConfig.postRuns {
a.runPostRun(postRun, err)
}
}

func (a *application) runPostRun(fn PostRun, err error) {
defer func() {
// handle panics in each postRun -- the app may already be in a panicking situation,
// this recover should not affect the original panic, as it is being run in a
// different call stack from the original panic, but the original panic should
// return without confusing things when a postRun also fails by panic
if v := recover(); v != nil {
a.state.Logger.Debugf("panic while calling postRun: %v", v)
}
}()
fn(&a.state, err)
}

func (a *application) WrapRunE(fn func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
wrapper := func(cmd *cobra.Command, args []string) error {
wrapper := func(cmd *cobra.Command, args []string) (err error) {
defer func() {
// when the worker has completed (or errored) we want to exit the event loop gracefully
if a.state.Bus != nil {
a.state.Bus.Publish(ExitEvent(false))
}
}()
return fn(cmd, args)
defer a.runPostRuns(err)
err = fn(cmd, args)
return
}

return a.execute(cmd.Context(), async(cmd, args, wrapper))
Expand Down
28 changes: 28 additions & 0 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,34 @@ func Test_Application_Setup_RunsInitializers(t *testing.T) {
require.NoError(t, cmd.Execute())
}

func Test_Application_Setup_RunsPostRuns(t *testing.T) {
name := "puppy"
version := "2.0"

cfg := NewSetupConfig(Identification{Name: name, Version: version}).WithPostRuns(
func(state *State, err error) {
t.Setenv("PUPPY_THING_STUFF", "bark-bark!")
},
)

t.Setenv("PUPPY_THING_STUFF", "ruff-ruff!")

app := New(*cfg)

app.SetupRootCommand(
&cobra.Command{
DisableFlagParsing: true,
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
})

app.Run()

assert.Equal(t, "bark-bark!", os.Getenv("PUPPY_THING_STUFF"))
}

func Test_SetupCommand(t *testing.T) {
p := &persistent{}

Expand Down
6 changes: 6 additions & 0 deletions setup_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type SetupConfig struct {
UIConstructor UIConstructor
Initializers []Initializer
postConstructs []postConstruct
postRuns []PostRun
}

func NewSetupConfig(id Identification) *SetupConfig {
Expand Down Expand Up @@ -97,6 +98,11 @@ func (c *SetupConfig) WithInitializers(initializers ...Initializer) *SetupConfig
return c
}

func (c *SetupConfig) WithPostRuns(postRuns ...PostRun) *SetupConfig {
c.postRuns = append(c.postRuns, postRuns...)
return c
}

func (c *SetupConfig) withPostConstructs(postConstructs ...postConstruct) *SetupConfig {
c.postConstructs = append(c.postConstructs, postConstructs...)
return c
Expand Down

0 comments on commit c42d666

Please sign in to comment.