Cleanup from CR.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-04-22 12:37:48 -04:00
parent 91dd0c0c69
commit ef9ad85429
5 changed files with 66 additions and 37 deletions

View File

@ -1,11 +1,5 @@
package main package main
import (
"os"
"os/exec"
"syscall"
)
const daemonBinary = "dockerd" const daemonBinary = "dockerd"
// DaemonProxy acts as a cli.Handler to proxy calls to the daemon binary // DaemonProxy acts as a cli.Handler to proxy calls to the daemon binary
@ -15,29 +9,3 @@ type DaemonProxy struct{}
func NewDaemonProxy() DaemonProxy { func NewDaemonProxy() DaemonProxy {
return DaemonProxy{} return DaemonProxy{}
} }
// CmdDaemon execs dockerd with the same flags
// TODO: add a deprecation warning?
func (p DaemonProxy) CmdDaemon(args ...string) error {
args = stripDaemonArg(os.Args[1:])
binaryAbsPath, err := exec.LookPath(daemonBinary)
if err != nil {
return err
}
return syscall.Exec(
binaryAbsPath,
append([]string{daemonBinary}, args...),
os.Environ())
}
// stripDaemonArg removes the `daemon` argument from the list
func stripDaemonArg(args []string) []string {
for i, arg := range args {
if arg == "daemon" {
return append(args[:i], args[i+1:]...)
}
}
return args
}

37
daemon_unix.go Normal file
View File

@ -0,0 +1,37 @@
// +build !windows
package main
import (
"os"
"os/exec"
"syscall"
)
// CmdDaemon execs dockerd with the same flags
// TODO: add a deprecation warning?
func (p DaemonProxy) CmdDaemon(args ...string) error {
// Use os.Args[1:] so that "global" args are passed to dockerd
args = stripDaemonArg(os.Args[1:])
// TODO: check dirname args[0] first
binaryAbsPath, err := exec.LookPath(daemonBinary)
if err != nil {
return err
}
return syscall.Exec(
binaryAbsPath,
append([]string{daemonBinary}, args...),
os.Environ())
}
// stripDaemonArg removes the `daemon` argument from the list
func stripDaemonArg(args []string) []string {
for i, arg := range args {
if arg == "daemon" {
return append(args[:i], args[i+1:]...)
}
}
return args
}

11
daemon_windows.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"fmt"
)
// CmdDaemon reports on an error on windows, because there is no exec
func (p DaemonProxy) CmdDaemon(args ...string) error {
return fmt.Errorf(
"`docker daemon` does not exist on windows. Please run `dockerd` directly")
}

18
daemon_windows_test.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"strings"
"testing"
)
func TestCmdDaemon(t *testing.T) {
proxy := NewDaemonProxy()
err := proxy.CmdDaemon("--help")
if err == nil {
t.Fatal("Expected CmdDaemon to fail in Windows.")
}
if !strings.Contains(err.Error(), "Please run `dockerd`") {
t.Fatalf("Expected an error about running dockerd, got %s", err)
}
}

View File

@ -9,16 +9,11 @@ import (
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/dockerversion" "github.com/docker/docker/dockerversion"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/reexec"
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
) )
func main() { func main() {
if reexec.Init() {
return
}
// Set terminal emulation based on platform as required. // Set terminal emulation based on platform as required.
stdin, stdout, stderr := term.StdStreams() stdin, stdout, stderr := term.StdStreams()