github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/backoff/run.go (about) 1 // Copyright 2012-2021 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 package main 5 6 import ( 7 "fmt" 8 "os" 9 "os/exec" 10 "time" 11 12 "github.com/cenkalti/backoff/v4" 13 ) 14 15 var ( 16 ErrNoCmd = fmt.Errorf("no command passed") 17 ) 18 19 func runit(timeout string, c string, a ...string) error { 20 if c == "" { 21 return ErrNoCmd 22 } 23 b := backoff.NewExponentialBackOff() 24 if len(timeout) != 0 { 25 d, err := time.ParseDuration(timeout) 26 if err != nil { 27 return err 28 } 29 v("Set timeout to %v", d) 30 b.MaxElapsedTime = d 31 } 32 f := func() error { 33 cmd := exec.Command(c, a...) 34 cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr 35 err := cmd.Run() 36 v("%q %q:%v", c, a, err) 37 return err 38 } 39 40 return backoff.Retry(f, b) 41 }