Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 1b7a668

Browse files
committed
Allow modifying default host/schema used in sail run
1 parent 26aa19c commit 1b7a668

File tree

5 files changed

+82
-15
lines changed

5 files changed

+82
-15
lines changed

config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ func resolvePath(homedir string, path string) string {
3232
// config describes the config.toml.
3333
// Changes to this should be accompanied by changes to DefaultConfig.
3434
type config struct {
35-
DefaultImage string `toml:"default_image"`
36-
ProjectRoot string `toml:"project_root"`
37-
DefaultHat string `toml:"default_hat"`
35+
DefaultImage string `toml:"default_image"`
36+
ProjectRoot string `toml:"project_root"`
37+
DefaultHat string `toml:"default_hat"`
38+
DefaultSchema string `toml:"default_schema"`
39+
DefaultHost string `toml:"default_host"`
3840
}
3941

4042
const DefaultConfig = `# sail configuration.
@@ -47,6 +49,12 @@ project_root = "~/Projects"
4749
4850
# default hat lets you configure a hat that's applied automatically by default.
4951
# default_hat = ""
52+
53+
# default schema used to clone repo in sail run if none given
54+
default_schema = "ssh"
55+
56+
# default host used to clone repo in sail run if none given
57+
default_host = "github.com"
5058
`
5159

5260
// metaRoot returns the root path of all metadata stored on the host.

editcmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *editcmd) Spec() cli.CommandSpec {
4343
}
4444

4545
func (c *editcmd) Run(fl *flag.FlagSet) {
46-
proj := c.gf.project(fl)
46+
proj := c.gf.project(schemaPrefs{}, fl)
4747

4848
c.gf.ensureDockerDaemon()
4949

globalflags.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,39 @@ func (gf *globalFlags) ensureDockerDaemon() {
3737
gf.debug("verified Docker is running")
3838
}
3939

40-
func requireRepo(fl *flag.FlagSet) repo {
40+
func requireRepo(conf config, prefs schemaPrefs, fl *flag.FlagSet) repo {
4141
repoURI := fl.Arg(0)
4242
if repoURI == "" {
4343
flog.Fatal("Argument <repo> must be provided.")
4444
}
4545

46-
r, err := parseRepo("ssh", repoURI)
46+
r, err := parseRepo(defaultSchema(conf, prefs), repoURI)
4747
if err != nil {
4848
flog.Fatal("failed to parse repo %q: %v", repoURI, err)
4949
}
5050
return r
5151
}
5252

53+
func defaultSchema(conf config, prefs schemaPrefs) string {
54+
switch {
55+
case prefs.ssh:
56+
return "ssh"
57+
case prefs.https:
58+
return "https"
59+
case prefs.http:
60+
return "http"
61+
case conf.DefaultSchema != "":
62+
return conf.DefaultSchema
63+
default:
64+
return "ssh"
65+
}
66+
}
67+
5368
// project reads the project as the first parameter.
54-
func (gf *globalFlags) project(fl *flag.FlagSet) *project {
69+
func (gf *globalFlags) project(prefs schemaPrefs, fl *flag.FlagSet) *project {
70+
conf := gf.config()
5571
return &project{
56-
conf: gf.config(),
57-
repo: requireRepo(fl),
72+
conf: conf,
73+
repo: requireRepo(conf, prefs, fl),
5874
}
5975
}

runcmd.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ type runcmd struct {
2121
hat string
2222
keep bool
2323
testCmd string
24+
25+
schemaPrefs
26+
}
27+
28+
type schemaPrefs struct {
29+
ssh bool
30+
http bool
31+
https bool
2432
}
2533

2634
func (c *runcmd) Spec() cli.CommandSpec {
@@ -32,7 +40,41 @@ If a project is not yet created or running with the name,
3240
one will be created and a new editor will be opened.
3341
If a project is already up and running, this won't
3442
start a new container, but instead will reuse the
35-
already running container and open a new editor.`,
43+
already running container and open a new editor.
44+
45+
If a schema and host are not provided, sail will use github over SSH.
46+
There are multiple ways to modify this behavior.
47+
48+
1. Specify a host. See examples section
49+
2. Specify a schema and host. See examples section
50+
3. Edit the config to provide your preferred defaults.
51+
52+
Examples:
53+
Use default host and schema (github.com over SSH, editable in config)
54+
- sail run cdr/code-server
55+
56+
Force SSH on a Github repo (user git is assumed by default)
57+
- sail run ssh://github.com/cdr/sshcode
58+
- sail run --ssh github.com/cdr/sshcode
59+
60+
Specify a custom SSH user
61+
- sail run ssh://[email protected]/super/secret-repo
62+
- sail run --ssh [email protected]/super/secret-repo
63+
64+
Force HTTPS on a Gitlab repo
65+
- sail run https://gitlab.com/inkscape/inkscape
66+
- sail run --https gitlab.com/inkscape/inkscape
67+
68+
Note:
69+
If you use ssh://, http://, or https://, you must specify a host.
70+
71+
This won't work:
72+
- sail run ssh://cdr/code-server
73+
74+
Instead, use flags to avoid providing a host.
75+
76+
This will work:
77+
- sail run --ssh cdr/code-server`,
3678
}
3779
}
3880

@@ -41,17 +83,18 @@ func (c *runcmd) RegisterFlags(fl *flag.FlagSet) {
4183
fl.StringVar(&c.hat, "hat", "", "Custom hat to use.")
4284
fl.BoolVar(&c.keep, "keep", false, "Keep container when it fails to build.")
4385
fl.StringVar(&c.testCmd, "test-cmd", "", "A command to use in-place of starting code-server for testing purposes.")
86+
87+
fl.BoolVar(&c.ssh, "ssh", false, "Clone repo over SSH")
88+
fl.BoolVar(&c.http, "http", false, "Clone repo over HTTP")
89+
fl.BoolVar(&c.https, "https", false, "Clone repo over HTTPS")
4490
}
4591

4692
const guestHomeDir = "/home/user"
4793

4894
func (c *runcmd) Run(fl *flag.FlagSet) {
49-
var (
50-
err error
51-
)
5295
c.gf.ensureDockerDaemon()
5396

54-
proj := c.gf.project(fl)
97+
proj := c.gf.project(c.schemaPrefs, fl)
5598

5699
// Abort if container already exists.
57100
exists, err := proj.cntExists()

shellcmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *shellcmd) Spec() cli.CommandSpec {
2424
}
2525

2626
func (c *shellcmd) Run(fl *flag.FlagSet) {
27-
proj := c.gf.project(fl)
27+
proj := c.gf.project(schemaPrefs{}, fl)
2828
c.gf.ensureDockerDaemon()
2929

3030
out, err := dockutil.FmtExec(proj.cntName(), "grep ^.*:.*:$(id -u): /etc/passwd | cut -d : -f 7-").CombinedOutput()

0 commit comments

Comments
 (0)