Skip to content

Commit

Permalink
fix: output
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Mar 19, 2023
1 parent 6b90381 commit d675b10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module go.octolab.org/toolset/try

go 1.20

require github.com/spf13/pflag v1.0.5
require (
github.com/avast/retry-go/v4 v4.3.3
github.com/spf13/pflag v1.0.5
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
github.com/avast/retry-go/v4 v4.3.3 h1:G56Bp6mU0b5HE1SkaoVjscZjlQb0oy4mezwY/cGH19w=
github.com/avast/retry-go/v4 v4.3.3/go.mod h1:rg6XFaiuFYII0Xu3RDbZQkxCofFwruZKW8oEF1jpWiU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 27 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"os"
"os/exec"
"time"

"github.com/avast/retry-go/v4"
"github.com/spf13/pflag"
)

Expand All @@ -19,7 +21,8 @@ func main() {
var opt = Option{}

cli := pflag.NewFlagSet("try", pflag.ContinueOnError)
cli.IntVar(&opt.Limit, "limit", 5, "max retry")
cli.UintVar(&opt.Limit, "limit", 5, "max retry")
cli.DurationVar(&opt.Delay, "delay", time.Millisecond*100, "retry delay")
if err := cli.Parse(flags); err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand All @@ -33,28 +36,39 @@ func main() {
}

type Option struct {
Limit int
Limit uint
Delay time.Duration
}

func (o Option) Retry(cmd string, args []string) error {
if o.Limit > 0 {
o.Limit = 3
}

for i := 0; i < o.Limit; i++ {
fmt.Printf("--- retry %d run ---\n", i+1)
c := exec.Command(cmd, args...)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err := c.Run()
if err == nil {
return nil
}
err := retry.Do(
func() error {
c := exec.Command(cmd, args...)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err := c.Run()
if err == nil {
return nil
}

return err
},
retry.Attempts(o.Limit),
retry.Delay(o.Delay),
retry.OnRetry(func(n uint, err error) {
fmt.Printf("--- %d run failed, err: %s ---\n", n+1, err)
}),
)

fmt.Printf("--- run %d failed, exit code %d ---\n", i+1, c.ProcessState.ExitCode())
if err != nil {
return err
}

return fmt.Errorf("failed to run command after retries")
return nil
}

func partitionCommand(args []string) ([]string, []string) {
Expand Down

0 comments on commit d675b10

Please sign in to comment.