github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/opts/commands.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "strings" 9 10 "github.com/pkg/errors" 11 12 "github.com/jenkins-x/jx-logging/pkg/log" 13 "github.com/olli-ai/jx/v2/pkg/util" 14 ) 15 16 // RunCommandFromDir runs a command in the given directory 17 // Deprecated use util.Command 18 func (o *CommonOptions) RunCommandFromDir(dir, name string, args ...string) error { 19 e := exec.Command(name, args...) 20 if dir != "" { 21 e.Dir = dir 22 } 23 e.Stdout = o.Out 24 e.Stderr = o.Err 25 err := os.Setenv("PATH", util.PathWithBinary()) 26 if err != nil { 27 return errors.Wrap(err, "failed to set PATH env variable") 28 } 29 err = e.Run() 30 if err != nil { 31 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 32 } 33 return err 34 } 35 36 // RunCommand runs a given command command with arguments 37 // Deprecated use util.Command 38 func (o *CommonOptions) RunCommand(name string, args ...string) error { 39 e := exec.Command(name, args...) 40 e.Stdout = o.Out 41 e.Stderr = o.Err 42 err := os.Setenv("PATH", util.PathWithBinary()) 43 if err != nil { 44 return errors.Wrap(err, "failed to set PATH env variable") 45 } 46 err = e.Run() 47 if err != nil { 48 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 49 } 50 return err 51 } 52 53 // RunCommandVerbose runs a given command with arguments in verbose mode 54 // Deprecated use util.Command 55 func (o *CommonOptions) RunCommandVerbose(name string, args ...string) error { 56 e := exec.Command(name, args...) 57 e.Stdout = o.Out 58 e.Stderr = o.Err 59 err := os.Setenv("PATH", util.PathWithBinary()) 60 if err != nil { 61 return errors.Wrap(err, "failed to set PATH env variable") 62 } 63 err = e.Run() 64 if err != nil { 65 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 66 } 67 return err 68 } 69 70 // RunCommandVerboseAt runs a given command in a given folder in verbose mode 71 // Deprecated use util.Command 72 func (o *CommonOptions) RunCommandVerboseAt(dir string, name string, args ...string) error { 73 e := exec.Command(name, args...) 74 if dir != "" { 75 e.Dir = dir 76 } 77 e.Stdout = o.Out 78 e.Stderr = o.Err 79 err := os.Setenv("PATH", util.PathWithBinary()) 80 if err != nil { 81 return errors.Wrap(err, "failed to set PATH env variable") 82 } 83 err = e.Run() 84 if err != nil { 85 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 86 } 87 return err 88 } 89 90 // RunCommandQuietly runs commands and discard the stdout and stderr 91 // Deprecated use util.Command 92 func (o *CommonOptions) RunCommandQuietly(name string, args ...string) error { 93 e := exec.Command(name, args...) 94 e.Stdout = ioutil.Discard 95 e.Stderr = ioutil.Discard 96 err := os.Setenv("PATH", util.PathWithBinary()) 97 if err != nil { 98 return errors.Wrap(err, "failed to set PATH env variable") 99 } 100 return e.Run() 101 } 102 103 // RunCommandInteractive run a given command interactively 104 // Deprecated use util.Command 105 func (o *CommonOptions) RunCommandInteractive(interactive bool, name string, args ...string) error { 106 e := exec.Command(name, args...) 107 e.Stdout = o.Out 108 e.Stderr = o.Err 109 if interactive { 110 e.Stdin = os.Stdin 111 } 112 err := os.Setenv("PATH", util.PathWithBinary()) 113 if err != nil { 114 return errors.Wrap(err, "failed to set PATH env variable") 115 } 116 err = e.Run() 117 if err != nil { 118 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 119 } 120 return err 121 } 122 123 // RunCommandInteractiveInDir run a given command interactively in a given directory 124 // Deprecated use util.Command 125 func (o *CommonOptions) RunCommandInteractiveInDir(interactive bool, dir string, name string, args ...string) error { 126 e := exec.Command(name, args...) 127 e.Stdout = o.Out 128 e.Stderr = o.Err 129 if interactive { 130 e.Stdin = os.Stdin 131 } 132 if dir != "" { 133 e.Dir = dir 134 } 135 err := os.Setenv("PATH", util.PathWithBinary()) 136 if err != nil { 137 return errors.Wrap(err, "failed to set PATH env variable") 138 } 139 err = e.Run() 140 if err != nil { 141 log.Logger().Errorf("Error: Command failed %s %s", name, strings.Join(args, " ")) 142 } 143 return err 144 } 145 146 // GetCommandOutput evaluates the given command and returns the trimmed output 147 // Deprecated use util.Command 148 func (o *CommonOptions) GetCommandOutput(dir string, name string, args ...string) (string, error) { 149 err := os.Setenv("PATH", util.PathWithBinary()) 150 if err != nil { 151 return "", errors.Wrap(err, "failed to set PATH env variable") 152 } 153 e := exec.Command(name, args...) 154 if dir != "" { 155 e.Dir = dir 156 } 157 data, err := e.CombinedOutput() 158 text := string(data) 159 text = strings.TrimSpace(text) 160 if err != nil { 161 return "", fmt.Errorf("Command failed '%s %s': %s %s\n", name, strings.Join(args, " "), text, err) 162 } 163 return text, err 164 } 165 166 // FlagChanged returns true if the given flag was supplied on the command line 167 func (o *CommonOptions) FlagChanged(name string) bool { 168 if o.Cmd != nil { 169 f := o.Cmd.Flag(name) 170 if f != nil { 171 return f.Changed 172 } 173 } 174 return false 175 }