Skip to content

Commit

Permalink
Add rudimentary support for verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 21, 2024
1 parent 4e0e2e3 commit be9ee25
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions runny/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runny

import (
"fmt"
"os"

"github.com/fatih/color"
Expand All @@ -21,9 +20,10 @@ func Run() {
runny.ShowHelp()
return
case "-v", "--verbose":
// TODO: handle verbose case
runny.verbose = true
default:
panic(fmt.Sprintf("Unknown option: %s", option))
color.Red("Unknown option: %s", option)
os.Exit(1)
}
args = args[1:]
}
Expand Down
12 changes: 8 additions & 4 deletions runny/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type CommandDef struct {
type Config struct {
Commands map[CommandName]CommandDef
Shell string
verbose bool
}

func (c *Config) GetShell() Shell {
Expand All @@ -30,7 +31,8 @@ func (c *Config) GetShell() Shell {
}
shell, err := NewShell(shellString)
if err != nil {
panic(err)
color.Red("Error: %v", err)
os.Exit(1)
}
return shell
}
Expand Down Expand Up @@ -81,18 +83,20 @@ func (c *Config) Execute(name CommandName, args ...string) error {
// Check the If
cond := strings.TrimSpace(command.If)
if len(cond) > 0 {
err := shell.Run(cond)
err := shell.Run(cond, c.verbose)
if err != nil {
// Run returns an error if the exit status is not zero. So in this case, this means the test failed.
// secondaryColor.Printf("'%v' not true\n", cond)
if c.verbose {
secondaryColor.Printf("%v: '%v' not true, skipping\n", name, cond)
}
return nil
}
}

// Handle the Run
run := strings.TrimSpace(command.Run)
if len(run) > 0 {
err := shell.Run(run, args...)
err := shell.Run(run, c.verbose, args...)
if err != nil {
fmt.Printf("%s %s\n", color.RedString(string(run)), secondaryColor.Sprint(err))
return err
Expand Down
8 changes: 6 additions & 2 deletions runny/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Shell interface {
Run(string, ...string) error
Run(string, bool, ...string) error
}

type BashShell struct {
Expand All @@ -24,10 +24,14 @@ func NewShell(command string) (Shell, error) {
return nil, fmt.Errorf("unsupported shell: %s", command)
}

func (b BashShell) Run(command string, extraArgs ...string) error {
func (b BashShell) Run(command string, verbose bool, extraArgs ...string) error {
if len(extraArgs) > 0 {
command = command + " " + strings.Join(extraArgs, " ")
}

if verbose {
secondaryColor.Println(command)
}
args := []string{"-c", command}

cmd := exec.Command(b.command, args...)
Expand Down

0 comments on commit be9ee25

Please sign in to comment.