Skip to content

Commit

Permalink
Merge pull request #816 from mdcurran/yml-extension-warning
Browse files Browse the repository at this point in the history
Log warning when atlantis.yml is used as config
  • Loading branch information
lkysow authored Oct 30, 2019
2 parents 0d5e25c + c2934b4 commit 649a124
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions server/events/yaml/parser_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ type ParserValidator struct{}
// for the repo at absRepoDir.
// Returns an error if for some reason it can't read that directory.
func (p *ParserValidator) HasRepoCfg(absRepoDir string) (bool, error) {
_, err := os.Stat(p.repoCfgPath(absRepoDir))
// Checks for a config file with an invalid extension (atlantis.yml)
const invalidExtensionFilename = "atlantis.yml"
_, err := os.Stat(p.repoCfgPath(absRepoDir, invalidExtensionFilename))
if err == nil {
return false, errors.Errorf("found %q as config file; rename using the .yaml extension - %q", invalidExtensionFilename, AtlantisYAMLFilename)
}

_, err = os.Stat(p.repoCfgPath(absRepoDir, AtlantisYAMLFilename))
if os.IsNotExist(err) {
return false, nil
}
Expand All @@ -38,7 +45,7 @@ func (p *ParserValidator) HasRepoCfg(absRepoDir string) (bool, error) {
// repo at absRepoDir.
// If there was no config file, it will return an os.IsNotExist(error).
func (p *ParserValidator) ParseRepoCfg(absRepoDir string, globalCfg valid.GlobalCfg, repoID string) (valid.RepoCfg, error) {
configFile := p.repoCfgPath(absRepoDir)
configFile := p.repoCfgPath(absRepoDir, AtlantisYAMLFilename)
configData, err := ioutil.ReadFile(configFile) // nolint: gosec

if err != nil {
Expand Down Expand Up @@ -122,8 +129,8 @@ func (p *ParserValidator) validateRawGlobalCfg(rawCfg raw.GlobalCfg, defaultCfg
return validCfg, nil
}

func (p *ParserValidator) repoCfgPath(repoDir string) string {
return filepath.Join(repoDir, AtlantisYAMLFilename)
func (p *ParserValidator) repoCfgPath(repoDir, cfgFilename string) string {
return filepath.Join(repoDir, cfgFilename)
}

func (p *ParserValidator) validateProjectNames(config valid.RepoCfg) error {
Expand Down
11 changes: 11 additions & 0 deletions server/events/yaml/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func TestHasRepoCfg_FileDoesNotExist(t *testing.T) {
Equals(t, false, exists)
}

func TestHasRepoCfg_InvalidFileExtension(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
_, err := os.Create(filepath.Join(tmpDir, "atlantis.yml"))
Ok(t, err)

r := yaml.ParserValidator{}
_, err = r.HasRepoCfg(tmpDir)
ErrContains(t, "found \"atlantis.yml\" as config file; rename using the .yaml extension - \"atlantis.yaml\"", err)
}

func TestParseRepoCfg_DirDoesNotExist(t *testing.T) {
r := yaml.ParserValidator{}
_, err := r.ParseRepoCfg("/not/exist", globalCfg, "")
Expand Down

0 comments on commit 649a124

Please sign in to comment.