// NewGlobalOptions constructor func NewGlobalOptions(c util.Settings, e *util.Environment) (*GlobalOptions, error) { baseURL, _ := c.GlobalString("base-url", DEFAULT_BASE_URL) baseURL = strings.TrimRight(baseURL, "/") debug, _ := c.GlobalBool("debug") journal, _ := c.GlobalBool("journal") verbose, _ := c.GlobalBool("verbose") // TODO(termie): switch negative flag showColors, _ := c.GlobalBool("no-colors") showColors = !showColors authTokenStore, _ := c.GlobalString("auth-token-store") authTokenStore = util.ExpandHomePath(authTokenStore, e.Get("HOME")) authToken := guessAuthToken(c, e, authTokenStore) // If debug is true, than force verbose and do not use colors. if debug { verbose = true showColors = false } return &GlobalOptions{ BaseURL: baseURL, Debug: debug, Journal: journal, Verbose: verbose, ShowColors: showColors, AuthToken: authToken, AuthTokenStore: authTokenStore, }, nil }
func guessAndUpdateDockerOptions(opts *DockerOptions, e *util.Environment) { if opts.DockerHost != "" { return } logger := util.RootLogger().WithField("Logger", "docker") // f := &util.Formatter{opts.GlobalOptions.ShowColors} f := &util.Formatter{false} // Check the unix socket, default on linux // This will fail instantly so don't bother with the goroutine unixSocket := "/var/run/docker.sock" logger.Println(f.Info("No Docker host specified, checking", unixSocket)) if _, err := os.Stat(unixSocket); err == nil { unixSocket = fmt.Sprintf("unix://%s", unixSocket) client, err := NewDockerClient(&DockerOptions{ DockerHost: unixSocket, }) if err == nil { _, err = client.Version() if err == nil { opts.DockerHost = unixSocket return } } } // Check the boot2docker port with default cert paths and such b2dCertPath := filepath.Join(e.Get("HOME"), ".boot2docker/certs/boot2docker-vm") b2dHost := "tcp://192.168.59.103:2376" logger.Printf(f.Info("No Docker host specified, checking for boot2docker", b2dHost)) client, err := NewDockerClient(&DockerOptions{ DockerHost: b2dHost, DockerCertPath: b2dCertPath, DockerTLSVerify: "1", }) if err == nil { // This can take a long time if it isn't up, so toss it in a // goroutine so we can time it out result := make(chan bool) go func() { _, err = client.Version() if err == nil { result <- true } else { result <- false } }() select { case success := <-result: if success { opts.DockerHost = b2dHost opts.DockerCertPath = b2dCertPath opts.DockerTLSVerify = "1" return } case <-time.After(1 * time.Second): } } // Pick a default localhost port and hope for the best :/ opts.DockerHost = "tcp://127.0.0.1:2375" logger.Println(f.Info("No Docker host found, falling back to default", opts.DockerHost)) }