Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunMurugan78 committed Feb 5, 2022
1 parent 4169bc3 commit fbc1282
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
39 changes: 39 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"flag"
"fmt"
"os"
"os/exec"
"time"
Expand Down Expand Up @@ -39,3 +41,40 @@ func Run(config Config) {
time.Sleep(time.Millisecond * time.Duration(config.Timeout))
}
}

func Execute(version string) {
flag.Usage = func() {
fmt.Println("Usage:\n\trtry [OPTIONS] COMMAND\nExample:\n\trtry \"ping google.com\"\nOptions:")
flag.PrintDefaults()
}

timeout := flag.Int("timeout", 1000, "timeout after to retry in milliseconds")
exitCode := flag.Int("code", 0, "expected exit code to stop")
flag.Parse()

commandLineArgs := flag.CommandLine.Args()

if len(commandLineArgs) == 0 {
fmt.Println("rtry ", version)
fmt.Println("Description: (Re-)Try executing a command till it succeeds. ")
fmt.Println("Author: Arun Murugan")
flag.Usage()
os.Exit(0)
}

Run(Config{
Timeout: *timeout,
CommandString: join(commandLineArgs),
ExitCode: *exitCode,
})
}

func join(args []string) string {
r := ""

for _, arg := range args {
r = r + " " + arg
}

return r
}
45 changes: 2 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/ArunMurugan78/rtry-cli/cmd"
)

const (
version = "v1.1.0"
)
var version string

func main() {

flag.Usage = func() {
fmt.Println("Usage:\n\trtry [OPTIONS] COMMAND\nExample:\n\trtry \"ping google.com\"\nOptions:")
flag.PrintDefaults()
}

timeout := flag.Int("timeout", 1000, "timeout after to retry in milliseconds")
exitCode := flag.Int("code", 0, "expected exit code to stop")
flag.Parse()

commandLineArgs := flag.CommandLine.Args()

if len(commandLineArgs) == 0 {
fmt.Println("rtry ", version)
fmt.Println("Description: (Re-)Try executing a command till it succeeds. ")
fmt.Println("Author: Arun Murugan")
flag.Usage()
os.Exit(0)
}

cmd.Run(cmd.Config{
Timeout: *timeout,
CommandString: Join(commandLineArgs),
ExitCode: *exitCode,
})

}

func Join(args []string) string {
r := ""

for _, arg := range args {
r = r + " " + arg
}

return r
cmd.Execute(version)
}

0 comments on commit fbc1282

Please sign in to comment.