tfvars-atlantis-config/repocfg/repocfg.go
2024-03-20 13:48:16 +00:00

75 lines
1.8 KiB
Go

package repocfg
import (
"fmt"
"github.com/runatlantis/atlantis/server/core/config/raw"
"gopkg.in/yaml.v2"
)
var ErrNoExistingConfig = fmt.Errorf("no existing config found")
// Options represents the top-level configuration for a new Atlantis RepoCfg
type Options struct {
Automerge bool
Autoplan bool
DefaultTerraformVersion string
Parallel bool
UseWorkspaces bool
}
// Component represents a Terraform component and its associated Terraform variable files
type Component struct {
Path string
VarFiles []string
}
// ExtRawRepoCfg is an embedded type for a raw.RepoCfg
type ExtRawRepoCfg struct {
raw.RepoCfg `yaml:",inline"`
}
// NewRepoCfg returns a new Atlantis RepoCfg from a slice of components
// and options.
//
// Reference: https://www.runatlantis.io/docs/repo-level-atlantis-yaml.html
func NewRepoCfg(components []Component, opts Options) (*ExtRawRepoCfg, error) {
repoCfg := &ExtRawRepoCfg{
RepoCfg: raw.RepoCfg{
Version: ptr(3),
Automerge: &opts.Automerge,
ParallelPlan: &opts.Parallel,
ParallelApply: &opts.Parallel,
},
}
var projects []raw.Project
for _, c := range components {
generated, err := ProjectsFrom(c, opts)
if err != nil {
return nil, fmt.Errorf("failed while creating projects with component %+v: %w", c, err)
}
for _, p := range generated {
projects = append(projects, p.Project)
}
}
repoCfg.Projects = append(repoCfg.Projects, projects...)
return repoCfg, nil
}
func (rc *ExtRawRepoCfg) MarshalYAML() (interface{}, error) {
m := yaml.MapSlice{
{Key: "version", Value: rc.Version},
{Key: "automerge", Value: rc.Automerge},
{Key: "parallel_plan", Value: rc.ParallelPlan},
{Key: "parallel_apply", Value: rc.ParallelApply},
{Key: "projects", Value: rc.Projects},
}
return m, nil
}