示例#1
0
func ExecCmd(shell *wsman.Shell, cmd string, out, err io.Writer) error {
	cmd_id, e := shell.NewCommand(cmd, nil)
	if nil != e {
		return e
	}
	defer func() {
		if e := shell.Signal(cmd_id, wsman.SIGNAL_TERMINATE); nil != e {
			if !strings.Contains(e.Error(), "The parameter is incorrect.") {
				fmt.Println("[error] terminate", e)
			}
		}
	}()

	for {
		res, e := shell.Read(cmd_id)
		if e != nil {
			return e
		}
		if nil != err {
			for _, bs := range res.Stderr {
				err.Write(bs)
			}
		}
		if nil != out {
			for _, bs := range res.Stdout {
				out.Write(bs)
			}
		}
		if res.IsDone() {
			if res.ExitCode != "" && res.ExitCode != "0" {
				return errors.New("exit with " + res.ExitCode)
			}
			return nil
		}
	}
}
示例#2
0
文件: main.go 项目: runner-mei/wsman
func Exit(shell *wsman.Shell, exitCode int) {
	if nil != shell {
		shell.Close()
	}
	os.Exit(exitCode)
}