本文整理汇总了Golang中os/exec.Cmd.Stdin方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.Stdin方法的具体用法?Golang Cmd.Stdin怎么用?Golang Cmd.Stdin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os/exec.Cmd
的用法示例。
在下文中一共展示了Cmd.Stdin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: start
func (s *sourceExec) start(cmd *exec.Cmd) error {
s.cmd = exec.Command(s.name, s.arg...)
r, err := s.cmd.StdoutPipe()
if err != nil {
return err
}
cmd.Stdin = r
if err := s.cmd.Start(); err != nil {
cmd.Stdin = nil
return err
}
return nil
}
示例2: doHighlight
func doHighlight(code, lexer string) string {
if lexer == "none" || lexer == "" {
lexer = "text"
}
defaults := []string{"-f", "html", "-O", "linenos=table,style=colorful,encoding=utf-8"}
var cmd *exec.Cmd
var args []string
if lexer == "autodetect" {
args = append(args, "-g")
} else {
args = append(args, "-l", lexer)
}
args = append(args, defaults...)
log.Debugf("Running Pygments with args: %v", args)
cmd = exec.Command(pygpath, args...)
cmd.Stdin = strings.NewReader(code)
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Errorf("Failed to run Pygments: %s. Stderr: %s", err, stderr.String())
return code
}
return out.String()
}
示例3: main
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
}
示例4: debug_cmd
func (ctx *Context) debug_cmd(cmd *exec.Cmd) {
if ctx.Verbose {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
}
示例5: Start
func (self *LocalCommand) Start() error {
var cmd *exec.Cmd
if self.ScriptMode {
cmd = exec.Command("sh")
cmd.Stdin = strings.NewReader(self.CmdString)
} else {
cmd = exec.Command("sh", "-c", self.CmdString)
}
// create the command, set the options
if self.WorkingDirectory != "" {
cmd.Dir = self.WorkingDirectory
}
cmd.Env = self.Environment
if cmd.Env == nil {
cmd.Env = os.Environ()
}
cmd.Stdout = self.Stdout
cmd.Stderr = self.Stderr
// cache the command running
self.Cmd = cmd
// start the command
return cmd.Start()
}
示例6: runPtyCmd
func (pipe *Pipe) runPtyCmd(cmd *exec.Cmd) error {
py, tty, err := pty.Open()
if err != nil {
return err
}
defer tty.Close()
env := os.Environ()
env = append(env, "TERM=xterm")
cmd.Env = env
cmd.Stdout = tty
cmd.Stdin = tty
cmd.Stderr = tty
cmd.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
ws := &Winsize{
Width: uint16(pipe.opts.Width),
Height: uint16(pipe.opts.Height),
}
_, _, syserr := syscall.Syscall(syscall.SYS_IOCTL, py.Fd(), uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
if syserr != 0 {
return syserr
}
pCmd := pipedCmd{
pipe: pipe,
cmd: cmd,
stdin: py,
stdout: py,
stderr: nil,
}
return pCmd.run()
}
示例7: 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()
}
示例8: Run
func (self *Login) Run() (err error) {
locationPathAbs, _ := filepath.Abs(ConvPath(LocationPath))
var buf bytes.Buffer
buf.WriteString(locationPathAbs)
buf.WriteString(ServerListPath)
list := NewList(buf.String())
buf.Reset()
if list.ExistServer(self.Name) {
server, _ := list.GetServer(self.Name)
var cmd *exec.Cmd
if server.UsePassword {
cmd = exec.Command("ssh", server.Host, "-l", server.LoginUser, "-p", strconv.Itoa(server.Port))
} else {
cmd = exec.Command("ssh", server.Host, "-l", server.LoginUser, "-p", strconv.Itoa(server.Port), "-i", ConvPath(server.IdentityFile))
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
} else {
help := NewHelp()
help.Run()
}
return
}
示例9: main
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
args := flag.Args()
ck(syscall.Chroot(args[0]))
ck(syscall.Chdir("/"))
var cmd *exec.Cmd
if len(args) == 1 {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
cmd = exec.Command(shell, "-i")
} else {
cmd = exec.Command(args[1], args[2:]...)
}
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
ck(cmd.Run())
}
示例10: verifyCertWithSystem
func verifyCertWithSystem(block *pem.Block, add func(*Certificate)) {
data := pem.EncodeToMemory(block)
var cmd *exec.Cmd
if needsTmpFiles() {
f, err := ioutil.TempFile("", "cert")
if err != nil {
fmt.Fprintf(os.Stderr, "can't create temporary file for cert: %v", err)
return
}
defer os.Remove(f.Name())
if _, err := f.Write(data); err != nil {
fmt.Fprintf(os.Stderr, "can't write temporary file for cert: %v", err)
return
}
if err := f.Close(); err != nil {
fmt.Fprintf(os.Stderr, "can't write temporary file for cert: %v", err)
return
}
cmd = exec.Command("/usr/bin/security", "verify-cert", "-c", f.Name(), "-l")
} else {
cmd = exec.Command("/usr/bin/security", "verify-cert", "-c", "/dev/stdin", "-l")
cmd.Stdin = bytes.NewReader(data)
}
if cmd.Run() == nil {
// Non-zero exit means untrusted
cert, err := ParseCertificate(block.Bytes)
if err != nil {
return
}
add(cert)
}
}
示例11: Run
func (h *Host) Run(command string) error {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", command)
} else {
cmd = exec.Command("sh", "-c", command)
}
realWriter := &RealWriter{
NewLine: true,
Prefix: "[" + h.Name + "]: ",
}
outWriter := &writer{
realWriter: realWriter,
Type: 1,
}
errWriter := &writer{
realWriter: realWriter,
Type: 2,
}
cmd.Stdout = outWriter
cmd.Stderr = errWriter
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
return err
}
return nil
}
示例12: 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)
}
示例13: RedirectIOTo
func RedirectIOTo(cmd *exec.Cmd, myin io.Reader, myout, myerr io.Writer) {
// redirect IO
cmd.Stdout = myout
cmd.Stderr = myerr
cmd.Stdin = myin
//return nil, err
}
示例14: start
func start(c *exec.Cmd) (pty *os.File, err error) {
pty, tty, err := open()
if err != nil {
return nil, err
}
defer tty.Close()
err = setEcho(pty, false)
if err != nil {
return nil, err
}
err = setEcho(tty, false)
if err != nil {
return nil, err
}
c.Stdout = tty
c.Stdin = tty
c.Stderr = tty
if c.SysProcAttr == nil {
c.SysProcAttr = &syscall.SysProcAttr{}
}
c.SysProcAttr.Setctty = true
c.SysProcAttr.Setsid = true
err = c.Start()
if err != nil {
pty.Close()
return nil, err
}
return pty, err
}
示例15: blastCoarse
func blastCoarse(
db *cablastp.DB, stdin *bytes.Reader, stdout *bytes.Buffer) error {
var cmd *exec.Cmd
if flagShortQueries {
cmd = exec.Command(
flagBlastn,
"-db", path.Join(db.Path, cablastp.FileBlastCoarse),
"-num_threads", s(flagGoMaxProcs),
"-max_target_seqs", "100000",
"-task", "blastn-short", "-evalue", sf(flagCoarseEval), "-penalty", "-1",
"-outfmt", "5", "-dbsize", su(db.BlastDBSize))
} else {
cmd = exec.Command(
flagBlastn,
"-db", path.Join(db.Path, cablastp.FileBlastCoarse),
"-num_threads", s(flagGoMaxProcs),
"-max_target_seqs", "100000",
"-evalue", sf(flagCoarseEval),
"-outfmt", "5", "-dbsize", su(db.BlastDBSize))
}
cmd.Stdin = stdin
cmd.Stdout = stdout
return cablastp.Exec(cmd)
}