Last active
January 12, 2019 12:13
-
-
Save ivanzoid/e0caf9f5a994183e5805132dd462caa3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os/exec" | |
"strings" | |
"fmt" | |
) | |
func runProgram1(program string, args ...string) (string, error) { | |
outStrings, err := runProgram(program, args...) | |
if err != nil { | |
return "", err | |
} | |
if len(outStrings) == 0 { | |
return "", nil | |
} | |
return outStrings[0], nil | |
} | |
func runProgram(program string, args ...string) ([]string, error) { | |
//dlog("Running %v", cmdString(program, args)) | |
cmd := exec.Command(program, args...) | |
out, err := cmd.Output() | |
if err != nil { | |
return nil, err | |
} | |
outString := string(out) | |
if len(outString) == 0 { | |
return nil, nil | |
} | |
outStrings := strings.Split(outString, "\n") | |
return outStrings, nil | |
} | |
// Utils | |
func cmdString(program string, args []string) string { | |
comps := make([]string, 0) | |
comps = append(comps, program) | |
for _, arg := range args { | |
if strings.Contains(arg, " ") { | |
comps = append(comps, fmt.Sprintf("'%v'", arg)) | |
} else { | |
comps = append(comps, arg) | |
} | |
} | |
result := strings.Join(comps, " ") | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment