Skip to content

Commit

Permalink
Adds init sub command with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowiem committed Jun 20, 2020
1 parent d0fda8b commit 44732c3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 24 deletions.
41 changes: 41 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// InitCmd is responsible for creating a `ecsrun.yml`
var InitCmd = &cobra.Command{
Use: "init",
Short: "Creates a blank `ecsrun.yml` config file in the current directory.",
Run: func(cmd *cobra.Command, args []string) {
initCmd()
},
}

func initCmd() {
// Reset viper as it carries over config from root. We want to build our own config.
viper.Reset()

config := make(map[string]interface{})
config["default"] = map[string]interface{}{
"cluster": "TODO",
"task": "TODO",
"security-group": "TODO",
"subnet": "TODO",
"cmd": []string{"bash", "-c", "echo", "hello", "world"},
}

if err := viper.MergeConfigMap(config); err != nil {
log.Fatal(err)
}

// Write the file.
if err := viper.SafeWriteConfigAs("./ecsrun.yaml"); err != nil {
log.Fatal(err)
red := color.New(color.FgRed)
red.Printf("ecsrun.yaml already exists.\n")
}
}
37 changes: 37 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/spf13/afero"
"github.com/spf13/viper"
)

var testFs = afero.NewMemMapFs()

// Tests
/////////

func TestInitCmd(t *testing.T) {
assert := assert.New(t)

viper.SetFs(testFs)

exists, err := afero.Exists(testFs, "./ecsrun.yaml")
if err != nil {
panic(err)
}
assert.False(exists)

initCmd()

exists, err = afero.Exists(testFs, "./ecsrun.yaml")
if err != nil {
panic(err)
}
assert.True(exists)

testFs.Remove("./ecsrun.yaml")
}
52 changes: 28 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ ecsrun is a CLI tool that allows users to run one-off administrative tasks
using their existing Task Definitions.`,

Run: func(cmd *cobra.Command, args []string) {
initEnvVars()
initAws()
if err := initConfigFile(); err != nil {
log.Debug(err)
}
Expand Down Expand Up @@ -105,39 +107,43 @@ func Execute(n func(*RunConfig) ECSClient, v VersionInfo) {
}

func init() {
cobra.OnInitialize(initEnvVars, initVerbose, initVersion, initAws)
cobra.OnInitialize(initVerbose, initVersion)

log.SetOutput(os.Stderr)

// Basic Flags
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().Bool("version", false, "version output")
rootCmd.Flags().BoolP("verbose", "v", false, "verbose output")
rootCmd.Flags().Bool("version", false, "version output")

// Config File Flags
rootCmd.PersistentFlags().String("config-file", "", "config file to read config entries from (default is $PWD/escrun.yml)")
rootCmd.PersistentFlags().String("config", "default", "config entry to read in the config file (default is 'default')")
rootCmd.PersistentFlags().Bool("dry-run", false, "dry-run your ecsrun execution to check config (default is false)")
rootCmd.Flags().String("config-file", "", "config file to read config entries from (default is $PWD/escrun.yml)")
rootCmd.Flags().String("config", "default", "config entry to read in the config file (default is 'default')")
rootCmd.Flags().Bool("dry-run", false, "dry-run your ecsrun execution to check config (default is false)")

// AWS Cred / Environment Flags
rootCmd.PersistentFlags().String("cred", "", "AWS credentials file (default is $HOME/.aws/.credentials)")
rootCmd.PersistentFlags().StringP("profile", "p", "", "AWS profile to target (default is AWS_PROFILE or 'default')")
rootCmd.PersistentFlags().String("region", "", `AWS region to target (default is AWS_REGION or pulled from $HOME/.aws/.credentials)`)
rootCmd.Flags().String("cred", "", "AWS credentials file (default is $HOME/.aws/.credentials)")
rootCmd.Flags().StringP("profile", "p", "", "AWS profile to target (default is AWS_PROFILE or 'default')")
rootCmd.Flags().String("region", "", `AWS region to target (default is AWS_REGION or pulled from $HOME/.aws/.credentials)`)

// Task Flags
rootCmd.PersistentFlags().StringP("cluster", "c", "", "The ECS Cluster to run the task in.")
rootCmd.PersistentFlags().StringP("task", "t", "", "The name of the ECS Task Definition to use.")
rootCmd.PersistentFlags().StringP("revision", "r", "", "The Task Definition revision to use.")
rootCmd.PersistentFlags().StringP("name", "n", "", "The name of the container in the Task Definition.")
rootCmd.PersistentFlags().StringP("launch-type", "l", "FARGATE", "The launch type to run as. Currently only Fargate is supported.")
rootCmd.PersistentFlags().StringSlice("cmd", []string{}, "The comma separated command override to apply.")
rootCmd.PersistentFlags().Int64("count", 1, "The number of tasks to launch for the given cmd.")
rootCmd.Flags().StringP("cluster", "c", "", "The ECS Cluster to run the task in.")
rootCmd.Flags().StringP("task", "t", "", "The name of the ECS Task Definition to use.")
rootCmd.Flags().StringP("revision", "r", "", "The Task Definition revision to use.")
rootCmd.Flags().StringP("name", "n", "", "The name of the container in the Task Definition.")
rootCmd.Flags().StringP("launch-type", "l", "FARGATE", "The launch type to run as. Currently only Fargate is supported.")
rootCmd.Flags().StringSlice("cmd", []string{}, "The comma separated command override to apply.")
rootCmd.Flags().Int64("count", 1, "The number of tasks to launch for the given cmd.")

// Network Flags
rootCmd.PersistentFlags().StringP("subnet", "s", "", "The Subnet ID that the task should be launched in.")
rootCmd.PersistentFlags().StringP("security-group", "g", "", "The Security Group ID that the task should be associated with.")
rootCmd.PersistentFlags().Bool("public", false, "Assigns a public IP to the task if included. (default is false)")
rootCmd.Flags().StringP("subnet", "s", "", "The Subnet ID that the task should be launched in.")
rootCmd.Flags().StringP("security-group", "g", "", "The Security Group ID that the task should be associated with.")
rootCmd.Flags().Bool("public", false, "Assigns a public IP to the task if included. (default is false)")

viper.BindPFlags(rootCmd.PersistentFlags())
// Bind all cobra flags to Viper. viper.Get is used heavily.
viper.BindPFlags(rootCmd.Flags())

// Add sub commands
rootCmd.AddCommand(InitCmd)
}

func initEnvVars() {
Expand Down Expand Up @@ -204,12 +210,10 @@ func getProfile() string {
}

func initAwsSession(profile string) (*session.Session, error) {
credFile, err := rootCmd.PersistentFlags().GetString("cred")
if err != nil {
log.Fatal("Not able to get credFile from cmd.", err)
}
credFile := viper.GetString("cred")

var sesh *session.Session
var err error

if credFile != "" {
sesh, err = session.NewSession(&aws.Config{
Expand Down

0 comments on commit 44732c3

Please sign in to comment.