本文整理汇总了Golang中os/exec.Cmd.Dir方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.Dir方法的具体用法?Golang Cmd.Dir怎么用?Golang Cmd.Dir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os/exec.Cmd
的用法示例。
在下文中一共展示了Cmd.Dir方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: cleanup
func cleanup(directory string) {
temp, err := ioutil.TempDir("", "")
isError(err, "")
files, err := ioutil.ReadDir(directory)
isError(err, "")
if 1 == len(files) {
folder := filepath.Join(directory, files[0].Name())
// Move to a tmp directory
files, err = ioutil.ReadDir(folder)
isError(err, "")
for _, file := range files {
var cmd *exec.Cmd
cmd = exec.Command("mv", file.Name(), temp)
cmd.Dir = folder
output, err := cmd.CombinedOutput()
isError(err, string(output))
}
err = os.RemoveAll(folder)
isError(err, "")
// move to the directory
files, err = ioutil.ReadDir(temp)
isError(err, "")
for _, file := range files {
var cmd *exec.Cmd
cmd = exec.Command("mv", file.Name(), directory)
cmd.Dir = temp
output, err := cmd.CombinedOutput()
isError(err, string(output))
}
}
}
示例2: Build
func (r *AgRecipe) Build(ctx *types.BuildContext) error {
log.Info("Building the_silver_searcher")
srcdir := r.UnpackedDir(ctx, r.Info())
var cmd *exec.Cmd
// Run autotools
cmd = exec.Command("autoreconf", "-i")
cmd.Dir = srcdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not run autotools")
return err
}
log.Infof("Running ./configure")
cmd = exec.Command(
"./configure",
"--host="+ctx.CrossPrefix,
"--build=i686",
"PKG_CONFIG=/bin/true",
)
cmd.Dir = srcdir
cmd.Env = ctx.Env.
Append("CC", ctx.StaticFlags).
Set("CFLAGS", "-fPIC "+ctx.StaticFlags).
Set("PCRE_LIBS", ctx.DependencyEnv["pcre"]["LDFLAGS"]).
Set("PCRE_CFLAGS", ctx.DependencyEnv["pcre"]["CFLAGS"]).
Set("LZMA_LIBS", ctx.DependencyEnv["lzma"]["LDFLAGS"]).
Set("LZMA_CFLAGS", ctx.DependencyEnv["lzma"]["CFLAGS"]).
Set("ZLIB_LIBS", ctx.DependencyEnv["zlib"]["LDFLAGS"]).
Set("ZLIB_CFLAGS", ctx.DependencyEnv["zlib"]["CFLAGS"]).
AsSlice()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not run configure")
return err
}
cmd = exec.Command("make")
cmd.Dir = srcdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not run make")
return err
}
log.Info("Finished building the_silver_searcher")
return nil
}
示例3: Build
func (r *PvRecipe) Build(ctx *types.BuildContext) error {
log.Info("Building pv")
srcdir := r.UnpackedDir(ctx, r.Info())
var cmd *exec.Cmd
// 1. Configure
log.Infof("Running ./configure")
cmd = exec.Command(
"./configure",
"--host="+ctx.CrossPrefix,
"--build=i686",
)
cmd.Dir = srcdir
cmd.Env = ctx.Env.
Append("CC", ctx.StaticFlags).
Set("CFLAGS", ctx.StaticFlags).
AsSlice()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not run configure")
return err
}
// 2. Fix linker in Makefile
cmd = exec.Command(
"sed",
"-i",
fmt.Sprintf("/^CC =/a LD = %s", ctx.Env.Get("LD")),
filepath.Join(srcdir, "Makefile"),
)
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not patch Makefile")
return err
}
// 3. Run build
cmd = exec.Command("make")
cmd.Dir = srcdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
log.WithField("err", err).Error("Could not run make")
return err
}
log.Info("Finished building pv")
return nil
}
示例4: TestDoAll_cached_sample
func TestDoAll_cached_sample(t *testing.T) {
cleanup("go-cache")
var gitCmd *exec.Cmd
var srcCmd *exec.Cmd
gitCmd = exec.Command("git", "checkout", "071610bf3a597bc41aae05e27c5407444b7ea0d1")
gitCmd.Dir = filepath.Join(testdataPath, "go-cached")
if o, err := gitCmd.CombinedOutput(); err != nil {
t.Fatal(string(o), err)
}
srcCmd = exec.Command("src", "do-all")
srcCmd.Dir = filepath.Join(testdataPath, "go-cached")
srcCmd.Env = append([]string{"SRCLIBPATH=" + srclibPath}, os.Environ()...)
if o, err := srcCmd.CombinedOutput(); err != nil {
t.Fatal(string(o), err)
}
gitCmd = exec.Command("git", "checkout", "34dd0f240fe12cdd8c9c6e24620cc0013518a55e")
gitCmd.Dir = filepath.Join(testdataPath, "go-cached")
if o, err := gitCmd.CombinedOutput(); err != nil {
t.Fatal(string(o), err)
}
srcCmd = exec.Command("src", "do-all")
srcCmd.Dir = filepath.Join(testdataPath, "go-cached")
srcCmd.Env = append([]string{"SRCLIBPATH=" + srclibPath}, os.Environ()...)
if o, err := srcCmd.CombinedOutput(); err != nil {
t.Fatal(string(o), err)
}
firstOne, err := ioutil.ReadFile(filepath.Join(testdataPath, "go-cached", buildstore.BuildDataDirName, "071610bf3a597bc41aae05e27c5407444b7ea0d1", "one", "sample.graph.json"))
if err != nil {
t.Fatal(err)
}
secondOne, err := ioutil.ReadFile(filepath.Join(testdataPath, "go-cached", buildstore.BuildDataDirName, "34dd0f240fe12cdd8c9c6e24620cc0013518a55e", "one", "sample.graph.json"))
if err != nil {
t.Fatal(err)
}
if string(firstOne) != string(secondOne) {
t.Error("Source unit \"one\" should have been cached: string(firstOne) != string(secondOne)")
}
firstTwo, err := ioutil.ReadFile(filepath.Join(testdataPath, "go-cached", buildstore.BuildDataDirName, "071610bf3a597bc41aae05e27c5407444b7ea0d1", "two", "sample.graph.json"))
if err != nil {
t.Fatal(err)
}
secondTwo, err := ioutil.ReadFile(filepath.Join(testdataPath, "go-cached", buildstore.BuildDataDirName, "34dd0f240fe12cdd8c9c6e24620cc0013518a55e", "two", "sample.graph.json"))
if err != nil {
t.Fatal(err)
}
if string(firstTwo) == string(secondTwo) {
t.Error("Source unit \"two\" should not be cached: string(firstTwo) == string(secondTwo)")
}
cleanup("go-cache")
}
示例5: Log
// 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
}
示例6: execute
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)
}
}
示例7: updateLocalRepo
func updateLocalRepo(url, dir string) (bool, error) {
var cmd *exec.Cmd
_, err := os.Stat(dir)
if os.IsNotExist(err) {
cmd = exec.Command("git", "clone", url, dir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return true, cmd.Run()
} else {
cmd = exec.Command("git", "pull", url)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return false, err
}
res := string(out)
fmt.Fprint(os.Stderr, res)
updated := true
if strings.Contains(res, "Already up-to-date.") {
updated = false
}
return updated, nil
}
}
示例8: fetchFullNDK
func fetchFullNDK() error {
url := "https://dl.google.com/android/ndk/android-" + ndkVersion + "-" + goos + "-" + ndkarch + "."
if goos == "windows" {
url += "exe"
} else {
url += "bin"
}
archive, err := fetch(url)
if err != nil {
return err
}
// The self-extracting ndk dist file for Windows terminates
// with an error (error code 2 - corrupted or incomplete file)
// but there are no details on what caused this.
//
// Strangely, if the file is launched from file browser or
// unzipped with 7z.exe no error is reported.
//
// In general we use the stripped NDK, so this code path
// is not used, and 7z.exe is not a normal dependency.
var inflate *exec.Cmd
if goos != "windows" {
// The downloaded archive is executed on linux and os x to unarchive.
// To do this execute permissions are needed.
os.Chmod(archive, 0755)
inflate = exec.Command(archive)
} else {
inflate = exec.Command("7z.exe", "x", archive)
}
inflate.Dir = tmpdir
return runCmd(inflate)
}
示例9: RunWithEnvAndWd
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
}
示例10: GetDiff
func GetDiff(repoPath, commitid string) (*Diff, error) {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
commit, err := repo.GetCommit(commitid)
if err != nil {
return nil, err
}
rd, wr := io.Pipe()
var cmd *exec.Cmd
// First commit of repository.
if commit.ParentCount() == 0 {
cmd = exec.Command("git", "show", commitid)
} else {
c, _ := commit.Parent(0)
cmd = exec.Command("git", "diff", c.Id.String(), commitid)
}
cmd.Dir = repoPath
cmd.Stdout = wr
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
go func() {
cmd.Run()
wr.Close()
}()
defer rd.Close()
return ParsePatch(cmd, rd)
}
示例11: ComplexCommand
//复杂命令 管道操作等
func ComplexCommand(command string, dir string) string {
if dir == "" {
dir = "/root/"
}
command = strings.TrimSpace(command)
if command == "" {
log.Println("lost command")
}
parts := strings.Fields(command)
var cmd *exec.Cmd
if len(parts) == 1 {
cmd = exec.Command(command)
} else {
cmd = exec.Command("/bin/bash", "-c", command)
}
cmd.Dir = dir
cmd.Env = []string{
"SHELL=/bin/bash",
"PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin",
"LC_ALL=en_US.UTF-8",
}
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
checkErr(err)
//fmt.Println(out.String())
return out.String()
}
示例12: SpecifiedCommand
//执行指定命令,简单命令,不包含管道操作、&&等
func SpecifiedCommand(command string, dir string) string {
if dir == "" {
dir = "/root/"
}
command = strings.TrimSpace(command)
if command == "" {
log.Println("lost command")
}
parts := strings.Fields(command)
var cmd *exec.Cmd
if len(parts) == 1 {
cmd = exec.Command(command)
} else {
commandFile := parts[0]
parts = parts[1:len(parts)]
commandSlice := []string{}
for _, param := range parts {
commandSlice = append(commandSlice, param)
}
cmd = exec.Command(commandFile, commandSlice...)
///usr/bin/wc -l /root/goproject/svntest/test/1.txt
//cmd = exec.Command("/usr/bin/wc", "-l", "/root/goproject/svntest/test/1.txt")
}
cmd.Dir = dir
env := "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin"
cmd.Env = strings.Split(env, ":")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
checkErr(err)
//fmt.Println(out.String())
return out.String()
}
示例13: Md5sumCheck
func Md5sumCheck(runUser, workdir, tarfile, md5file string) error {
var cmd *exec.Cmd
var md5Actual string
if "darwin" == runtime.GOOS {
cmd = BuildCommand(runUser, "md5", "-q", path.Join(workdir, tarfile))
} else {
cmd = BuildCommand(runUser, "md5sum", path.Join(workdir, tarfile))
}
cmd.Dir = file.SelfDir()
bs, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cd %s; md5sum -c %s fail", workdir, md5file)
}
strMd5file, _ := file.ToString(path.Join(workdir, md5file))
if "" == strMd5file {
return fmt.Errorf("md5file is empty")
}
if "darwin" == runtime.GOOS {
md5Actual = strings.Replace(string(bs), "\n", "", -1)
} else {
md5Actual = strings.Fields(string(bs))[0]
}
md5Except := strings.Fields(strMd5file)[0]
if md5Actual == md5Except {
return nil
}
return fmt.Errorf("md5Actual:%s, md5Except:%s<<<===end", md5Actual, md5Except)
}
示例14: Start
func (lc *LocalCommand) Start() error {
if lc.Shell == "" {
lc.Shell = "sh"
}
var cmd *exec.Cmd
if lc.ScriptMode {
cmd = exec.Command(lc.Shell)
cmd.Stdin = strings.NewReader(lc.CmdString)
} else {
cmd = exec.Command(lc.Shell, "-c", lc.CmdString)
}
// create the command, set the options
if lc.WorkingDirectory != "" {
cmd.Dir = lc.WorkingDirectory
}
cmd.Env = lc.Environment
if cmd.Env == nil {
cmd.Env = os.Environ()
}
cmd.Stdout = lc.Stdout
cmd.Stderr = lc.Stderr
// cache the command running
lc.Cmd = cmd
// start the command
return cmd.Start()
}
示例15: build
func (project *Project) build(runIT bool, debug bool) (int, error) {
app, err := exec.LookPath("mvn")
var cmd *exec.Cmd
if runIT {
cmd = exec.Command(app, "clean", "install", "-Pintegration-tests")
} else {
cmd = exec.Command(app, "clean", "install")
}
path := strings.Replace(project.FullPath, "pom.xml", "", 1)
cmd.Dir = path
stdout, err := cmd.StdoutPipe()
check(err)
err = cmd.Start()
check(err)
in := bufio.NewScanner(stdout)
for in.Scan() {
if debug {
log.Printf(in.Text())
}
}
err = cmd.Wait()
if err != nil {
log.Println(err)
return 1, err
}
return 0, nil
}