Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Call a given function after calling the command (PostRun) #21

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cli.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Cli struct {
rootCommand *Command
defaultCommand *Command
preRunCommand func(*Cli) error
postRunCommand func(*Cli) error
bannerFunction func(*Cli) string
errorHandler func(string, error) error
}
Expand Down Expand Up @@ -70,7 +71,18 @@ func (c *Cli) Run(args ...string) error {
if len(args) == 0 {
args = os.Args[1:]
}
return c.rootCommand.run(args)
if err := c.rootCommand.run(args); err != nil {
return err
}

if c.postRunCommand != nil {
err := c.postRunCommand(c)
if err != nil {
return err
}
}

return nil
}

// DefaultCommand - Sets the given command as the command to run when
Expand All @@ -95,6 +107,11 @@ func (c *Cli) PreRun(callback func(*Cli) error) {
c.preRunCommand = callback
}

// PostRun - Calls the given function after running the specific command.
func (c *Cli) PostRun(callback func(*Cli) error) {
c.postRunCommand = callback
}

// BoolFlag - Adds a boolean flag to the root command.
func (c *Cli) BoolFlag(name, description string, variable *bool) *Cli {
c.rootCommand.BoolFlag(name, description, variable)
Expand Down
8 changes: 6 additions & 2 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestCli(t *testing.T) {

c.preRunCommand = func(*Cli) error { return errors.New("testing coverage") }
c.Run("test")
c.postRunCommand = func(*Cli) error { return errors.New("testing coverage") }
})

t.Run("Run DefaultCommand()", func(t *testing.T) {
Expand All @@ -45,6 +46,10 @@ func TestCli(t *testing.T) {
c.PreRun(func(*Cli) error { return nil })
})

t.Run("Run PostRun()", func(t *testing.T) {
c.PostRun(func(*Cli) error { return nil })
})

t.Run("Run BoolFlag()", func(t *testing.T) {
var variable bool
c.BoolFlag("bool", "description", &variable)
Expand Down Expand Up @@ -101,7 +106,6 @@ func TestCli_CLIAddFlags(t *testing.T) {
if e != nil {
t.Errorf("expected no error, got %v", e)
}

}

func TestCli_CommandAddFlags(t *testing.T) {
Expand All @@ -121,8 +125,8 @@ func TestCli_CommandAddFlags(t *testing.T) {
if e != nil {
t.Errorf("expected no error, got %v", e)
}

}

func TestCli_InheritFlags(t *testing.T) {
c := NewCli("test", "description", "0")
var name string
Expand Down
5 changes: 1 addition & 4 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,16 @@ func TestCommand(t *testing.T) {
})

t.Run("Run StringFlag()", func(t *testing.T) {
var variable = "variable"
variable := "variable"
c.StringFlag("name", "description", &variable)

})

t.Run("Run IntFlag()", func(t *testing.T) {
var variable int
c.IntFlag("test", "description", &variable)

})

t.Run("Run LongDescription()", func(t *testing.T) {
c.LongDescription("name")
})

}
42 changes: 42 additions & 0 deletions website/docs/guide/prepostrun.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "Pre/Post run"
date: 2023-09-23T16:15:09+08:00
draft: false
weight: 100
chapter: false
---

The PreRun and PostRun methods allow you to specify custom functions that should be executed before and after running a command, respectively. These functions can be used for tasks such as setup, cleanup, or any other actions you want to perform before or after executing a command.

PreRun

The PreRun method is used to specify a function that should run before executing a command. The function you pass to PreRun takes a *Cli parameter, which represents the CLI application itself. You can use this function to perform any necessary setup or validation before running the command.

PostRun
The PostRun method is used to specify a function that should run after executing a command. Similar to PreRun, the function you pass to PostRun takes a *Cli parameter. You can use this function to perform any cleanup or post-processing tasks after the command execution.

Example of postRun
```go
func main() {
cli := clir.NewCli("MyApp", "My CLI application")

// Define a command
cmd := cli.NewSubCommand("mycommand", "Description of mycommand")

// Add flags, actions, and other configurations for the command

// Define a PostRun function for the command
cmd.PostRun(func(c *clir.Cli) error {
// Perform cleanup or post-processing here
fmt.Println("Running PostRun for 'mycommand'")
return nil // Return an error if something goes wrong
})

// Run the CLI application
if err := cli.Run(os.Args[1:]...); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}

```
Loading