This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
globalflags.go
160 lines (136 loc) · 3.64 KB
/
globalflags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/fatih/color"
"golang.org/x/xerrors"
"go.coder.com/flog"
)
type globalFlags struct {
verbose bool
configPath string
}
func (gf *globalFlags) debug(msg string, args ...interface{}) {
if !gf.verbose {
return
}
flog.Log(
flog.Level(color.New(color.FgHiMagenta).Sprint("DEBUG")),
msg, args...,
)
}
func (gf *globalFlags) config() config {
return mustReadConfig(gf.configPath)
}
// ensureDockerDaemon verifies that Docker is running.
func (gf *globalFlags) ensureDockerDaemon() {
// docker is installed in /usr/local/bin on MacOS, but this isn't in
// $PATH when launched by a browser that was opened via Finder.
if runtime.GOOS == "darwin" {
path := os.Getenv("PATH")
localBin := "/usr/local/bin"
if !strings.Contains(path, localBin) {
sep := fmt.Sprintf("%c", os.PathListSeparator)
os.Setenv("PATH", strings.Join([]string{path, localBin}, sep))
}
}
out, err := exec.Command("docker", "info").CombinedOutput()
if err != nil {
flog.Fatal("failed to run `docker info`: %v\n%s", err, out)
}
gf.debug("verified Docker is running")
}
func requireRepo(conf config, prefs schemaPrefs, fl *flag.FlagSet) repo {
var (
repoURI = strings.Join(fl.Args(), "/")
r repo
err error
)
if repoURI == "" {
flog.Fatal("Argument <repo> must be provided.")
}
// if this returns a non-empty string know it's pointing to a valid project on disk
// an error indicates an existing path outside of the project dir
repoName, err := pathIsRunnable(conf, repoURI)
if err != nil {
flog.Fatal(err.Error())
}
if repoName != "" {
// we only need the path since the repo exists on disk.
// there's not currently way for us to figure out the host anyways
r = repo{URL: &url.URL{Path: repoName}}
} else {
r, err = parseRepo(defaultSchema(conf, prefs), conf.DefaultHost, conf.DefaultOrganization, repoURI)
if err != nil {
flog.Fatal("failed to parse repo %q: %v", repoURI, err)
}
}
// check if path is pointing to a subdirectory
if sp := strings.Split(r.Path, "/"); len(sp) > 2 {
r.Path = strings.Join(sp[:2], "/")
r.subdir = strings.Join(sp[2:], "/")
}
return r
}
// pathIsRunnable returns the container name if the given path exists and is
// in the projects directory, else an empty string. An error is returned if
// and only if the path exists but it isn't in the user's project directory.
func pathIsRunnable(conf config, path string) (cnt string, _ error) {
fp, err := filepath.Abs(path)
if err != nil {
return
}
s, err := os.Stat(fp)
if err != nil {
return
}
if !s.IsDir() {
return
}
pre := expandRoot(conf.ProjectRoot)
if pre[len(pre)-1] != '/' {
pre = pre + "/"
}
// path exists but doesn't belong to projects directory, return error
if !strings.HasPrefix(fp, pre[:len(pre)-1]) {
return "", xerrors.Errorf("directory %s exists but isn't in projects directory", fp)
}
split := strings.Split(fp, "/")
if len(split) < 2 {
return
}
return strings.TrimPrefix(fp, pre), nil
}
func expandRoot(path string) string {
u, _ := user.Current()
return strings.Replace(path, "~/", u.HomeDir+"/", 1)
}
func defaultSchema(conf config, prefs schemaPrefs) string {
switch {
case prefs.ssh:
return "ssh"
case prefs.https:
return "https"
case prefs.http:
return "http"
case conf.DefaultSchema != "":
return conf.DefaultSchema
default:
return "ssh"
}
}
// project reads the project as the first parameter.
func (gf *globalFlags) project(prefs schemaPrefs, fl *flag.FlagSet) *project {
conf := gf.config()
return &project{
conf: conf,
repo: requireRepo(conf, prefs, fl),
}
}