Skip to content

Commit

Permalink
Fixes up tests and command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowiem committed May 14, 2020
1 parent ee9e639 commit bebc315
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
67 changes: 28 additions & 39 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
/*
Copyright © 2020 Matt Gowie <matt@masterpoint.io>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand All @@ -51,7 +29,10 @@ using their existing Task Definitions.
TODO: Supply more info here.`,

Run: func(cmd *cobra.Command, args []string) {
log.Info("SHRED!")
cluster := viper.GetString("cluster")
def := viper.GetString("def")
runCmd := viper.GetString("cmd")

},
}

Expand All @@ -65,18 +46,31 @@ func Execute() {
}

func init() {
cobra.OnInitialize(initConfig, initVerbose, initAws)
log.Debug("Root init.")
cobra.OnInitialize(initConfig, initVerbose, initAws, buildRunConfig)

log.SetOutput(os.Stderr)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.escrun.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.escrun)")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringP("cred", "c", "", "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().StringP("region", "r", "", `aws region to target (default is AWS_REGION or pulled from $HOME/.aws/.credentials)`)

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().StringP("region", "r", "", `AWS region to target (default is AWS_REGION or pulled from $HOME/.aws/.credentials)`)

rootCmd.PersistentFlags().String("cluster", "", "The ECS Cluster to run the task in.")
rootCmd.PersistentFlags().StringP("def", "d", "", "The ECS Task Definition to use.")
rootCmd.PersistentFlags().StringP("cmd", "c", "", "The ECS Task Definition to use.")

rootCmd.MarkFlagRequired("cluster")
rootCmd.MarkFlagRequired("def")
rootCmd.MarkFlagRequired("cmd")

viper.BindPFlag("profile", rootCmd.PersistentFlags().Lookup("profile"))
viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region"))
viper.BindPFlag("cluster", rootCmd.PersistentFlags().Lookup("cluster"))
viper.BindPFlag("def", rootCmd.PersistentFlags().Lookup("def"))
viper.BindPFlag("cmd", rootCmd.PersistentFlags().Lookup("cmd"))
}

// initConfig reads in config file and ENV variables if set.
Expand Down Expand Up @@ -120,7 +114,7 @@ func initVerbose() {
}

func initAws() {
profile := getProfile(nil)
profile := getProfile()

// Create our AWS session object for AWS API Usage
sesh, err := initAwsSession(profile)
Expand All @@ -130,13 +124,6 @@ func initAws() {
os.Exit(1)
}

cred, err := sesh.Config.Credentials.Get()
if err != nil {
log.Fatal("Unable to get credentials from Session. Check your credentials and profile.")
log.Fatal(err)
os.Exit(1)
}

region := viper.GetString("region")
if region == "" {
region = *sesh.Config.Region
Expand All @@ -146,11 +133,9 @@ func initAws() {

viper.Set("profile", profile)
viper.Set("region", region)
viper.Set("accesskey", cred.AccessKeyID)
viper.Set("secretkey", cred.SecretAccessKey)
}

func getProfile(t *testing.T) string {
func getProfile() string {
var profile = viper.GetString("profile")
if profile == "" {
profile = "default"
Expand Down Expand Up @@ -191,3 +176,7 @@ func initAwsSession(profile string) (*session.Session, error) {

return sesh, err
}

func buildRunConfig() (*RunConfig) {

}
11 changes: 6 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"github.com/spf13/viper"
)

var previous_profile string
var previousProfile string

func setup() {
previous_profile = os.Getenv("AWS_PROFILE")
previousProfile = os.Getenv("AWS_PROFILE")
os.Unsetenv("AWS_PROFILE")
os.Unsetenv("AWS_ACCESS_KEY_ID")
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
}

func teardown() {
os.Setenv("AWS_PROFILE", previous_profile)
os.Setenv("AWS_PROFILE", previousProfile)
viper.Reset()
}

Expand All @@ -29,6 +29,7 @@ func TestExecute(t *testing.T) {

os.Setenv("AWS_ACCESS_KEY_ID", "123")
os.Setenv("AWS_SECRET_ACCESS_KEY", "SECRET123")

Execute()

var accessKey = viper.Get("accessKey")
Expand All @@ -55,11 +56,11 @@ func TestGetProfile(t *testing.T) {
assert := assert.New(t)
setup()

var profile1 = getProfile(t)
var profile1 = getProfile()
assert.Equal("default", profile1)

os.Setenv("AWS_PROFILE", "not-default")
var profile2 = getProfile(t)
var profile2 = getProfile()
assert.Equal("not-default", profile2)

teardown()
Expand Down

0 comments on commit bebc315

Please sign in to comment.