From db640d8f62121a8983115376f7086ef672890cae Mon Sep 17 00:00:00 2001 From: Ian Mckay Date: Tue, 2 Oct 2018 11:12:19 +1000 Subject: [PATCH] Work for overriding the manifest file location (#122) * Work for overriding the manifest file location * Fix tests for manifest location * Fix manifest init --- documentation/content/api/cli.md | 10 ++++++++++ internal/manifest/init.go | 14 +++++++------- internal/manifest/init_test.go | 2 +- internal/manifest/manifest.go | 25 ++++++++----------------- internal/manifest/write.go | 4 ++-- internal/plugins/add.go | 4 ++-- internal/tasks/add.go | 9 +++++---- internal/tasks/delete.go | 5 ++++- internal/tasks/delete_test.go | 29 +++++++++++++++-------------- internal/tasks/events.go | 4 +++- internal/tasks/events_test.go | 22 ++++++++++++---------- internal/tasks/flags.go | 5 +++++ internal/tasks/generate.go | 5 ++++- internal/tasks/generate_test.go | 1 + internal/tasks/init.go | 2 +- internal/tasks/upsert.go | 11 +++++++---- internal/tasks/upsert_test.go | 1 + 17 files changed, 88 insertions(+), 65 deletions(-) diff --git a/documentation/content/api/cli.md b/documentation/content/api/cli.md index d80da449..e61c8f79 100644 --- a/documentation/content/api/cli.md +++ b/documentation/content/api/cli.md @@ -40,6 +40,16 @@ $ kombustion --profile MyProfile --- +### `--manifest-file` + +_location of the manifest file, defaults to `kombustion.yaml`_ + +```bash +$ kombustion --manifest-file dir/dir/kombustion.yaml +``` + +--- + ### `--load-plugin` _Load arbitrary plugin._ diff --git a/internal/manifest/init.go b/internal/manifest/init.go index 355bd1b5..b5605e9b 100644 --- a/internal/manifest/init.go +++ b/internal/manifest/init.go @@ -16,16 +16,16 @@ type InitialisePrompter interface { } // InitaliseNewManifest creates a new manifest with a survey -func InitialiseNewManifest(objectStore core.ObjectStore) error { - return initialiseNewManifest(objectStore, &surveyPrompt{}) +func InitialiseNewManifest(objectStore core.ObjectStore, manifestLocation string) error { + return initialiseNewManifest(objectStore, &surveyPrompt{}, manifestLocation) } -func initialiseNewManifest(objectStore core.ObjectStore, prompter InitialisePrompter) error { +func initialiseNewManifest(objectStore core.ObjectStore, prompter InitialisePrompter, manifestLocation string) error { // Load the manifest file from this directory - if CheckManifestExists(objectStore) { + if CheckManifestExists(objectStore, manifestLocation) { printer.Fatal( - fmt.Errorf("Sorry we can't create a new kombustion.yaml, one already exists."), - "If you want to re-initialise your kombustion.yaml file, first remove it.", + fmt.Errorf("Sorry we can't create a new manifest file, one already exists."), + "If you want to re-initialise your manifest file, first remove it.", "https://www.kombustion.io/api/manifest/", ) } @@ -36,7 +36,7 @@ func initialiseNewManifest(objectStore core.ObjectStore, prompter InitialiseProm return err } - err = WriteManifestObject(objectStore, manifest) + err = WriteManifestObject(objectStore, manifest, manifestLocation) if err != nil { return err } diff --git a/internal/manifest/init_test.go b/internal/manifest/init_test.go index ce764876..e71912d2 100644 --- a/internal/manifest/init_test.go +++ b/internal/manifest/init_test.go @@ -95,7 +95,7 @@ func TestInitialiseManifest(t *testing.T) { objectStore := coretest.NewMockObjectStore() - err := initialiseNewManifest(objectStore, testPrompt) + err := initialiseNewManifest(objectStore, testPrompt, "kombustion.yaml") assert.Nil(t, err) data, err := objectStore.Get("kombustion.yaml") diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index a8c1c076..6516eb12 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -2,44 +2,35 @@ package manifest import ( "fmt" + "github.com/KablamoOSS/kombustion/internal/core" yaml "github.com/KablamoOSS/yaml" ) -func CheckManifestExists(objectStore core.ObjectStore) bool { - data, err := GetManifestObject(objectStore) +func CheckManifestExists(objectStore core.ObjectStore, manifestLocation string) bool { + data, err := GetManifestObject(objectStore, manifestLocation) if err != nil { return false } return data != nil } -func GetManifestObject(objectStore core.ObjectStore, path ...string) (*Manifest, error) { +func GetManifestObject(objectStore core.ObjectStore, manifestLocation string, path ...string) (*Manifest, error) { var manifest Manifest var err error - ymlpath := append(path, "kombustion.yml") - ymldata, err := objectStore.Get(ymlpath[0], ymlpath[1:]...) - if err != nil { - return &Manifest{}, fmt.Errorf("kombustion.yml: %v", err) - } - // Read the manifest file - yamlpath := append(path, "kombustion.yaml") + yamlpath := append(path, manifestLocation) yamldata, err := objectStore.Get(yamlpath[0], yamlpath[1:]...) if err != nil { - return &Manifest{}, fmt.Errorf("kombustion.yaml: %v", err) + return &Manifest{}, fmt.Errorf("%v: %v", manifestLocation, err) } - if ymldata != nil && yamldata != nil { - return &Manifest{}, fmt.Errorf("there are both kombustion.yaml && kombustion.yml files, please remove one") - } else if ymldata != nil { - manifest, err = unmarshalManifest(ymldata) - } else if yamldata != nil { + if yamldata != nil { manifest, err = unmarshalManifest(yamldata) } else { - return &Manifest{}, fmt.Errorf("kombustion.yaml was not found") + return &Manifest{}, fmt.Errorf("%v was not found", manifestLocation) } if err != nil { diff --git a/internal/manifest/write.go b/internal/manifest/write.go index f979a55c..46d0a26f 100644 --- a/internal/manifest/write.go +++ b/internal/manifest/write.go @@ -6,7 +6,7 @@ import ( ) // WriteManifestToDisk - Write the final manifest to disk -func WriteManifestObject(objectStore core.ObjectStore, manifest *Manifest) error { +func WriteManifestObject(objectStore core.ObjectStore, manifest *Manifest, manifestLocation string) error { // Marshal the the struct into yaml manifestString, err := yaml.Marshal(&manifest) if err != nil { @@ -14,7 +14,7 @@ func WriteManifestObject(objectStore core.ObjectStore, manifest *Manifest) error } // Write the manifest - err = objectStore.Put(manifestString, "kombustion.yaml") + err = objectStore.Put(manifestString, manifestLocation) if err != nil { return err } diff --git a/internal/plugins/add.go b/internal/plugins/add.go index 7bed971a..cc6b560d 100644 --- a/internal/plugins/add.go +++ b/internal/plugins/add.go @@ -22,7 +22,7 @@ func init() { // AddPluginsToManifest - Add all new plugin to the manifest // update it if it's already there // then write the manifest to disk -func AddPluginsToManifest(objectStore core.ObjectStore, manifest *manifestType.Manifest, pluginLocations []string) (*manifestType.Manifest, error) { +func AddPluginsToManifest(objectStore core.ObjectStore, manifest *manifestType.Manifest, pluginLocations []string, manifestLocation string) (*manifestType.Manifest, error) { printer.Progress("Kombusting") // Get the lockFile @@ -36,7 +36,7 @@ func AddPluginsToManifest(objectStore core.ObjectStore, manifest *manifestType.M } printer.Progress("Updating manifest") - err = manifestType.WriteManifestObject(objectStore, manifest) + err = manifestType.WriteManifestObject(objectStore, manifest, manifestLocation) if err != nil { printer.Error(err, config.ErrorHelpInfo, "") return manifest, err diff --git a/internal/tasks/add.go b/internal/tasks/add.go index f7622dfa..8855f507 100644 --- a/internal/tasks/add.go +++ b/internal/tasks/add.go @@ -15,22 +15,23 @@ import ( func AddPluginToManifest(c *cli.Context) error { // Get the plugin to add pluginNames := c.Args() + manifestLocation := c.GlobalString("manifest-file") objectStore := core.NewFilesystemStore(".") - addPluginToManifest(objectStore, pluginNames) + addPluginToManifest(objectStore, pluginNames, manifestLocation) return nil } -func addPluginToManifest(objectStore core.ObjectStore, pluginNames []string) { +func addPluginToManifest(objectStore core.ObjectStore, pluginNames []string, manifestLocation string) { printer.Step("Add plugins") // Try and load the manifest - manifestFile, err := manifest.GetManifestObject(objectStore) + manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation) if err != nil { printer.Fatal(err, config.ErrorHelpInfo, "") } // Add them - _, err = plugins.AddPluginsToManifest(objectStore, manifestFile, pluginNames) + _, err = plugins.AddPluginsToManifest(objectStore, manifestFile, pluginNames, manifestLocation) if err != nil { printer.Fatal(err, config.ErrorHelpInfo, "") } diff --git a/internal/tasks/delete.go b/internal/tasks/delete.go index a05e5ba5..fcde956e 100644 --- a/internal/tasks/delete.go +++ b/internal/tasks/delete.go @@ -37,6 +37,7 @@ func Delete(c *cli.Context) { region := c.String("region") envName := c.String("environment") stackName := c.String("stack-name") + manifestFile := c.GlobalString("manifest-file") taskDelete( client, @@ -46,6 +47,7 @@ func Delete(c *cli.Context) { stackName, region, envName, + manifestFile, ) } @@ -57,10 +59,11 @@ func taskDelete( stackName string, region string, envName string, + manifestLocation string, ) { printer.Progress("Kombusting") - manifestFile, err := manifest.GetManifestObject(objectStore) + manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation) if err != nil { printer.Fatal(err, config.ErrorHelpInfo, "") } diff --git a/internal/tasks/delete_test.go b/internal/tasks/delete_test.go index b0004dc9..ee534bd1 100644 --- a/internal/tasks/delete_test.go +++ b/internal/tasks/delete_test.go @@ -6,18 +6,17 @@ import ( printer "github.com/KablamoOSS/go-cli-printer" "github.com/KablamoOSS/kombustion/internal/coretest" - awsCF "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/aws/aws-sdk-go/aws" + awsCF "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/stretchr/testify/assert" ) type MockStackDeleter struct { AcctID string - Events map[string][]*awsCF.StackEvent - Stacks map[string]*awsCF.Stack + Events map[string][]*awsCF.StackEvent + Stacks map[string]*awsCF.Stack } - func (msd *MockStackDeleter) Open(_, _ string) string { if msd.Events == nil { msd.Events = make(map[string][]*awsCF.StackEvent) @@ -104,11 +103,12 @@ func TestDeleteTask(t *testing.T) { taskDelete( deleter, objectStore, - "test.yaml", // templatePath - "foo-stack", // stackName - "ci", // envName - "profile", // profile - "region", // region + "test.yaml", // templatePath + "foo-stack", // stackName + "ci", // envName + "profile", // profile + "region", // region + "kombustion.yaml", // manifest location ) }, ) @@ -130,11 +130,12 @@ func TestDeleteTaskStackNotFound(t *testing.T) { taskDelete( deleter, objectStore, - "test.yaml", // templatePath - "event-stack", // stackName - "ci", // envName - "profile", // profile - "region", // region + "test.yaml", // templatePath + "event-stack", // stackName + "ci", // envName + "profile", // profile + "region", // region + "kombustion.yaml", // manifest location ) }, ) diff --git a/internal/tasks/events.go b/internal/tasks/events.go index fcaa2106..a7604db8 100644 --- a/internal/tasks/events.go +++ b/internal/tasks/events.go @@ -35,6 +35,7 @@ func PrintEvents(c *cli.Context) { c.GlobalString("environment"), c.GlobalString("region"), c.String("profile"), + c.GlobalString("manifest-file"), ) } @@ -46,10 +47,11 @@ func printEvents( envName string, profile string, region string, + manifestLocation string, ) { printer.Progress("Kombusting") - manifestFile, err := manifest.GetManifestObject(objectStore) + manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation) if err != nil { printer.Fatal(err, config.ErrorHelpInfo, "") } diff --git a/internal/tasks/events_test.go b/internal/tasks/events_test.go index b52a035e..d14210bb 100644 --- a/internal/tasks/events_test.go +++ b/internal/tasks/events_test.go @@ -63,11 +63,12 @@ func TestEventsTask(t *testing.T) { printEvents( objectStore, eventer, - "test.yaml", // templatePath - "event-stack", // stackName - "ci", // envName - "profile", // profile - "region", // region + "test.yaml", // templatePath + "event-stack", // stackName + "ci", // envName + "profile", // profile + "region", // region + "kombustion.yaml", // manifest location ) }, ) @@ -89,11 +90,12 @@ func TestEventsTaskNotFound(t *testing.T) { printEvents( objectStore, eventer, - "test.yaml", // templatePath - "event-stack", // stackName - "ci", // envName - "profile", // profile - "region", // region + "test.yaml", // templatePath + "event-stack", // stackName + "ci", // envName + "profile", // profile + "region", // region + "kombustion.yaml", // manifest location ) }, ) diff --git a/internal/tasks/flags.go b/internal/tasks/flags.go index a1b6df65..c6285810 100644 --- a/internal/tasks/flags.go +++ b/internal/tasks/flags.go @@ -16,6 +16,11 @@ var GlobalFlags = []cli.Flag{ Name: "profile", Usage: "use a profile from ~/.aws/credentials eg `MyProfile`", }, + cli.StringFlag{ + Name: "manifest-file", + Usage: "location of the manifest file, defaults to `kombustion.yaml`", + Value: "kombustion.yaml", + }, } // CloudFormationStackFlags for tasks relating to CRUD of cloudformation stacks diff --git a/internal/tasks/generate.go b/internal/tasks/generate.go index d9f77426..c3c8990b 100644 --- a/internal/tasks/generate.go +++ b/internal/tasks/generate.go @@ -66,6 +66,7 @@ func Generate(c *cli.Context) { outputParameters := c.Bool("write-parameters") env := c.String("environment") generateDefaultOutputs := c.Bool("generate-default-outputs") + manifestFile := c.GlobalString("manifest-file") generate( objectStore, @@ -77,6 +78,7 @@ func Generate(c *cli.Context) { outputParameters, env, generateDefaultOutputs, + manifestFile, ) } @@ -90,6 +92,7 @@ func generate( outputParameters bool, envName string, generateDefaultOutputs bool, + manifestLocation string, ) { printer.Step("Generate template") printer.Progress("Kombusting") @@ -104,7 +107,7 @@ func generate( } // manifestFile := manifest.FindAndLoadManifest() - manifestFile, err := manifest.GetManifestObject(objectStore) + manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation) if err != nil { printer.Fatal( fmt.Errorf("Couldn't load manifest file: %v", err), diff --git a/internal/tasks/generate_test.go b/internal/tasks/generate_test.go index 17bf3f5b..35f34d40 100644 --- a/internal/tasks/generate_test.go +++ b/internal/tasks/generate_test.go @@ -87,6 +87,7 @@ func TestSimpleGenerate(t *testing.T) { true, // ouputParameters "ci", // envName false, // generateDefaultOutputs + "kombustion.yaml", // manifest location ) }, ) diff --git a/internal/tasks/init.go b/internal/tasks/init.go index b88623f4..325dccb8 100644 --- a/internal/tasks/init.go +++ b/internal/tasks/init.go @@ -24,5 +24,5 @@ func InitialiseNewManifestTask(c *cli.Context) error { objectStore := core.NewFilesystemStore(".") // This funciton is a thin layer between the task, and the cli wrapper - return manifest.InitialiseNewManifest(objectStore) + return manifest.InitialiseNewManifest(objectStore, c.GlobalString("manifest-file")) } diff --git a/internal/tasks/upsert.go b/internal/tasks/upsert.go index 579b7c4b..732fa7e0 100644 --- a/internal/tasks/upsert.go +++ b/internal/tasks/upsert.go @@ -79,6 +79,7 @@ func Upsert(c *cli.Context) { generateDefaultOutputs := c.Bool("generate-default-outputs") capabilities := getCapabilities(c) confirm := c.Bool("confirm") + manifestFile := c.GlobalString("manifest-file") client := &cloudformation.Wrapper{} @@ -97,6 +98,7 @@ func Upsert(c *cli.Context) { generateDefaultOutputs, capabilities, confirm, + manifestFile, ) } func upsert( @@ -114,6 +116,7 @@ func upsert( generateDefaultOutputs bool, capabilities []*string, confirm bool, + manifestLocation string, ) { printer.Progress("Kombusting") @@ -127,7 +130,7 @@ func upsert( } // manifestFile := manifest.FindAndLoadManifest() - manifestFile, err := manifest.GetManifestObject(objectStore) + manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation) if err != nil { printer.Fatal( fmt.Errorf("Couldn't load manifest file: %v", err), @@ -189,9 +192,9 @@ func upsert( printer.Progress("Generating template") // Template generation parameters generateParams := cloudformation.GenerateParams{ - ObjectStore: objectStore, - Filename: templatePath, - Env: envName, + ObjectStore: objectStore, + Filename: templatePath, + Env: envName, GenerateDefaultOutputs: generateDefaultOutputs || manifestFile.GenerateDefaultOutputs, ParamMap: paramMap, Plugins: loadedPlugins, diff --git a/internal/tasks/upsert_test.go b/internal/tasks/upsert_test.go index 218880e2..f80ecc3f 100644 --- a/internal/tasks/upsert_test.go +++ b/internal/tasks/upsert_test.go @@ -189,6 +189,7 @@ func TestUpsert(t *testing.T) { false, // generateDefaultOutputs []*string{}, // capabilities false, // confirm + "kombustion.yaml", // manifest location ) }, )