Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
c2dea219
Commit
c2dea219
authored
Feb 19, 2010
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exec: add dir argument to Run.
fix, test MergeWithStdout R=r CC=golang-dev
https://golang.org/cl/214046
parent
4589c345
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
15 deletions
+52
-15
main.go
src/cmd/hgpatch/main.go
+2
-2
exec.go
src/pkg/exec/exec.go
+3
-3
exec_test.go
src/pkg/exec/exec_test.go
+47
-10
No files found.
src/cmd/hgpatch/main.go
View file @
c2dea219
...
@@ -346,12 +346,12 @@ func run(argv []string, input []byte) (out string, err os.Error) {
...
@@ -346,12 +346,12 @@ func run(argv []string, input []byte) (out string, err os.Error) {
// fmt.Fprintf(os.Stderr, "%v\n", argv);
// fmt.Fprintf(os.Stderr, "%v\n", argv);
var
cmd
*
exec
.
Cmd
var
cmd
*
exec
.
Cmd
if
len
(
input
)
==
0
{
if
len
(
input
)
==
0
{
cmd
,
err
=
exec
.
Run
(
prog
,
argv
,
os
.
Environ
(),
exec
.
DevNull
,
exec
.
Pipe
,
exec
.
MergeWithStdout
)
cmd
,
err
=
exec
.
Run
(
prog
,
argv
,
os
.
Environ
(),
""
,
exec
.
DevNull
,
exec
.
Pipe
,
exec
.
MergeWithStdout
)
if
err
!=
nil
{
if
err
!=
nil
{
goto
Error
goto
Error
}
}
}
else
{
}
else
{
cmd
,
err
=
exec
.
Run
(
prog
,
argv
,
os
.
Environ
(),
exec
.
Pipe
,
exec
.
Pipe
,
exec
.
MergeWithStdout
)
cmd
,
err
=
exec
.
Run
(
prog
,
argv
,
os
.
Environ
(),
""
,
exec
.
Pipe
,
exec
.
Pipe
,
exec
.
MergeWithStdout
)
if
err
!=
nil
{
if
err
!=
nil
{
goto
Error
goto
Error
}
}
...
...
src/pkg/exec/exec.go
View file @
c2dea219
...
@@ -78,7 +78,7 @@ func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
...
@@ -78,7 +78,7 @@ func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
// of the returned Cmd is the other end of the pipe.
// of the returned Cmd is the other end of the pipe.
// Otherwise the field in Cmd is nil.
// Otherwise the field in Cmd is nil.
func
Run
(
argv0
string
,
argv
,
envv
[]
string
,
stdin
,
stdout
,
stderr
int
)
(
p
*
Cmd
,
err
os
.
Error
)
{
func
Run
(
argv0
string
,
argv
,
envv
[]
string
,
dir
string
,
stdin
,
stdout
,
stderr
int
)
(
p
*
Cmd
,
err
os
.
Error
)
{
p
=
new
(
Cmd
)
p
=
new
(
Cmd
)
var
fd
[
3
]
*
os
.
File
var
fd
[
3
]
*
os
.
File
...
@@ -89,13 +89,13 @@ func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd,
...
@@ -89,13 +89,13 @@ func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd,
goto
Error
goto
Error
}
}
if
stderr
==
MergeWithStdout
{
if
stderr
==
MergeWithStdout
{
p
.
Stderr
=
p
.
Stdout
fd
[
2
]
=
fd
[
1
]
}
else
if
fd
[
2
],
p
.
Stderr
,
err
=
modeToFiles
(
stderr
,
2
);
err
!=
nil
{
}
else
if
fd
[
2
],
p
.
Stderr
,
err
=
modeToFiles
(
stderr
,
2
);
err
!=
nil
{
goto
Error
goto
Error
}
}
// Run command.
// Run command.
p
.
Pid
,
err
=
os
.
ForkExec
(
argv0
,
argv
,
envv
,
""
,
&
fd
)
p
.
Pid
,
err
=
os
.
ForkExec
(
argv0
,
argv
,
envv
,
dir
,
&
fd
)
if
err
!=
nil
{
if
err
!=
nil
{
goto
Error
goto
Error
}
}
...
...
src/pkg/exec/exec_test.go
View file @
c2dea219
...
@@ -11,39 +11,76 @@ import (
...
@@ -11,39 +11,76 @@ import (
)
)
func
TestRunCat
(
t
*
testing
.
T
)
{
func
TestRunCat
(
t
*
testing
.
T
)
{
cmd
,
err
:=
Run
(
"/bin/cat"
,
[]
string
{
"cat"
},
nil
,
cmd
,
err
:=
Run
(
"/bin/cat"
,
[]
string
{
"cat"
},
nil
,
""
,
Pipe
,
Pipe
,
DevNull
)
Pipe
,
Pipe
,
DevNull
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"opencmd /bin/cat: %v
"
,
err
)
t
.
Fatal
(
"run:
"
,
err
)
}
}
io
.
WriteString
(
cmd
.
Stdin
,
"hello, world
\n
"
)
io
.
WriteString
(
cmd
.
Stdin
,
"hello, world
\n
"
)
cmd
.
Stdin
.
Close
()
cmd
.
Stdin
.
Close
()
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stdout
)
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stdout
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"reading from /bin/cat: %v
"
,
err
)
t
.
Fatal
(
"read:
"
,
err
)
}
}
if
string
(
buf
)
!=
"hello, world
\n
"
{
if
string
(
buf
)
!=
"hello, world
\n
"
{
t
.
Fatalf
(
"read
ing from /bin/cat
: got %q"
,
buf
)
t
.
Fatalf
(
"read: got %q"
,
buf
)
}
}
if
err
=
cmd
.
Close
();
err
!=
nil
{
if
err
=
cmd
.
Close
();
err
!=
nil
{
t
.
Fatal
f
(
"closing /bin/cat: %v
"
,
err
)
t
.
Fatal
(
"close:
"
,
err
)
}
}
}
}
func
TestRunEcho
(
t
*
testing
.
T
)
{
func
TestRunEcho
(
t
*
testing
.
T
)
{
cmd
,
err
:=
Run
(
"/bin/echo"
,
[]
string
{
"echo"
,
"hello"
,
"world"
},
nil
,
cmd
,
err
:=
Run
(
"/bin/echo"
,
[]
string
{
"echo"
,
"hello"
,
"world"
},
nil
,
""
,
DevNull
,
Pipe
,
DevNull
)
DevNull
,
Pipe
,
DevNull
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"opencmd /bin/echo: %v
"
,
err
)
t
.
Fatal
(
"run:
"
,
err
)
}
}
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stdout
)
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stdout
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
f
(
"reading from /bin/echo: %v
"
,
err
)
t
.
Fatal
(
"read:
"
,
err
)
}
}
if
string
(
buf
)
!=
"hello world
\n
"
{
if
string
(
buf
)
!=
"hello world
\n
"
{
t
.
Fatalf
(
"read
ing from /bin/echo
: got %q"
,
buf
)
t
.
Fatalf
(
"read: got %q"
,
buf
)
}
}
if
err
=
cmd
.
Close
();
err
!=
nil
{
if
err
=
cmd
.
Close
();
err
!=
nil
{
t
.
Fatalf
(
"closing /bin/echo: %v"
,
err
)
t
.
Fatal
(
"close:"
,
err
)
}
}
func
TestStderr
(
t
*
testing
.
T
)
{
cmd
,
err
:=
Run
(
"/bin/sh"
,
[]
string
{
"sh"
,
"-c"
,
"echo hello world 1>&2"
},
nil
,
""
,
DevNull
,
DevNull
,
Pipe
)
if
err
!=
nil
{
t
.
Fatal
(
"run:"
,
err
)
}
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stderr
)
if
err
!=
nil
{
t
.
Fatal
(
"read:"
,
err
)
}
if
string
(
buf
)
!=
"hello world
\n
"
{
t
.
Fatalf
(
"read: got %q"
,
buf
)
}
if
err
=
cmd
.
Close
();
err
!=
nil
{
t
.
Fatal
(
"close:"
,
err
)
}
}
func
TestMergeWithStdout
(
t
*
testing
.
T
)
{
cmd
,
err
:=
Run
(
"/bin/sh"
,
[]
string
{
"sh"
,
"-c"
,
"echo hello world 1>&2"
},
nil
,
""
,
DevNull
,
Pipe
,
MergeWithStdout
)
if
err
!=
nil
{
t
.
Fatal
(
"run:"
,
err
)
}
buf
,
err
:=
ioutil
.
ReadAll
(
cmd
.
Stdout
)
if
err
!=
nil
{
t
.
Fatal
(
"read:"
,
err
)
}
if
string
(
buf
)
!=
"hello world
\n
"
{
t
.
Fatalf
(
"read: got %q"
,
buf
)
}
if
err
=
cmd
.
Close
();
err
!=
nil
{
t
.
Fatal
(
"close:"
,
err
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment