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

Commit 7354999

Browse files
deansheatherNathan Potter
authored and
Nathan Potter
committed
add default_organization key to config (#218)
Automatically inserts the default_organization into repo URLs if there are no slashes in trimPath().
1 parent 7338504 commit 7354999

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

config.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ func resolvePath(homedir string, path string) string {
3131
// config describes the config.toml.
3232
// Changes to this should be accompanied by changes to DefaultConfig.
3333
type config struct {
34-
DefaultImage string `toml:"default_image"`
35-
ProjectRoot string `toml:"project_root"`
36-
DefaultHat string `toml:"default_hat"`
37-
DefaultSchema string `toml:"default_schema"`
38-
DefaultHost string `toml:"default_host"`
34+
DefaultImage string `toml:"default_image"`
35+
ProjectRoot string `toml:"project_root"`
36+
DefaultHat string `toml:"default_hat"`
37+
DefaultSchema string `toml:"default_schema"`
38+
DefaultHost string `toml:"default_host"`
39+
DefaultOrganization string `toml:"default_organization"`
3940
}
4041

42+
// DefaultConfig is the default configuration file string.
4143
const DefaultConfig = `# sail configuration.
4244
# default_image is the default Docker image to use if the repository provides none.
4345
default_image = "codercom/ubuntu-dev"
@@ -54,6 +56,10 @@ default_schema = "ssh"
5456
5557
# default host used to clone repo in sail run if none given
5658
default_host = "github.com"
59+
60+
# default_oranization lets you configure which username to use on default_host
61+
# when cloning a repo.
62+
# default_organization = ""
5763
`
5864

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

globalflags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func requireRepo(conf config, prefs schemaPrefs, fl *flag.FlagSet) repo {
4343
flog.Fatal("Argument <repo> must be provided.")
4444
}
4545

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

project_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_project(t *testing.T) {
5151
rb := newRollback()
5252
defer rb.run()
5353

54-
repo, err := parseRepo(test.schema, "github.com", test.repo)
54+
repo, err := parseRepo(test.schema, "github.com", "", test.repo)
5555
require.NoError(t, err)
5656

5757
p := &project{

repo.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (r repo) BaseName() string {
4444
// It can be a full url like https://github.com/cdr/sail or ssh://[email protected]/cdr/sail,
4545
// or just the path like cdr/sail and the host + schema will be inferred.
4646
// By default the host and the schema will be the provided defaultSchema.
47-
func parseRepo(defaultSchema, defaultHost, name string) (repo, error) {
47+
func parseRepo(defaultSchema, defaultHost, defaultOrganization, name string) (repo, error) {
4848
u, err := url.Parse(name)
4949
if err != nil {
5050
return repo{}, xerrors.Errorf("failed to parse repo path: %w", err)
@@ -68,6 +68,11 @@ func parseRepo(defaultSchema, defaultHost, name string) (repo, error) {
6868
}
6969
}
7070

71+
// add the defaultOrganization if the path has no slashes
72+
if defaultOrganization != "" && !strings.Contains(r.trimPath(), "/") {
73+
r.Path = fmt.Sprintf("%v/%v", defaultOrganization, r.trimPath())
74+
}
75+
7176
// make sure path doesn't have a leading forward slash
7277
r.Path = strings.TrimPrefix(r.Path, "/")
7378

repo_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99

1010
func TestParseRepo(t *testing.T) {
1111
var tests = []struct {
12-
defSchema string
13-
defHost string
14-
fullPath string
12+
defSchema string
13+
defHost string
14+
defOrganization string
15+
fullPath string
1516

1617
expPath string
1718
expHost string
@@ -23,6 +24,7 @@ func TestParseRepo(t *testing.T) {
2324
{
2425
"ssh",
2526
"github.com",
27+
"",
2628
"cdr/sail",
2729
"cdr/sail",
2830
"github.com",
@@ -34,6 +36,7 @@ func TestParseRepo(t *testing.T) {
3436
{
3537
"http",
3638
"github.com",
39+
"",
3740
"cdr/sail",
3841
"cdr/sail",
3942
"github.com",
@@ -45,6 +48,7 @@ func TestParseRepo(t *testing.T) {
4548
{
4649
"https",
4750
"github.com",
51+
"",
4852
"cdr/sail",
4953
"cdr/sail",
5054
"github.com",
@@ -56,6 +60,7 @@ func TestParseRepo(t *testing.T) {
5660
{
5761
"https",
5862
"github.com",
63+
"",
5964
"https://github.com/cdr/sail",
6065
"cdr/sail",
6166
"github.com",
@@ -67,6 +72,7 @@ func TestParseRepo(t *testing.T) {
6772
{
6873
"ssh",
6974
"github.com",
75+
"",
7076
"[email protected]/cdr/sail.git",
7177
"cdr/sail",
7278
"github.com",
@@ -78,6 +84,7 @@ func TestParseRepo(t *testing.T) {
7884
{
7985
"http",
8086
"github.com",
87+
"",
8188
"ssh://[email protected]/cdr/sail",
8289
"cdr/sail",
8390
"github.com",
@@ -89,17 +96,30 @@ func TestParseRepo(t *testing.T) {
8996
{
9097
"https",
9198
"my.private-git.com",
99+
"",
92100
"private/repo",
93101
"private/repo",
94102
"my.private-git.com",
95103
"",
96104
"https",
97105
"https://my.private-git.com/private/repo.git",
98106
},
107+
// ensure default organization works as expected
108+
{
109+
"ssh",
110+
"github.com",
111+
"cdr",
112+
"sail",
113+
"cdr/sail",
114+
"github.com",
115+
"git",
116+
"ssh",
117+
"ssh://[email protected]/cdr/sail.git",
118+
},
99119
}
100120

101121
for _, test := range tests {
102-
repo, err := parseRepo(test.defSchema, test.defHost, test.fullPath)
122+
repo, err := parseRepo(test.defSchema, test.defHost, test.defOrganization, test.fullPath)
103123
require.NoError(t, err)
104124

105125
assert.Equal(t, test.expPath, repo.Path, "expected path to be the same")

sail_helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func run(t *testing.T, name, repo, hatPath string, fns ...func(t *testing.T, p *
4343

4444
conf := mustReadConfig(filepath.Join(metaRoot(), ".sail.toml"))
4545

46-
repo, err := parseRepo("ssh", "github.com", repo)
46+
repo, err := parseRepo("ssh", "github.com", "", repo)
4747
require.NoError(t, err)
4848

4949
p.proj = &project{

versionmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var version string
1111

12-
type versioncmd struct {}
12+
type versioncmd struct{}
1313

1414
func (v *versioncmd) Spec() cli.CommandSpec {
1515
return cli.CommandSpec{

0 commit comments

Comments
 (0)