當前位置: 首頁>>代碼示例>>Golang>>正文


Golang RunParams.Run方法代碼示例

本文整理匯總了Golang中github.com/juju/utils/exec.RunParams.Run方法的典型用法代碼示例。如果您正苦於以下問題:Golang RunParams.Run方法的具體用法?Golang RunParams.Run怎麽用?Golang RunParams.Run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/juju/utils/exec.RunParams的用法示例。


在下文中一共展示了RunParams.Run方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: RunCommands

// RunCommands exists to satisfy the Runner interface.
func (runner *runner) RunCommands(commands string) (*utilexec.ExecResponse, error) {
	srv, err := runner.startJujucServer()
	if err != nil {
		return nil, err
	}
	defer srv.Close()

	env, err := runner.context.HookVars(runner.paths)
	if err != nil {
		return nil, errors.Trace(err)
	}
	command := utilexec.RunParams{
		Commands:    commands,
		WorkingDir:  runner.paths.GetCharmDir(),
		Environment: env,
	}

	err = command.Run()
	if err != nil {
		return nil, err
	}
	runner.context.SetProcess(command.Process())

	// Block and wait for process to finish
	result, err := command.Wait()
	return result, runner.context.Flush("run commands", err)
}
開發者ID:snailwalker,項目名稱:juju,代碼行數:28,代碼來源:runner.go

示例2: TestKillAbortedIfUnsuccessfull

func (s *execSuite) TestKillAbortedIfUnsuccessfull(c *gc.C) {
	killCalled := false

	mockChan := make(chan time.Time, 1)
	defer close(mockChan)
	params := exec.RunParams{
		Commands:    "sleep 100",
		WorkingDir:  "",
		Environment: []string{},
		Clock:       &mockClock{C: mockChan},
		KillProcess: func(*os.Process) error {
			killCalled = true
			return nil
		},
	}

	err := params.Run()
	c.Assert(err, gc.IsNil)
	c.Assert(params.Process(), gc.Not(gc.IsNil))

	cancelChan := make(chan struct{}, 1)
	defer close(cancelChan)
	cancelChan <- struct{}{}
	mockChan <- time.Now()
	res, err := params.WaitWithCancel(cancelChan)
	c.Assert(err, gc.ErrorMatches, fmt.Sprintf("tried to kill process %d, but timed out", params.Process().Pid))
	c.Assert(res, gc.IsNil)
	c.Assert(killCalled, jc.IsTrue)
}
開發者ID:anastasiamac,項目名稱:utils,代碼行數:29,代碼來源:exec_test.go

示例3: startProcess

func (s *InterfaceSuite) startProcess(c *gc.C) *os.Process {
	command := exec.RunParams{
		Commands: "trap 'exit 0' SIGTERM; while true;do sleep 1;done",
	}
	err := command.Run()
	c.Assert(err, jc.ErrorIsNil)
	p := command.Process()
	s.AddCleanup(func(c *gc.C) { p.Kill() })
	return p
}
開發者ID:Pankov404,項目名稱:juju,代碼行數:10,代碼來源:context_test.go

示例4: TestWaitWithCancel

func (*execSuite) TestWaitWithCancel(c *gc.C) {
	params := exec.RunParams{
		Commands: "sleep 100",
		Clock:    &mockClock{C: make(chan time.Time)},
	}

	err := params.Run()
	c.Assert(err, gc.IsNil)
	c.Assert(params.Process(), gc.Not(gc.IsNil))

	cancelChan := make(chan struct{}, 1)
	defer close(cancelChan)
	cancelChan <- struct{}{}
	result, err := params.WaitWithCancel(cancelChan)
	c.Assert(err, gc.Equals, exec.ErrCancelled)
	c.Assert(string(result.Stdout), gc.Equals, "")
	c.Assert(string(result.Stderr), gc.Equals, "")
	c.Assert(result.Code, gc.Equals, cancelErrCode)
}
開發者ID:anastasiamac,項目名稱:utils,代碼行數:19,代碼來源:exec_test.go

示例5: runScript

// runScript executes the given lines in bash
func (c *Client) runScript(lines ...string) error {
	p := exec.RunParams{
		Commands: strings.Join(lines, "\n"),
	}

	os.MkdirAll(c.SccDir, 0755)
	p.WorkingDir = c.SccDir
	if err := p.Run(); err != nil {
		return err
	}

	if r, err := p.Wait(); err != nil {
		return err
	} else {
		os.Stderr.Write(r.Stderr)
		os.Stdout.Write(r.Stdout)
		if r.Code != 0 {
			return errors.New("Script failed")
		}

		return nil
	}
}
開發者ID:mever,項目名稱:steam,代碼行數:24,代碼來源:client.go

示例6: runCommandsWithTimeout

// runCommandsWithTimeout is a helper to abstract common code between run commands and
// juju-run as an action
func (runner *runner) runCommandsWithTimeout(commands string, timeout time.Duration, clock clock.Clock) (*utilexec.ExecResponse, error) {
	srv, err := runner.startJujucServer()
	if err != nil {
		return nil, err
	}
	defer srv.Close()

	env, err := runner.context.HookVars(runner.paths)
	if err != nil {
		return nil, errors.Trace(err)
	}
	command := utilexec.RunParams{
		Commands:    commands,
		WorkingDir:  runner.paths.GetCharmDir(),
		Environment: env,
		Clock:       clock,
	}

	err = command.Run()
	if err != nil {
		return nil, err
	}
	runner.context.SetProcess(hookProcess{command.Process()})

	var cancel chan struct{}
	if timeout != 0 {
		cancel = make(chan struct{})
		go func() {
			<-clock.After(timeout)
			close(cancel)
		}()
	}

	// Block and wait for process to finish
	return command.WaitWithCancel(cancel)
}
開發者ID:AlexisBruemmer,項目名稱:juju,代碼行數:38,代碼來源:runner.go

示例7: TestRunCommands

func (*execSuite) TestRunCommands(c *gc.C) {
	newDir, err := longPathAsString(c.MkDir())
	c.Assert(err, gc.IsNil)
	for i, test := range []struct {
		message     string
		commands    string
		workingDir  string
		environment []string
		stdout      string
		stderr      string
		code        int
	}{
		{
			message:  "test stdout capture",
			commands: "echo 'testing stdout'",
			stdout:   "testing stdout\r\n",
		}, {
			message:  "test stderr capture",
			commands: "Write-Error 'testing stderr'",
			stderr:   "testing stderr\r\n",
		}, {
			message:  "test return code",
			commands: "exit 42",
			code:     42,
		}, {
			message:    "test working dir",
			commands:   "(pwd).Path",
			workingDir: newDir,
			stdout:     filepath.FromSlash(newDir) + "\r\n",
		}, {
			message:     "test environment",
			commands:    "echo $env:OMG_IT_WORKS",
			environment: []string{"OMG_IT_WORKS=like magic"},
			stdout:      "like magic\r\n",
		},
	} {
		c.Logf("%v: %s", i, test.message)

		params := exec.RunParams{
			Commands:    test.commands,
			WorkingDir:  test.workingDir,
			Environment: test.environment,
		}

		result, err := exec.RunCommands(params)
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), jc.Contains, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)

		err = params.Run()
		c.Assert(err, gc.IsNil)
		c.Assert(params.Process(), gc.Not(gc.IsNil))
		result, err = params.Wait()
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), jc.Contains, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)

	}
}
開發者ID:rogpeppe,項目名稱:juju-utils,代碼行數:61,代碼來源:exec_windows_test.go

示例8: TestRunCommands

func (*execSuite) TestRunCommands(c *gc.C) {
	newDir := c.MkDir()

	for i, test := range []struct {
		message     string
		commands    string
		workingDir  string
		environment []string
		stdout      string
		stderr      string
		code        int
	}{
		{
			message:  "test stdout capture",
			commands: "echo testing stdout",
			stdout:   "testing stdout\n",
		}, {
			message:  "test stderr capture",
			commands: "echo testing stderr >&2",
			stderr:   "testing stderr\n",
		}, {
			message:  "test return code",
			commands: "exit 42",
			code:     42,
		}, {
			message:    "test working dir",
			commands:   "pwd",
			workingDir: newDir,
			stdout:     newDir + "\n",
		}, {
			message:     "test environment",
			commands:    "echo $OMG_IT_WORKS",
			environment: []string{"OMG_IT_WORKS=like magic"},
			stdout:      "like magic\n",
		}, {
			message:  "multiple commands",
			commands: "cat\necho 123",
			stdout:   "123\n",
		},
	} {
		c.Logf("%v: %s", i, test.message)

		params := exec.RunParams{
			Commands:    test.commands,
			WorkingDir:  test.workingDir,
			Environment: test.environment,
		}

		result, err := exec.RunCommands(params)
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), gc.Equals, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)

		err = params.Run()
		c.Assert(err, gc.IsNil)
		c.Assert(params.Process(), gc.Not(gc.IsNil))
		result, err = params.Wait()
		c.Assert(err, gc.IsNil)
		c.Assert(string(result.Stdout), gc.Equals, test.stdout)
		c.Assert(string(result.Stderr), gc.Equals, test.stderr)
		c.Assert(result.Code, gc.Equals, test.code)

	}
}
開發者ID:rogpeppe,項目名稱:juju-utils,代碼行數:65,代碼來源:exec_linux_test.go


注:本文中的github.com/juju/utils/exec.RunParams.Run方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。