func getOutputLine(cmd *exec.Cmd) (string, error) { out, err := cmd.Output() if err != nil { return "", err } return strings.Trim(string(out), " \n\t\r"), nil }
func ShellExec(cmd *exec.Cmd) []byte { stdout, err := cmd.Output() if err != nil { log.Fatalln(err) } return stdout }
// Log returns an array of Commit objects, representing the history as like // git log. Newest commit is at index zero, oldest at the end. Leave hashish // as a blank string to get the default "git log" result. func Log(path, hashish string) ([]*Commit, error) { logFormat := "%H" // Just the hashes var cmd *exec.Cmd if hashish == "" { cmd = command("git", "log", "--pretty="+logFormat) } else { cmd = command("git", "log", "--pretty="+logFormat, hashish) } cmd.Dir = path output, err := cmd.Output() if err != nil { return []*Commit{}, err } lineBytes := bytes.Split(output, []byte{'\n'}) // The last split is just an empty string, right? lineBytes = lineBytes[0 : len(lineBytes)-1] commits := make([]*Commit, len(lineBytes)) for x := 0; x < len(lineBytes); x++ { commit, commitErr := NewCommit(path, string(lineBytes[x])) if commitErr != nil { return []*Commit{}, commitErr } commits[x] = commit } return commits, nil }
func mustGetOutput(c *exec.Cmd) []byte { b, err := c.Output() if err != nil { panic(err) } return b }
/* Checks if the constants are equal to the ones in Python by running the python interpreter. Also exercises the New(), Join() and Encode() functions. */ func TestConstants(t *testing.T) { var shouldBeBytes []byte var cmd *exec.Cmd constants := []string{"ascii_letters", "ascii_lowercase", "ascii_uppercase", "digits", "hexdigits", "octdigits", "punctuation", "printable", "whitespace"} shouldbe := []string{ASCII_letters, ASCII_lowercase, ASCII_uppercase, Digits, HexDigits, OctDigits, Punctuation, Printable, Whitespace} for i, constant := range constants { // Make sure to use python3. Tested on Fedora and Arch Linux. cmd = exec.Command("python3", "-c", "import string; print(string."+constant+")") output, err := cmd.Output() if err != nil { /* One of the commands failed, assume Python is not available */ // This can be used if one doesn't want to ignore the lack of python: //t.Errorf("execution failed: %s %s\n", New(" ").Join(cmd.Args), output) return } shouldBeBytes = New(shouldbe[i]).Encode() for i2, b := range shouldBeBytes { if (i2 >= len(output)) || (b != output[i2]) { if len(output) == 0 { output = New("no output").Encode() } t.Errorf("constant %s failed, got: %s\n", constants[i], output) return } } } }
// Utility Functions func bExec(cmd exec.Cmd) (out []byte) { out, err := cmd.Output() if err != nil { log.Fatal(err) } return }
func pluginInfo(name string) (ver, short, long string) { if os.Getenv("HKPLUGINMODE") == "info" { return "", "plugin exec loop", "plugin exec loop" } short = "no description" long = name + ": unknown description" var cmd exec.Cmd cmd.Args = []string{name} cmd.Path = lookupPlugin(name) cmd.Env = append([]string{"HKPLUGINMODE=info"}, os.Environ()...) buf, err := cmd.Output() if err != nil { return } info := string(buf) if !strings.HasPrefix(info, name+" ") { return } info = info[len(name)+1:] i := strings.Index(info, ": ") if i < 0 { return } ver, info = info[:i], info[i+2:] i = strings.Index(info, "\n\n") if i < 0 || 50 < i || strings.Contains(info[:i], "\n") { return } short, long = info[:i], info[i+2:] return ver, strings.TrimSpace(short), strings.TrimSpace(long) }
func GetWireDefaultGateway() (net.IP, error) { var regex *regexp.Regexp var command *exec.Cmd switch runtime.GOOS { case "darwin": command = exec.Command("netstat", "-rn", "-f", "inet") regex = regexp.MustCompile(`default\s+(\d+\.\d+\.\d+\.\d+)\s+.*`) case "linux": command = exec.Command("ip", "-4", "route", "show") regex = regexp.MustCompile(`default\s+via\s+(\d+\.\d+\.\d+\.\d+)\s+.*`) default: return nil, fmt.Errorf("Getting gateway is not supported in %v", runtime.GOOS) } output, err := command.Output() if err != nil { return nil, err } if result := regex.FindSubmatch(output); result == nil { return nil, fmt.Errorf("Unable to get default gateway") } else { return net.ParseIP(string(result[1])), nil } }
func (spec SpecPackage) checkVersionPackageExact(defaults PlatformDefaults) (err error) { var cmd *exec.Cmd var verRex string switch spec.Type { case "homebrew": // sbt 0.12.3 0.12.4 0.13.0 cmd = exec.Command("/usr/local/bin/brew", "ls", "--versions", spec.Name) verRex = " " + spec.Version + "( |$)" default: return errors.New("Unknown package manager type " + spec.Type) } out, err := cmd.Output() if err != nil { return err } re, err := regexp.Compile(verRex) if err != nil { return fmt.Errorf("Bad verRex %s for package manager type %s", verRex, spec.Type) } if re.FindIndex(out) == nil && !spec.Absent { return errors.New("No such version installed") } return nil }
func worker(throttle chan struct{}, wg *sync.WaitGroup, e fsnotify.Op, f string) { defer func() { <-throttle wg.Done() }() m := findMatch(e, f) if m == nil { return } // Atomicity for the given match m.mu.Lock() defer m.mu.Unlock() var cmd *exec.Cmd for i, c := range m.cmds { if len(c) == 1 { cmd = exec.Command(c[0]) } else { cmd = exec.Command(c[0], c[1:]...) } if b, err := cmd.Output(); err != nil { log.Printf("command (%s) error: %s", c, err) } else if len(b) > 0 { log.Printf("%s\n%s", m.rule.cmds[i], b) } } }
// stem words and add them to vocabulary func (vocabulary Dictionary) add(words []string, mode string) (wordIDs []int) { var lemma string wordIDs = make([]int, len(words)) for idx, word := range words { if _, ok := vocabulary.Vtoi[word]; ok { wordIDs[idx] = vocabulary.Vtoi[word] } else { python := new(exec.Cmd) args := []string{"lemmatizer.py", "--vocabulary"} args = append(args, word) args = append(args, "--pos") args = append(args, mode) python.Args = args // python.Path = "/home/lea/Code/Python/lemmatizer.py" python.Path = "/local/lea/thesis/python/lemmatizer.py" out, _ := python.Output() lemma = strings.Trim(string(out), "\n") if _, ok := vocabulary.Vtoi[lemma]; !ok { vocabulary.VList[vocabIdx] = lemma vocabulary.Vtoi[lemma] = vocabIdx vocabulary.Itov[vocabIdx] = lemma vocabulary.POSList[vocabIdx] = mode vocabIdx++ } wordIDs[idx] = vocabulary.Vtoi[lemma] } } return wordIDs }
func Recon(pkgPath string) (pkgInfo *PkgInfo, err error) { var gitProgPath, pkgPathAbs string // Firstly look up if they've got Git gitProgPath, err = exec.LookPath("git") if err != nil { return } pkgPathAbs = goSrcify(pkgPath) args := []string{gitProgPath} args = append(args, prettyGitHashGetArgs...) cmd := exec.Cmd{ Args: args, Dir: pkgPathAbs, Path: gitProgPath, } var output []byte if output, err = cmd.Output(); err != nil { return } pkgInfo = &PkgInfo{ CommitHash: string(output), OsInfo: strings.Join([]string{runtime.GOOS, runtime.GOARCH}, "/"), GoVersion: runtime.Version(), } return }
func executeCommand(targetPath string, command string) (logContent string, err error) { logBuffer := new(bytes.Buffer) defer func() { logContent = logBuffer.String() }() bufferLog(logBuffer, separator) bufferLog(logBuffer, "\nEntry into target Path: %s\n", targetPath) err = os.Chdir(targetPath) if err != nil { bufferLog(logBuffer, "ChdirError (%s): %s\n", targetPath, err) return "", err } cmdWithArgs := strings.Split(command, " ") var cmd *exec.Cmd cmdLength := len(cmdWithArgs) realCmd := cmdWithArgs[0] args := cmdWithArgs[1:cmdLength] bufferLog(logBuffer, "Execute command (cmd=%s, args=%s)...\n", realCmd, args) if cmdLength > 1 { cmd = exec.Command(realCmd, args...) } else { cmd = exec.Command(realCmd) } result, err := cmd.Output() if err != nil { bufferLog(logBuffer, "CmdRunError (cmd=%s, args=%v): %s\n", realCmd, args, err) return "", err } bufferLog(logBuffer, "Output: %v\n", string(result)) return "", nil }
func pluginInfo(name string) (ver, short, long string) { if os.Getenv("HKPLUGINMODE") == "info" { return "", "[plugin exec loop]", "[plugin exec loop]\n" } var cmd exec.Cmd cmd.Args = []string{name} cmd.Path = lookupPlugin(name) cmd.Env = append([]string{"HKPLUGINMODE=info"}, os.Environ()...) buf, err := cmd.Output() if err != nil { fmt.Fprintln(os.Stderr, err) return "", "[unknown description]", "[unknown description]\n" } info := string(buf) if !strings.HasPrefix(info, name+" ") { return "", "[unknown description]", "[unknown description]\n" } info = info[len(name)+1:] i := strings.Index(info, ": ") ver, info = info[:i], info[i+2:] i = strings.Index(info, "\n\n") short, long = info[:i], info[i+2:] if len(short) > 50 || strings.Contains(short, "\n") { return "", "[unknown description]", "[unknown description]\n" } return ver, short, long }
/** * Execute command inside shell on linux or windows */ func ExecShellScriptOrPanic(args string) string { var ( shell string cmd *exec.Cmd ) switch runtime.GOOS { case "windows": shell = "cmd.exe" cmdArgs := []string{"/C", args} cmd = exec.Command(shell, cmdArgs...) break case "linux": shell = "bash" cmdArgs := []string{"-c", args} cmd = exec.Command(shell, cmdArgs...) break default: panic("Unsupported target OS, unable to exec command !") break } output, err := cmd.Output() if err != nil { panic(err) } return string(output) }
func getHostNS(configNSPath bool) (string, error) { //2. Get the namespaces on host /* ipc:[4026531839] mnt:[4026531840] mnt:[4026531856] mnt:[4026532129] net:[4026531956] net:[4026532265] pid:[4026531836] pid:[4026532263] user:[4026531837] user:[4026532227] uts:[4026531838] */ var cmd *exec.Cmd if configNSPath { cmd = exec.Command("/bin/sh", "-c", "readlink /proc/1/ns/* | sort -u") } else { cmd = exec.Command("/bin/sh", "-c", "readlink /proc/*/ns/* | sort -u") } out, err := cmd.Output() if err != nil { return "", err } str := strings.TrimSuffix(string(out), "\n") str = strings.TrimSpace(str) if str == "" { return "", errors.New("can not find namespace on host.") } return str, nil }
// getRevisionFromPath takes a path like /home/csparr/go/src/github.com/sparrc/gdm // and the VCS Repository information and returns the currently checked out // revision, ie, 759e96ebaffb01c3cba0e8b129ef29f56507b323 func getRevisionFromPath(fullpath string, root *vcs.RepoRoot) string { // Check that we have the executable available _, err := exec.LookPath(root.VCS.Cmd) if err != nil { fmt.Fprintf(os.Stderr, "gdm missing %s command.\n", root.VCS.Name) os.Exit(1) } // Determine command to get the current hash var cmd *exec.Cmd switch root.VCS.Cmd { case "git": cmd = exec.Command("git", "rev-parse", "HEAD") case "hg": cmd = exec.Command("hg", "id", "-i") case "bzr": cmd = exec.Command("bzr", "revno") default: fmt.Fprintf(os.Stderr, "gdm does not support %s\n", root.VCS.Cmd) os.Exit(1) } cmd.Dir = fullpath output, err := cmd.Output() if err != nil { fmt.Fprintf(os.Stderr, "Error getting revision hash at %s, %s\n", fullpath, err.Error()) os.Exit(1) } return strings.TrimSpace(string(output)) }
/* GetCommandOutputString runs the given command, returning its output as a string - or an error on failure. */ func GetCommandOutputString(command *exec.Cmd) (outputString string, err error) { bytes, err := command.Output() if err != nil { return "", err } return string(bytes), nil }
// trimOutput runs the command and returns its stdout output with trimmed whitespace. // if cmd.Stderr is not set, it is set to redStderr. func trimOutput(cmd *exec.Cmd) (string, error) { if cmd.Stderr == nil { cmd.Stderr = redStderr } logCmd(cmd) out, err := cmd.Output() return strings.TrimSpace(string(out)), err }
func (repository *gitRepository) runGitCommand(cmd *exec.Cmd) (string, error) { cmd.Dir = repository.DirPath out, err := cmd.Output() if err != nil { return "", err } return strings.Trim(string(out), " \n"), nil }
func wildcardGlob(pat string) ([]string, error) { // TODO(ukai): use find cache for glob if exists. pat = wildcardUnescape(pat) pattern := filepath.Clean(pat) if pattern != pat { // For some reason, go's Glob normalizes // foo/../bar to bar. i := strings.IndexAny(pattern, "*?[") if i < 0 { // no wildcard. if any files matched with pattern, // return pat. _, err := os.Stat(pat) if err != nil { return nil, nil } return []string{pat}, nil } if strings.Contains(pattern[i+1:], "..") { // We ask shell to expand a glob to avoid this. cmdline := []string{"/bin/sh", "-c", "/bin/ls -d " + pat} cmd := exec.Cmd{ Path: cmdline[0], Args: cmdline, } // Ignore errors. out, _ := cmd.Output() ws := newWordScanner(out) var files []string for ws.Scan() { files = append(files, string(ws.Bytes())) } return files, nil } // prefix + meta + suffix, and suffix doesn't have '..' prefix := pattern[:i] i = strings.IndexAny(pat, "*?[") if i < 0 { return nil, fmt.Errorf("wildcard metachar mismatch? pattern=%q pat=%q", pattern, pat) } oprefix := pat[:i] matched, err := filepath.Glob(pattern) if err != nil { return nil, err } var files []string for _, m := range matched { file := oprefix + strings.TrimPrefix(m, prefix) _, err := os.Stat(file) if err != nil { continue } files = append(files, file) } return files, nil } return filepath.Glob(pat) }
// Run executes cmd with option STDIN func (c DefaultCommander) Run(cmd *exec.Cmd) ([]byte, error) { out, err := cmd.Output() defer func() { // If this fails, there isn't much to do _ = cmd.Process.Kill() }() return out, err }
func configureTuntap(is_client bool, devName string) { // Set default IP address, if needed. if len(ipaddr) == 0 { if is_client { ipaddr = "10.93.0.2" } else { ipaddr = "10.93.0.1" } } // Need to run: ifconfig tunX 10.0.0.1 10.0.0.1 netmask 255.255.255.0 up var cmd *exec.Cmd if is_client { cmd = exec.Command("/sbin/ifconfig", devName, ipaddr, server_addr, "netmask", netmask, "up") } else { cmd = exec.Command("/sbin/ifconfig", devName, ipaddr, ipaddr, "netmask", netmask, "up") } out, err := cmd.Output() if err != nil { log.Printf("Error running configuration command: %s\n", err) } else { log.Printf("Configured successfully (output: '%s')\n", out) } // TODO: we should repeatedly check until the interface is up, rather than // just waiting for a given length <-time.After(1 * time.Second) // Get output of ifconfig. stat := exec.Command("/sbin/ifconfig") out, err = stat.Output() if err != nil { log.Printf("Error running status command: %s\n", err) } else { lines := strings.Split(string(out), "\n") var out_lines []string found := false for _, l := range lines { // We start grabbing at the tun device, and stop when we hit a blank line, // which indicates another device. if strings.HasPrefix(l, devName) { found = true } else if found && l == "" { found = false } if found { out_lines = append(out_lines, l) } } log.Printf("ifconfig output:\n%s", strings.Join(out_lines, "\n")) } }
func (f *funcShell) Eval(w evalWriter, ev *Evaluator) error { err := assertArity("shell", 1, len(f.args)) if err != nil { return err } abuf := newEbuf() err = f.args[1].Eval(abuf, ev) if err != nil { return err } if ev.avoidIO && !hasNoIoInShellScript(abuf.Bytes()) { te := traceEvent.begin("shell", tmpval(abuf.Bytes()), traceEventMain) ev.hasIO = true io.WriteString(w, "$(") w.Write(abuf.Bytes()) writeByte(w, ')') traceEvent.end(te) abuf.release() return nil } arg := abuf.String() abuf.release() if bc, err := parseBuiltinCommand(arg); err != nil { glog.V(1).Infof("sh builtin: %v", err) } else { glog.Info("use sh builtin:", arg) glog.V(2).Infof("builtin command: %#v", bc) te := traceEvent.begin("sh-builtin", literal(arg), traceEventMain) bc.run(w) traceEvent.end(te) return nil } shellVar, err := ev.EvaluateVar("SHELL") if err != nil { return err } cmdline := []string{shellVar, "-c", arg} if glog.V(1) { glog.Infof("shell %q", cmdline) } cmd := exec.Cmd{ Path: cmdline[0], Args: cmdline, Stderr: os.Stderr, } te := traceEvent.begin("shell", literal(arg), traceEventMain) out, err := cmd.Output() shellStats.add(time.Since(te.t)) if err != nil { glog.Warningf("$(shell %q) failed: %q", arg, err) } w.Write(formatCommandOutput(out)) traceEvent.end(te) return nil }
func CmdOutput(cmd *exec.Cmd) ([]byte, error) { bs := bytes.Buffer{} cmd.Stderr = &bs if output, err := cmd.Output(); err != nil { fmt.Printf("Error running %v %v:\n%v\n%v\n", cmd.Path, cmd.Args, string(output), string(bs.Bytes())) return nil, err } else { return output, nil } }
// commandOutput runs an exec.Cmd, builds for every line of the output // a new Searchresult, and passes these into channel c. func commandOutput(cmd *exec.Cmd, c chan []Searchresult) { out, _ := cmd.Output() res := make([]Searchresult, 0) split := strings.Split(string(out), "\n") for _, i := range split { sr := NewSearchResult(i) res = append(res, *sr) } c <- res }
func GetCmdOutput(cmd *exec.Cmd) (string, error) { buf := &bytes.Buffer{} cmd.Stderr = buf out, err := cmd.Output() if err != nil { return "", fmt.Errorf("External program '%s' failed (%s), with "+ "output:\n%s", cmd.Args[0], err, buf.String()) } return strings.TrimSpace(string(out)), nil }
func openBuf(data []byte, filetype string) (obuf []byte, err error) { p, err := exec.LookPath("sox") if err != nil { log.Fatalln("no sox installed!", err) } var c *exec.Cmd var reader io.Reader switch filetype { case "mp3": fallthrough case "wav": fallthrough case "ogg": fallthrough case "au": reader = bytes.NewReader(data) case "mp4": fallthrough case "m4a": // else write to tmpfile, then invoke sox, defer deletion of tmpfile. weak. w, err := mktemp(filetype) if err != nil { log.Println("couldn't make tmp file", err) return nil, err } defer func() { w.Close(); os.Remove(w.Name()) }() var n int n, err = w.Write(data) if n != len(data) { log.Println("couldn't write all data to tmp file!") } if err != nil { log.Println("couldn't write to tmp file", w.Name(), err) return nil, err } w.Seek(0, os.SEEK_SET) reader = w default: return nil, errors.New("unrecognized filetype") } // use samplerate from args, 16 bits, big endian, two channels c = exec.Command(p, "-t", filetype, "-", "-b", "16", "-c", "2", "-e", "signed-integer", "-t", "raw", "-r", strconv.Itoa(samplerate), "-B", "-") c.Stdin = reader errbuf := new(bytes.Buffer) c.Stderr = errbuf obuf, err = c.Output() if err != nil { log.Println("error running sox", err, string(errbuf.Bytes())) return nil, err } return }
func main() { fmt.Printf("GoHEVC Analyzer %v for HM\n\n", hevc.Version) if len(os.Args) == 3 { var cmd *exec.Cmd const refYUV = "temp.yuv" if os.Args[2] == "hm80" { cmd = exec.Command("TAppDecoder80", "-b", os.Args[1], "-o", refYUV) } else if os.Args[2] == "hm90" { cmd = exec.Command("TAppDecoder90", "-b", os.Args[1], "-o", refYUV) } else if os.Args[2] == "hm91" { cmd = exec.Command("TAppDecoder91", "-b", os.Args[1], "-o", refYUV) } else { fmt.Printf("Unsupported HM Version\n") return } fmt.Printf("Decoding %s", os.Args[1]) out, err := cmd.Output() if err != nil { log.Fatal(err) } else { fmt.Printf("%s\n", out) fmt.Printf("\nAnalyzing %s\n", os.Args[1]) traceFile, err := os.Open("TraceDec.txt") if err != nil { log.Fatal(err) } defer traceFile.Close() yuvFile, err := os.Open(refYUV) if err != nil { log.Fatal(err) } defer yuvFile.Close() var ha hevc.Analyzer if err = ha.ParseTrace(traceFile); err != nil { log.Fatal(err) } else { if err = ha.ParseYUV(yuvFile); err != nil { log.Fatal(err) } else { ha.ParseCmd(os.Args[1], bufio.NewReader(os.Stdin), string(out)) } } } } else { fmt.Printf("Usage: gohevc.exe bitstream.265 hmversion(hm80/hm90/hm91)\n") } }
func runCheck(cmd *exec.Cmd, name string, context string) bool { log.Print("running: ", name) out, err := cmd.Output() if err != nil { log.Print("could not run command: ", err.Error()) } // We assume a command ran correctly if there is no output return len(out) == 0 }