Skip to content

Commit

Permalink
Rename Config to RepoCfg
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Mar 25, 2019
1 parent c92ba39 commit 21bf48f
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 103 deletions.
6 changes: 3 additions & 3 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (p *DefaultProjectCommandBuilder) buildProjectCommandCtx(
return p.buildCtx(ctx, cmd, projCfg, commentFlags, automerge, verbose), nil
}

func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName string, dir string, workspace string, repoDir string) (projectCfg *valid.Project, repoCfg *valid.Config, err error) {
func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName string, dir string, workspace string, repoDir string) (projectCfg *valid.Project, repoCfg *valid.RepoCfg, err error) {
hasConfigFile, err := p.ParserValidator.HasRepoCfg(repoDir)
if err != nil {
err = errors.Wrapf(err, "looking for %s file in %q", yaml.AtlantisYAMLFilename, repoDir)
Expand All @@ -296,7 +296,7 @@ func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName s
return
}

var repoConfig valid.Config
var repoConfig valid.RepoCfg
repoConfig, err = p.ParserValidator.ParseRepoCfg(repoDir, p.GlobalCfg, ctx.BaseRepo.ID())
if err != nil {
return
Expand Down Expand Up @@ -328,7 +328,7 @@ func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName s

// validateWorkspaceAllowed returns an error if there are projects configured
// in globalCfg for repoRelDir and none of those projects use workspace.
func (p *DefaultProjectCommandBuilder) validateWorkspaceAllowed(repoCfg *valid.Config, repoRelDir string, workspace string) error {
func (p *DefaultProjectCommandBuilder) validateWorkspaceAllowed(repoCfg *valid.RepoCfg, repoRelDir string, workspace string) error {
if repoCfg == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/events/project_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ProjectFinder interface {
// DetermineProjects returns the list of projects that were modified based on
// the modifiedFiles. The list will be de-duplicated.
DetermineProjects(log *logging.SimpleLogger, modifiedFiles []string, repoFullName string, repoDir string) []models.Project
DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.Config, repoDir string) ([]valid.Project, error)
DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.RepoCfg, repoDir string) ([]valid.Project, error)
}

// DefaultProjectFinder implements ProjectFinder.
Expand Down Expand Up @@ -80,7 +80,7 @@ func (p *DefaultProjectFinder) DetermineProjects(log *logging.SimpleLogger, modi
// of the config for each project and see if the modifiedFiles matches.
// The list will be de-duplicated.
// todo: move this to yaml.MergedRepoCfg as a method on this project
func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.Config, repoDir string) ([]valid.Project, error) {
func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLogger, modifiedFiles []string, config valid.RepoCfg, repoDir string) ([]valid.Project, error) {
var projects []valid.Project
for _, project := range config.Projects {
log.Debug("checking if project at dir %q workspace %q was modified", project.Dir, project.Workspace)
Expand Down
16 changes: 8 additions & 8 deletions server/events/project_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {

cases := []struct {
description string
config valid.Config
config valid.RepoCfg
modified []string
expProjPaths []string
}{
Expand All @@ -260,7 +260,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
// If our caller is interested in autoplan enabled projects, they'll
// need to filter the results.
description: "autoplan disabled",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -276,7 +276,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: "autoplan default",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -292,7 +292,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: "parent dir modified",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project",
Expand All @@ -308,7 +308,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: "parent dir modified matches",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project1",
Expand All @@ -324,7 +324,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: "dir deleted",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project3",
Expand All @@ -340,7 +340,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: "multiple projects",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
},
{
description: ".tfvars file modified",
config: valid.Config{
config: valid.RepoCfg{
Projects: []valid.Project{
{
Dir: "project2",
Expand Down
16 changes: 8 additions & 8 deletions server/events/yaml/parser_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ func (p *ParserValidator) HasRepoCfg(repoDir string) (bool, error) {
// If there was no config file, then this can be detected by checking the type
// of error: os.IsNotExist(error) but it's instead preferred to check with
// HasRepoCfg.
func (p *ParserValidator) ParseRepoCfg(repoDir string, globalCfg valid.GlobalCfg, repoID string) (valid.Config, error) {
func (p *ParserValidator) ParseRepoCfg(repoDir string, globalCfg valid.GlobalCfg, repoID string) (valid.RepoCfg, error) {
configFile := p.repoCfgPath(repoDir)
configData, err := ioutil.ReadFile(configFile) // nolint: gosec

if err != nil {
if !os.IsNotExist(err) {
return valid.Config{}, errors.Wrapf(err, "unable to read %s file", AtlantisYAMLFilename)
return valid.RepoCfg{}, errors.Wrapf(err, "unable to read %s file", AtlantisYAMLFilename)
}
// Don't wrap os.IsNotExist errors because we want our callers to be
// able to detect if it's a NotExist err.
return valid.Config{}, err
return valid.RepoCfg{}, err
}

var rawConfig raw.Config
var rawConfig raw.RepoCfg
if err := yaml.UnmarshalStrict(configData, &rawConfig); err != nil {
return valid.Config{}, err
return valid.RepoCfg{}, err
}

// Set ErrorTag to yaml so it uses the YAML field names in error messages.
validation.ErrorTag = "yaml"
if err := rawConfig.Validate(); err != nil {
return valid.Config{}, err
return valid.RepoCfg{}, err
}

validConfig := rawConfig.ToValid()

// We do the project name validation after we get the valid config because
// we need the defaults of dir and workspace to be populated.
if err := p.validateProjectNames(validConfig); err != nil {
return valid.Config{}, err
return valid.RepoCfg{}, err
}
err = globalCfg.ValidateRepoCfg(validConfig, repoID)
return validConfig, err
Expand Down Expand Up @@ -102,7 +102,7 @@ func (p *ParserValidator) repoCfgPath(repoDir string) string {
return filepath.Join(repoDir, AtlantisYAMLFilename)
}

func (p *ParserValidator) validateProjectNames(config valid.Config) error {
func (p *ParserValidator) validateProjectNames(config valid.RepoCfg) error {
// First, validate that all names are unique.
seen := make(map[string]bool)
for _, project := range config.Projects {
Expand Down
30 changes: 15 additions & 15 deletions server/events/yaml/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestParseRepoCfg(t *testing.T) {
description string
input string
expErr string
exp valid.Config
exp valid.RepoCfg
}{
// Version key.
{
Expand Down Expand Up @@ -137,7 +137,7 @@ projects:
input: `
version: 2
projects:`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: nil,
Workflows: map[string]valid.Workflow{},
Expand All @@ -157,7 +157,7 @@ projects:
version: 2
projects:
- dir: .`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand All @@ -184,7 +184,7 @@ projects:
autoplan:
when_modified: ["**/*.tf*"]
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand All @@ -206,7 +206,7 @@ version: 2
projects:
- dir: "."
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand All @@ -229,7 +229,7 @@ projects:
- dir: "."
workflows: ~
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -257,7 +257,7 @@ workflows:
apply:
steps:
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -290,7 +290,7 @@ projects:
workflow: myworkflow
workflows:
myworkflow: ~`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -328,7 +328,7 @@ projects:
enabled: false
workflows:
myworkflow: ~`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -366,7 +366,7 @@ projects:
enabled: false
workflows:
myworkflow: ~`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -404,7 +404,7 @@ projects:
enabled: false
workflows:
myworkflow: ~`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -519,7 +519,7 @@ projects:
- name: myname2
dir: .
workspace: workspace`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -561,7 +561,7 @@ workflows:
- plan # NOTE: we don't validate if they make sense
- apply
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -623,7 +623,7 @@ workflows:
- apply:
extra_args: ["a", "b"]
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down Expand Up @@ -681,7 +681,7 @@ workflows:
steps:
- run: echo apply "arg 2"
`,
exp: valid.Config{
exp: valid.RepoCfg{
Version: 2,
Projects: []valid.Project{
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
// DefaultAutomerge is the default setting for automerge.
const DefaultAutomerge = false

// Config is the representation for the whole config file at the top level.
type Config struct {
// RepoCfg is the representation for the whole config file at the top level.
type RepoCfg struct {
Version *int `yaml:"version,omitempty"`
Projects []Project `yaml:"projects,omitempty"`
Workflows map[string]Workflow `yaml:"workflows,omitempty"`
Automerge *bool `yaml:"automerge,omitempty"`
}

func (c Config) Validate() error {
func (r RepoCfg) Validate() error {
equals2 := func(value interface{}) error {
asIntPtr := value.(*int)
if asIntPtr == nil {
Expand All @@ -29,31 +29,31 @@ func (c Config) Validate() error {
}
return nil
}
return validation.ValidateStruct(&c,
validation.Field(&c.Version, validation.By(equals2)),
validation.Field(&c.Projects),
validation.Field(&c.Workflows),
return validation.ValidateStruct(&r,
validation.Field(&r.Version, validation.By(equals2)),
validation.Field(&r.Projects),
validation.Field(&r.Workflows),
)
}

func (c Config) ToValid() valid.Config {
func (r RepoCfg) ToValid() valid.RepoCfg {
validWorkflows := make(map[string]valid.Workflow)
for k, v := range c.Workflows {
for k, v := range r.Workflows {
validWorkflows[k] = v.ToValid(k)
}

var validProjects []valid.Project
for _, p := range c.Projects {
for _, p := range r.Projects {
validProjects = append(validProjects, p.ToValid())
}

automerge := DefaultAutomerge
if c.Automerge != nil {
automerge = *c.Automerge
if r.Automerge != nil {
automerge = *r.Automerge
}

return valid.Config{
Version: *c.Version,
return valid.RepoCfg{
Version: *r.Version,
Projects: validProjects,
Workflows: validWorkflows,
Automerge: automerge,
Expand Down
Loading

0 comments on commit 21bf48f

Please sign in to comment.