55 "errors"
66 "fmt"
77 "math"
8+ "mime"
89 "net/http"
910 "os"
1011 "os/exec"
@@ -24,6 +25,10 @@ func IsInstalled(programName string) bool {
2425}
2526
2627func Execute (programName string , args ... string ) error {
28+ if _ , err := exec .LookPath (programName ); err != nil {
29+ cli .Error ("'%s' ist not installed!" , programName )
30+ return err
31+ }
2732 cmd := exec .Command (programName , args ... )
2833 cmd .Stdin = os .Stdin
2934 cmd .Stdout = os .Stdout
@@ -32,6 +37,10 @@ func Execute(programName string, args ...string) error {
3237}
3338
3439func ExecuteInDir (workingDir , programName string , args ... string ) error {
40+ if _ , err := exec .LookPath (programName ); err != nil {
41+ cli .Error ("'%s' ist not installed!" , programName )
42+ return err
43+ }
3544 cmd := exec .Command (programName , args ... )
3645 cmd .Dir = workingDir
3746 cmd .Stdin = os .Stdin
@@ -41,12 +50,20 @@ func ExecuteInDir(workingDir, programName string, args ...string) error {
4150}
4251
4352func ExecuteHidden (programName string , args ... string ) (string , error ) {
53+ if _ , err := exec .LookPath (programName ); err != nil {
54+ cli .Error ("'%s' ist not installed!" , programName )
55+ return "" , err
56+ }
4457 cmd := exec .Command (programName , args ... )
4558 out , err := cmd .CombinedOutput ()
4659 return string (out ), err
4760}
4861
4962func ExecuteInDirHidden (workingDir , programName string , args ... string ) (string , error ) {
63+ if _ , err := exec .LookPath (programName ); err != nil {
64+ cli .Error ("'%s' ist not installed!" , programName )
65+ return "" , err
66+ }
5067 cmd := exec .Command (programName , args ... )
5168 cmd .Dir = workingDir
5269 out , err := cmd .CombinedOutput ()
@@ -83,7 +100,7 @@ func OpenBrowser(url string) error {
83100
84101func GithubTagFromVersion (owner , repo , version string ) (string , error ) {
85102 res , err := http .Get (fmt .Sprintf ("https://api.github.com/repos/%s/%s/tags" , owner , repo ))
86- if err != nil || res .StatusCode != http .StatusOK {
103+ if err != nil || res .StatusCode != http .StatusOK || ! HasContentType ( res . Header , "application/json" ) {
87104 return "" , cli .Error ("Couldn't access git tags from 'github.com/%s/%s'." , owner , repo )
88105 }
89106 defer res .Body .Close ()
@@ -190,3 +207,20 @@ func ClientVersionFromCGVersion(owner, repo, cgVersion string) string {
190207 cli .Warn ("No compatible client library version found. Using latest client library version." )
191208 return "latest"
192209}
210+ func HasContentType (h http.Header , mimetype string ) bool {
211+ contentType := h .Get ("Content-type" )
212+ if contentType == "" {
213+ return mimetype == "application/octet-stream"
214+ }
215+
216+ for _ , v := range strings .Split (contentType , "," ) {
217+ t , _ , err := mime .ParseMediaType (v )
218+ if err != nil {
219+ break
220+ }
221+ if t == mimetype {
222+ return true
223+ }
224+ }
225+ return false
226+ }
0 commit comments