diff --git a/cmd/duffle/main.go b/cmd/duffle/main.go index 4e8c8f4a..ba6f527d 100644 --- a/cmd/duffle/main.go +++ b/cmd/duffle/main.go @@ -63,14 +63,13 @@ func loadCredentials(files []string, b *bundle.Bundle) (map[string]string, error return creds, credentials.Validate(creds, b.Credentials) } + credDir := home.Home(homePath()).Credentials() // Credentials directory should exist from duffle init + // The strategy here is "last one wins". We loop through each credential file and // calculate its credentials. Then we insert them into the creds map in the order // in which they were supplied on the CLI. for _, file := range files { - if !isPathy(file) { - file = filepath.Join(home.Home(homePath()).Credentials(), file+".yaml") - } - cset, err := credentials.Load(file) + cset, err := credentials.Load(findCreds(credDir, file)) if err != nil { return creds, err } @@ -86,9 +85,23 @@ func loadCredentials(files []string, b *bundle.Bundle) (map[string]string, error return creds, credentials.Validate(creds, b.Credentials) } -// isPathy checks to see if a name looks like a path. -func isPathy(name string) bool { - return strings.Contains(name, string(filepath.Separator)) +func findCreds(credDir string, file string) string { + if !fileExists(file) { + testPath := filepath.Join(credDir, file+".yaml") + if fileExists(testPath) { + file = testPath + } else { + file = filepath.Join(credDir, file+".yml") // Don't bother checking existence because it fails later + } + } + return file +} + +func fileExists(path string) bool { + if _, err := os.Stat(path); !os.IsNotExist(err) { + return true + } + return false } func must(err error) { diff --git a/cmd/duffle/main_test.go b/cmd/duffle/main_test.go index e77cafc5..09aa47f9 100644 --- a/cmd/duffle/main_test.go +++ b/cmd/duffle/main_test.go @@ -39,20 +39,6 @@ func CreateTestHome(t *testing.T) home.Home { return testHome } -func TestIsPathy(t *testing.T) { - is := assert.New(t) - thispath := filepath.Join("this", "is", "a", "path") - fooya := filepath.Join("..", "foo.yaml") - for path, expect := range map[string]bool{ - "foo": false, - thispath: true, - "foo.yaml": false, - fooya: true, - } { - is.Equal(expect, isPathy(path), "Expected %t, for %s", expect, path) - } -} - func TestLoadCredentials(t *testing.T) { cred1 := credentials.CredentialSet{ Name: "first", @@ -128,3 +114,51 @@ func TestLoadCredentials(t *testing.T) { is.Equal("cred3", creds["haversack"]) is.Equal("cred1", creds["gym-bag"]) } + +func TestFindCreds(t *testing.T) { + credDir, err := ioutil.TempDir("", "credTest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(credDir) + is := assert.New(t) + + tests := map[string]struct { + input string + expectedFilePath func() string + }{ + "no path yaml": { + input: "creds1", + expectedFilePath: func() string { + credPath := filepath.Join(credDir, "creds1.yaml") + err = ioutil.WriteFile(credPath, []byte("test"), 0644) + if err != nil { + t.Fatal(err) + } + return credPath + }, + }, + "no path yml": { + input: "creds2", + expectedFilePath: func() string { + credPath := filepath.Join(credDir, "creds2.yml") + err = ioutil.WriteFile(credPath, []byte("test"), 0644) + if err != nil { + t.Fatal(err) + } + return credPath + }, + }, + "path": { + input: "testdata/dufflehome/credentials/testing.yaml", + expectedFilePath: func() string { + return "testdata/dufflehome/credentials/testing.yaml" + }, + }, + } + + for name, testCase := range tests { + is.Equal(testCase.expectedFilePath(), findCreds(credDir, testCase.input), "Fail on test: "+name) + os.RemoveAll(filepath.Join(credDir, "*")) + } +}