Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Work for overriding the manifest file location (#122)
Browse files Browse the repository at this point in the history
* Work for overriding the manifest file location

* Fix tests for manifest location

* Fix manifest init
  • Loading branch information
iann0036 authored and ojkelly committed Oct 2, 2018
1 parent b823f0e commit db640d8
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 65 deletions.
10 changes: 10 additions & 0 deletions documentation/content/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down
14 changes: 7 additions & 7 deletions internal/manifest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
)
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/manifest/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 8 additions & 17 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/manifest/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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 {
return err
}

// Write the manifest
err = objectStore.Put(manifestString, "kombustion.yaml")
err = objectStore.Put(manifestString, manifestLocation)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/plugins/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions internal/tasks/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
Expand Down
5 changes: 4 additions & 1 deletion internal/tasks/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,6 +47,7 @@ func Delete(c *cli.Context) {
stackName,
region,
envName,
manifestFile,
)
}

Expand All @@ -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, "")
}
Expand Down
29 changes: 15 additions & 14 deletions internal/tasks/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
},
)
Expand All @@ -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
)
},
)
Expand Down
4 changes: 3 additions & 1 deletion internal/tasks/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func PrintEvents(c *cli.Context) {
c.GlobalString("environment"),
c.GlobalString("region"),
c.String("profile"),
c.GlobalString("manifest-file"),
)
}

Expand All @@ -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, "")
}
Expand Down
22 changes: 12 additions & 10 deletions internal/tasks/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
},
)
Expand All @@ -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
)
},
)
Expand Down
5 changes: 5 additions & 0 deletions internal/tasks/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion internal/tasks/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -77,6 +78,7 @@ func Generate(c *cli.Context) {
outputParameters,
env,
generateDefaultOutputs,
manifestFile,
)
}

Expand All @@ -90,6 +92,7 @@ func generate(
outputParameters bool,
envName string,
generateDefaultOutputs bool,
manifestLocation string,
) {
printer.Step("Generate template")
printer.Progress("Kombusting")
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions internal/tasks/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestSimpleGenerate(t *testing.T) {
true, // ouputParameters
"ci", // envName
false, // generateDefaultOutputs
"kombustion.yaml", // manifest location
)
},
)
Expand Down
2 changes: 1 addition & 1 deletion internal/tasks/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Loading

0 comments on commit db640d8

Please sign in to comment.