本文整理汇总了Golang中exec.LookPath函数的典型用法代码示例。如果您正苦于以下问题:Golang LookPath函数的具体用法?Golang LookPath怎么用?Golang LookPath使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LookPath函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Compile
// Compile compiles and links sourcefile and atomically renames the
// resulting binary to runfile.
func Compile(sourcefile, runfile string, useGd bool) (err os.Error) {
pid := strconv.Itoa(os.Getpid())
content, err := ioutil.ReadFile(sourcefile)
if len(content) > 2 && content[0] == '#' && content[1] == '!' {
content[0] = '/'
content[1] = '/'
sourcefile = runfile + "." + pid + ".go"
ioutil.WriteFile(sourcefile, content, 0600)
defer os.Remove(sourcefile)
}
bindir := filepath.Join(runtime.GOROOT(), "bin")
if useGd {
gd := filepath.Join(bindir, "mgd")
if _, err := os.Stat(gd); err != nil {
if gd, err = exec.LookPath("mgd"); err != nil {
return os.NewError("can't find mgd")
}
}
err = Exec([]string{gd, "-L", "_obj", ".", "-q", "-o", runfile})
return err
} else {
n := TheChar()
gc := filepath.Join(bindir, n+"g")
ld := filepath.Join(bindir, n+"l")
if _, err := os.Stat(gc); err != nil {
if gc, err = exec.LookPath(n + "g"); err != nil {
return os.NewError("can't find " + n + "g")
}
}
if _, err := os.Stat(ld); err != nil {
if ld, err = exec.LookPath(n + "l"); err != nil {
return os.NewError("can't find " + n + "l")
}
}
gcout := runfile + "." + pid + "." + n
ldout := runfile + "." + pid
err = Exec([]string{gc, "-o", gcout, sourcefile})
if err != nil {
return err
}
defer os.Remove(gcout)
err = Exec([]string{ld, "-o", ldout, gcout})
if err != nil {
return err
}
return os.Rename(ldout, runfile)
}
panic("Unreachable code")
}
示例2: main
func main() {
flag.Parse()
cwd, _ := os.Getwd()
binary, _ := exec.LookPath(flag.Args()[0])
for {
cmd, _ := exec.Run(binary, flag.Args(), nil, cwd, exec.PassThrough, exec.PassThrough, exec.PassThrough)
pid := cmd.Process.Pid
extras := make(ABC, 0)
if *httpTimeoutUrl != "" {
extras = append(extras, setupHttpTimeoutCheck(pid))
}
if *httpStatusUrl != "" {
extras = append(extras, setupHttpStatusCheck(pid))
}
if *maxMemory > 0 {
extras = append(extras, setupMaxMemoryCheck(pid))
}
cmd.Wait(os.WSTOPPED)
println("Process died, restarting.")
extras.closeAll()
}
}
示例3: envOrDefault
func envOrDefault(cmd, dfl string) string {
p, err := exec.LookPath(cmd)
if err != nil {
return dfl
}
return p
}
示例4: ExecRead
func ExecRead(cmd string, args ...string) (out []byte, err os.Error) {
abscmd, err := exec.LookPath(cmd)
if err != nil {
return out, os.NewError("Couldn't find " + cmd + ": " + err.String())
}
cmdargs := make([]string, len(args)+1)
cmdargs[0] = cmd
for i, a := range args {
cmdargs[i+1] = a
}
printexec(cmd, args)
pid, err := exec.Run(abscmd, cmdargs, nil, "",
exec.PassThrough, exec.Pipe, exec.PassThrough)
if err != nil {
return
}
out, err = ioutil.ReadAll(pid.Stdout)
if err != nil {
return
}
ws, err := pid.Wait(0) // could have been os.WRUSAGE
if err != nil {
return
}
if ws.ExitStatus() != 0 {
err = os.NewError(cmd + " exited with status " + strconv.Itoa(ws.ExitStatus()))
}
return out, nil
}
示例5: run
// run runs the command argv, resolving argv[0] if necessary by searching $PATH.
// It provides input on standard input to the command.
func run(argv []string, input []byte) (out string, err os.Error) {
if len(argv) < 1 {
err = os.EINVAL
goto Error
}
prog, ok := lookPathCache[argv[0]]
if !ok {
prog, err = exec.LookPath(argv[0])
if err != nil {
goto Error
}
lookPathCache[argv[0]] = prog
}
cmd := exec.Command(prog, argv[1:]...)
if len(input) > 0 {
cmd.Stdin = bytes.NewBuffer(input)
}
bs, err := cmd.CombinedOutput()
if err == nil {
return string(bs), nil
}
Error:
err = &runError{dup(argv), err}
return
}
示例6: diff
func diff(b1, b2 []byte) (data []byte, err os.Error) {
f1, err := ioutil.TempFile("", "gofix")
if err != nil {
return nil, err
}
defer os.Remove(f1.Name())
defer f1.Close()
f2, err := ioutil.TempFile("", "gofix")
if err != nil {
return nil, err
}
defer os.Remove(f2.Name())
defer f2.Close()
f1.Write(b1)
f2.Write(b2)
diffcmd, err := exec.LookPath("diff")
if err != nil {
return nil, err
}
c, err := exec.Run(diffcmd, []string{"diff", f1.Name(), f2.Name()}, nil, "",
exec.DevNull, exec.Pipe, exec.MergeWithStdout)
if err != nil {
return nil, err
}
defer c.Close()
return ioutil.ReadAll(c.Stdout)
}
示例7: genRun
// genRun implements run and tryRun.
func genRun(dir string, stdin []byte, cmd []string, quiet bool) os.Error {
bin, err := exec.LookPath(cmd[0])
if err != nil {
return err
}
p, err := exec.Run(bin, cmd, os.Environ(), dir, exec.Pipe, exec.Pipe, exec.MergeWithStdout)
if *verbose {
fmt.Fprintf(os.Stderr, "%s: %s; %s %s\n", argv0, dir, bin, strings.Join(cmd[1:], " "))
}
if err != nil {
return err
}
go func() {
p.Stdin.Write(stdin)
p.Stdin.Close()
}()
var buf bytes.Buffer
io.Copy(&buf, p.Stdout)
io.Copy(&buf, p.Stdout)
w, err := p.Wait(0)
p.Close()
if !w.Exited() || w.ExitStatus() != 0 {
if !quiet || *verbose {
if dir != "" {
dir = "cd " + dir + "; "
}
fmt.Fprintf(os.Stderr, "%s: === %s%s\n", argv0, dir, strings.Join(cmd, " "))
os.Stderr.Write(buf.Bytes())
fmt.Fprintf(os.Stderr, "--- %s\n", w)
}
return os.ErrorString("running " + cmd[0] + ": " + w.String())
}
return nil
}
示例8: run
// run executes the specified command and returns its output and an error.
func run(cmd ...string) ([]byte, os.Error) {
// find the specified binary
bin, err := exec.LookPath(cmd[0])
if err != nil {
// report binary as well as the error
return nil, os.NewError(cmd[0] + ": " + err.String())
}
// run the binary and read its combined stdout and stderr into a buffer
p, err := exec.Run(bin, cmd, os.Environ(), "", exec.DevNull, exec.Pipe, exec.MergeWithStdout)
if err != nil {
return nil, err
}
var buf bytes.Buffer
io.Copy(&buf, p.Stdout)
w, err := p.Wait(0)
p.Close()
if err != nil {
return nil, err
}
// set the error return value if the program had a non-zero exit status
if !w.Exited() || w.ExitStatus() != 0 {
err = os.ErrorString("running " + cmd[0] + ": " + w.String())
}
return buf.Bytes(), err
}
示例9: mustRunDoozer
func mustRunDoozer(listen, web, attach string) *exec.Cmd {
exe, err := exec.LookPath("doozerd")
if err != nil {
panic(err)
}
args := []string{
"doozerd",
"-l=127.0.0.1:" + listen,
"-w=127.0.0.1:" + web,
}
if attach != "" {
args = append(args, "-a", "127.0.0.1:"+attach)
}
cmd, err := exec.Run(
exe,
args,
nil,
".",
exec.PassThrough,
exec.PassThrough,
exec.PassThrough,
)
if err != nil {
panic(err)
}
return cmd
}
示例10: TestRmRf
func TestRmRf(t *testing.T) {
t.Log("TestRmRf")
wd, clean := setupUfs(t)
defer clean()
err := os.MkdirAll(wd+"/ro/dir/subdir", 0755)
CheckSuccess(err)
contents := "hello"
fn := wd + "/ro/dir/subdir/y"
err = ioutil.WriteFile(fn, []byte(contents), 0644)
CheckSuccess(err)
bin, err := exec.LookPath("rm")
CheckSuccess(err)
cmd := exec.Command(bin, "-rf", wd+"/mount/dir")
err = cmd.Run()
if err != nil {
t.Fatal("rm -rf returned error:", err)
}
for _, f := range []string{"dir/subdir/y", "dir/subdir", "dir"} {
if fi, _ := os.Lstat(filepath.Join(wd, "mount", f)); fi != nil {
t.Errorf("file %s should have disappeared: %v", f, fi)
}
}
names, err := Readdirnames(wd + "/rw/DELETIONS")
CheckSuccess(err)
if len(names) != 3 {
t.Fatal("unexpected names", names)
}
}
示例11: run
func run(name string, args ...string) string {
path, err := exec.LookPath(name)
if err != nil {
panic(err)
}
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
p, err := exec.Run(path, append([]string{name}, args...), os.Environ(),
cwd, exec.Pipe, exec.Pipe, exec.Pipe)
if err != nil {
panic(err)
}
defer p.Close()
data, err := ioutil.ReadAll(p.Stdout)
if err != nil {
panic(err)
}
return strings.TrimSpace(string(data))
}
示例12: getProgramPath
func getProgramPath() (string, os.Error) {
program := os.Args[0]
dir, _ := path.Split(program)
if dir == "" {
binPath, pathError := exec.LookPath(program)
if pathError != nil {
if syscall.Access(program, syscall.O_RDONLY) != 0 {
return "", os.NewError("Path to " + program + " couldn't be found")
}
cwd, cwdError := os.Getwd()
if cwdError != nil {
return "", cwdError
}
return cwd, nil
}
binPath, _ = path.Split(binPath)
return binPath, nil
}
dir, _ = abspath(dir)
return dir, nil
}
示例13: main
func main() {
commands := []string{
`ls /home`,
`ls '/home'`,
`ls -l /home`,
`echo 'Hello, one and all!'`,
`which python`,
// Outputs "hello, world\n" instead of "\n"
`python -c print 'hello, world'`,
// Outputs "\n" instead of "hello, world\n"
`python -c "print 'hello, world'"`,
}
for ndx, c := range commands {
fmt.Printf("Command %d of %d: %s\n", ndx+1, len(commands), c)
// When I replace 3 with -1, the last 2 commands produce a Python error
argv := strings.Split(c, " ", 3)
bin, err := exec.LookPath(argv[0])
if cmd, e := exec.Run(bin, argv, nil, "", exec.DevNull, exec.Pipe, exec.MergeWithStdout); e == nil && err == nil {
body, _ := ioutil.ReadAll(cmd.Stdout)
fmt.Printf("%s\n", string(body))
} else {
fmt.Printf("e: %v", e)
fmt.Printf("err: %v", err)
}
}
}
示例14: CreateTestArgv
func CreateTestArgv() []string {
pwd, e := os.Getwd()
if e != nil {
log.Fatal("[ERROR] could not locate working directory\n")
}
argv := make([]string, 0)
if global.GetString("-backend") == "express" {
vmrun, e := exec.LookPath("vmrun")
if e != nil {
log.Fatalf("[ERROR] %s\n", e)
}
argv = append(argv, vmrun)
}
argv = append(argv, filepath.Join(pwd, global.GetString("-test-bin")))
if global.GetString("-bench") != "" {
argv = append(argv, "-test.bench")
argv = append(argv, global.GetString("-bench"))
}
if global.GetString("-match") != "" {
argv = append(argv, "-test.run")
argv = append(argv, global.GetString("-match"))
}
if global.GetBool("-verbose") {
argv = append(argv, "-test.v")
}
return argv
}
示例15: Format
// Format takes a markdown-formatted string and returns an equivalent
// HTML-formatted string.
//
// TODO write built-in markdown implementation, to avoid forking for every
// post
func Format(md string) (html string, err os.Error) {
cmdName, err := exec.LookPath("markdown")
if err != nil {
return
}
cmd, err := exec.Run(
cmdName,
[]string{},
os.Environ(),
".",
exec.Pipe,
exec.Pipe,
exec.PassThrough,
)
if err != nil {
return
}
cmd.Stdin.WriteString(md)
cmd.Stdin.Close()
b, err := ioutil.ReadAll(cmd.Stdout)
if err != nil {
return
}
html = string(b)
err = cmd.Close()
return
}