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) }
// 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 }
// Initialize a new ExecutablePlugin from path to executable and daemon mode (true or false) func NewExecutablePlugin(a Arg, path string) (*ExecutablePlugin, error) { jsonArgs, err := json.Marshal(a) if err != nil { return nil, err } // Init the cmd cmd := new(exec.Cmd) cmd.Path = path cmd.Args = []string{path, string(jsonArgs)} // Link the stdout for response reading stdout, err := cmd.StdoutPipe() if err != nil { return nil, err } stderr, err := cmd.StderrPipe() if err != nil { return nil, err } // Init the ExecutablePlugin and return ePlugin := new(ExecutablePlugin) ePlugin.cmd = cmd ePlugin.stdout = stdout ePlugin.args = a ePlugin.stderr = stderr return ePlugin, nil }
// Run a command, waiting for it to finish. Will first run CMD's path; // failing that, will lookup the path and attempt to do the same. func execvp(cmd *exec.Cmd) error { if err := cmd.Start(); err == nil { // Wait for command to finish return cmd.Wait() } else { // Didn't work? Search for the executable's path path, err := exec.LookPath(cmd.Path) if err != nil { return err } // Reset our path cmd.Path = path // Try again with the executable found in $PATH err = cmd.Start() if err != nil { return err } return cmd.Wait() } }
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 }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s", Help) os.Exit(1) } flag.Parse() if *version { fmt.Printf("%s", Version) os.Exit(0) } if *unset != "" { os.Unsetenv(*unset) } cmd := new(exec.Cmd) cmd.Env = env // Check for "-" as an argument, because it means the same as "-i" if flag.Arg(0) == "-" { cmd.Env = make([]string, 0) } for i, arg := range flag.Args() { if strings.Index(arg, delim) > 0 { cmd.Env = append(cmd.Env, arg) } else if arg != "-" { if *nullEol { fatal.Fatalln("cannot specify --null (-0) with command") } cmd.Path = arg cmd.Args = append(cmd.Args, flag.Args()[i:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := execvp(cmd) if err != nil { fatal.Fatalln(err) } return } i++ } eol := '\n' if *nullEol { eol = '\x00' } for _, e := range env { fmt.Printf("%s%c", e, eol) } return }
func RunWithEnvAndWd(command string, args []string, env []string, wd string) (proc *exec.Cmd, err error) { //log.Println(command, args) //hho := exec.PassThrough args = prepend(args, command) env = mergeEnv(os.Environ(), env) binpath, err := findCmd(findEnv(env, "PATH"), command) if err != nil { return nil, err } cmd := new(exec.Cmd) cmd.Path = binpath cmd.Args = args cmd.Env = env cmd.Dir = wd cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout err = cmd.Start() if err != nil { log.Print("Error running command ", command, ": ", err, "\n") return nil, err } return cmd, nil }
func execute(path string, vcs string, vcs_verb string) { if vcs_verb == "" { return } a := func(wd string, vcs string, vcs_verb string, seq bool) { msg := "=== @{!" + vcs_color[vcs] + "} " msg += filepath.Base(path) msg += "@| (@!" + vcs + "@|) ===" var cmd exec.Cmd cmd.Dir = wd cmd.Args = strings.Split(vcs_verb, " ") path, err := exec.LookPath(cmd.Args[0]) if err != nil { path = cmd.Args[0] } cmd.Path = path out, err := cmd.CombinedOutput() if err != nil { br := color.Colorize("!r") rst := color.Colorize("|") fmt.Printf(color.Sprint(msg)+"\n%s\n%s%s%s\n\n", out, br, err, rst) } else { fmt.Printf(color.Sprint(msg)+"\n%s\n", out) } if !seq { quit <- 0 } } if *sequential { a(path, vcs, vcs_verb, *sequential) } else { count += 1 go a(path, vcs, vcs_verb, *sequential) } }
func (mp *CustomMediaPlayer) PlayFile(file string) error { var command exec.Cmd command.Path = mp.Executable mp.Args[len(mp.Args)-1] = file command.Args = mp.Args command.Start() return nil }
func (r *RealCommandRunner) resolve(cmd *exec.Cmd) *exec.Cmd { originalPath := cmd.Path path, err := exec.LookPath(cmd.Path) if err != nil { path = cmd.Path } cmd.Path = path cmd.Args = append([]string{originalPath}, cmd.Args...) return cmd }
func (vocabulary *Vocabulary) CreateCovarianceMatrix() { vocabulary.Covariances = new(CovarianceStruct) args := []string{"wnCovarianceSvsH.py", "--vocabulary"} args = append(args, vocabulary.Dictionary.VList...) args = append(args, "--pos") args = append(args, vocabulary.Dictionary.POSList...) cmd := new(exec.Cmd) cmd.Args = args // cmd.Path = "/home/lea/Code/Python/wnCovariance.py" cmd.Path = "/local/lea/thesis/python/wnCovarianceSvsH.py" out, err := cmd.Output() if err != nil { fmt.Println(err) } vocabulary.parse(string(out)) }
func main() { args := parseFlags(os.Args) if unset != "" { os.Unsetenv(unset) } cmd := exec.Cmd{Env: env} // Check for "-" as an argument, because it means the same as "-i" if flag.Arg(0) == "-" { cmd.Env = []string{} } for i, arg := range args { if strings.Index(arg, "=") > 0 { cmd.Env = append(cmd.Env, arg) } else if arg != "-" { if nullEol { fatal.Fatalln("cannot specify --null (-0) with command") } cmd.Path = arg cmd.Args = append(cmd.Args, args[i:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := execvp(cmd); err != nil { fatal.Fatalln(err) } return } } eol := '\n' if nullEol { eol = '\x00' } for _, e := range env { fmt.Printf("%s%c", e, eol) } return }
func (p *plugin) fetchMailqCount() (count uint64, err error) { var path string if p.path != "" { path = p.path } else { path, err = exec.LookPath(p.mailq.command) if err != nil { return } } cmd := exec.Cmd{ Path: path, Args: append([]string{p.mailq.command}, p.mailq.args...), } if p.path != "" { cmd.Path = p.path } stdout, err := cmd.StdoutPipe() if err != nil { return } err = cmd.Start() if err != nil { return } count, err = p.mailq.parse(stdout) if err != nil { cmd.Wait() return } err = cmd.Wait() return }
func cmd_to_channel(argv []string, name string, out chan string) { var cmd exec.Cmd cmd.Path = argv[0] cmd.Args = argv log.Printf("starting '%s'\n", name) stdout, err := cmd.StdoutPipe() if err != nil { log.Println(err) } stderr, err := cmd.StderrPipe() if err != nil { log.Println(err) } if err := cmd.Start(); err != nil { log.Fatal(err) } mux_readers_into_channel(stdout, stderr, out) cmd.Wait() log.Printf("'%s' finished\n", name) }
// Pipe mail to an external command (E.g. sendmail -t) func execSend(payload []byte, execCmd string) { sendmail := new(exec.Cmd) sendmail.Args = strings.Fields(execCmd) sendmail.Path = sendmail.Args[0] stdin, err := sendmail.StdinPipe() if err != nil { panic(err) } defer stdin.Close() sendmail.Stdout = os.Stdout sendmail.Stderr = os.Stderr err = sendmail.Start() if err != nil { panic(err) } stdin.Write(payload) stdin.Close() err = sendmail.Wait() if err != nil { //Warn.Printf("%s: %s", execCmd, err) panic(err) } }
// collectGitInfo runs several git commands to compose a Git object. func collectGitInfo() *Git { gitCmds := map[string][]string{ "id": {"git", "rev-parse", "HEAD"}, "branch": {"git", "rev-parse", "--abbrev-ref", "HEAD"}, "aname": {"git", "log", "-1", "--pretty=%aN"}, "aemail": {"git", "log", "-1", "--pretty=%aE"}, "cname": {"git", "log", "-1", "--pretty=%cN"}, "cemail": {"git", "log", "-1", "--pretty=%cE"}, "message": {"git", "log", "-1", "--pretty=%s"}, "remotes": {"git", "remote", "-v"}, } results := map[string]string{} remotes := map[string]Remote{} gitPath, err := exec.LookPath("git") if err != nil { log.Fatal(err) } for key, args := range gitCmds { cmd := exec.Cmd{} cmd.Path = gitPath cmd.Args = args cmd.Stderr = os.Stderr ret, err := cmd.Output() if err != nil { log.Fatal(err) } s := string(ret) s = strings.TrimRight(s, "\n") results[key] = s } for _, line := range strings.Split(results["remotes"], "\n") { matches := remotesRE.FindAllStringSubmatch(line, -1) if len(matches) != 1 { continue } if len(matches[0]) != 3 { continue } name := matches[0][1] url := matches[0][2] r := Remote{ Name: name, Url: url, } remotes[name] = r } h := Head{} h.Id = results["id"] h.AuthorName = results["aname"] h.AuthorEmail = results["aemail"] h.CommitterName = results["cname"] h.CommitterEmail = results["cemail"] h.Message = results["message"] g := Git{} g.Head = h g.Branch = results["branch"] for _, r := range remotes { g.Remotes = append(g.Remotes, &r) } return &g }
// Info runs several git commands to compose a Git object. func Info() *Git { gitCmds := map[string][]string{ "id": {"git", "rev-parse", "HEAD"}, "branch": {"git", "rev-parse", "--abbrev-ref", "HEAD"}, "aname": {"git", "log", "-1", "--pretty=%aN"}, "aemail": {"git", "log", "-1", "--pretty=%aE"}, "cname": {"git", "log", "-1", "--pretty=%cN"}, "cemail": {"git", "log", "-1", "--pretty=%cE"}, "message": {"git", "log", "-1", "--pretty=%s"}, "remotes": {"git", "remote", "-v"}, } results := map[string]string{} remotes := map[string]Remote{} gitPath, err := exec.LookPath("git") if err != nil { log.Fatal(err) } for key, args := range gitCmds { if key == "branch" { if envBranch := os.Getenv("GIT_BRANCH"); envBranch != "" { results[key] = envBranch continue } } cmd := exec.Cmd{} cmd.Path = gitPath cmd.Args = args cmd.Stderr = os.Stderr ret, err := cmd.Output() if err != nil { log.Fatal(err) } s := string(ret) s = strings.TrimRight(s, "\n") results[key] = s } for _, line := range strings.Split(results["remotes"], "\n") { matches := remotesRE.FindAllStringSubmatch(line, -1) if len(matches) != 1 { continue } if len(matches[0]) != 3 { continue } name := matches[0][1] url := matches[0][2] r := Remote{ Name: name, URL: url, } remotes[name] = r } h := Head{ ID: results["id"], AuthorName: results["aname"], AuthorEmail: results["aemail"], CommitterName: results["cname"], CommitterEmail: results["cemail"], Message: results["message"], } g := &Git{ Head: h, Branch: results["branch"], } for _, r := range remotes { g.Remotes = append(g.Remotes, &r) } return g }