Skip to content

Commit

Permalink
Merge pull request #27 from daidokoro/config-functions
Browse files Browse the repository at this point in the history
Config functions - Version 0.80-beta
  • Loading branch information
daidokoro authored Mar 12, 2018
2 parents 538e98d + 6165260 commit f8e5d88
Show file tree
Hide file tree
Showing 40 changed files with 839 additions and 779 deletions.
14 changes: 5 additions & 9 deletions bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bucket

import (
"bytes"
"fmt"
"net/url"
"strings"
"time"
Expand All @@ -29,15 +28,15 @@ func S3Read(URL string, sess *session.Session) (string, error) {
}

// Parse s3 url
bucket := src.Scheme
bucket := src.Host
key := strings.TrimPrefix(src.Path, "/")

params := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}

Log.Debug(fmt.Sprintln("Calling S3 [GetObject] with parameters:", params))
Log.Debug("calling S3 [GetObject] with parameters: %s", params)
resp, err := svc.GetObject(params)
if err != nil {
return "", err
Expand All @@ -48,7 +47,6 @@ func S3Read(URL string, sess *session.Session) (string, error) {
Log.Debug("Reading from S3 Response Body")
buf.ReadFrom(resp.Body)
return buf.String(), nil

}

// S3write - Writes a file to s3 and returns the presigned url
Expand All @@ -63,7 +61,7 @@ func S3write(bucket string, key string, body string, sess *session.Session) (str
},
}

Log.Debug(fmt.Sprintln("Calling S3 [PutObject] with parameters:", params))
Log.Debug("calling S3 [PutObject] with parameters: %s", params)
_, err := svc.PutObject(params)
if err != nil {
return "", err
Expand All @@ -80,7 +78,6 @@ func S3write(bucket string, key string, body string, sess *session.Session) (str
}

return url, nil

}

// Create - create s3 bucket
Expand All @@ -91,7 +88,7 @@ func Create(bucket string, sess *session.Session) error {
Bucket: &bucket,
}

Log.Debug(fmt.Sprintln("Calling S3 [CreateBucket] with parameters:", params))
Log.Debug("calling S3 [CreateBucket] with parameters: %s", params)
_, err := svc.CreateBucket(params)
if err != nil {
return err
Expand All @@ -102,7 +99,6 @@ func Create(bucket string, sess *session.Session) error {
}

return nil

}

// Exists - checks if bucket exists - if err, then its assumed that the bucket does not exist.
Expand All @@ -112,7 +108,7 @@ func Exists(bucket string, sess *session.Session) (bool, error) {
Bucket: &bucket,
}

Log.Debug(fmt.Sprintln("Calling S3 [HeadBucket] with parameters:", params))
Log.Debug("calling S3 [HeadBucket] with parameters: %s", params)
_, err := svc.HeadBucket(params)
if err != nil {
return false, err
Expand Down
12 changes: 5 additions & 7 deletions commands/aws_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a *awsLambda) Invoke(sess *session.Session) error {
params.InvocationType = aws.String("Event")
}

log.Debug(fmt.Sprintln("Calling [Invoke] with parameters:", params))
log.Debug("Calling [Invoke] with parameters: %s", params)
resp, err := svc.Invoke(params)
if err != nil {
return err
Expand All @@ -49,13 +49,11 @@ func (a *awsLambda) Invoke(sess *session.Session) error {

if run.lambdAsync {
code := strconv.FormatInt(*resp.StatusCode, 10)
log.Info(
fmt.Sprintln("lambda async response code:", log.ColorString(code, "green")),
)
log.Info("lambda async response code: %s", log.ColorString(code, "green"))
return nil
}

log.Debug(fmt.Sprintln("lambda response:", a.response))
log.Debug("lambda response: %s", a.response)
return nil
}

Expand All @@ -80,7 +78,7 @@ var invokeCmd = &cobra.Command{
if run.funcEvent != "" {
if strings.HasPrefix(run.funcEvent, "@") {
input := strings.Replace(run.funcEvent, "@", "", -1)
log.Debug(fmt.Sprintf("file input detected [%s], opening file", input))
log.Debug("file input detected [%s], opening file", input)
event, err := ioutil.ReadFile(input)
utils.HandleError(err)
f.payload = event
Expand All @@ -91,7 +89,7 @@ var invokeCmd = &cobra.Command{

if err := f.Invoke(sess); err != nil {
if strings.Contains(err.Error(), "Unhandled") {
log.Error(fmt.Sprintf("Unhandled Exception: Potential Issue with Lambda Function Logic: %s\n", f.name))
log.Error("Unhandled Exception: Potential Issue with Lambda Function Logic: %s\n", f.name)
}
utils.HandleError(err)
}
Expand Down
47 changes: 18 additions & 29 deletions commands/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ var create = &cobra.Command{
}

// check if stack exists in config
if _, ok := stacks[s]; !ok {
if _, ok := stacks.Get(s); !ok {
utils.HandleError(fmt.Errorf("Stack [%s] not found in config", s))
}

if stacks[s].Source == "" {
stacks[s].Source = source
if stacks.MustGet(s).Source == "" {
stacks.MustGet(s).Source = source
}

err = stacks[s].GenTimeParser()
err = stacks.MustGet(s).GenTimeParser()
utils.HandleError(err)

err = stacks[s].Change("create", run.changeName)
err = stacks.MustGet(s).Change("create", run.changeName)
utils.HandleError(err)

log.Info(fmt.Sprintf("change-set [%s] creation successful", run.changeName))
log.Info("change-set [%s] creation successful", run.changeName)

},
}
Expand All @@ -91,13 +91,11 @@ var rm = &cobra.Command{
err := Configure(run.cfgSource, run.cfgRaw)
utils.HandleError(err)

if _, ok := stacks[run.stackName]; !ok {
if _, ok := stacks.Get(run.stackName); !ok {
utils.HandleError(fmt.Errorf("Stack not found: [%s]", run.stackName))
}

s := stacks[run.stackName]

err = s.Change("rm", run.changeName)
err = stacks.MustGet(run.stackName).Change("rm", run.changeName)
utils.HandleError(err)

},
Expand All @@ -117,15 +115,12 @@ var list = &cobra.Command{
err := Configure(run.cfgSource, run.cfgRaw)
utils.HandleError(err)

if _, ok := stacks[run.stackName]; !ok {
if _, ok := stacks.Get(run.stackName); !ok {
utils.HandleError(fmt.Errorf("Stack not found: [%s]", run.stackName))
}

s := stacks[run.stackName]

if err := s.Change("list", run.changeName); err != nil {
utils.HandleError(err)
}
err = stacks.MustGet(run.stackName).Change("list", run.changeName)
utils.HandleError(err)
},
}

Expand Down Expand Up @@ -153,17 +148,14 @@ var execute = &cobra.Command{
return
}

if _, ok := stacks[run.stackName]; !ok {
if _, ok := stacks.Get(run.stackName); !ok {
utils.HandleError(fmt.Errorf("Stack not found: [%s]", run.stackName))
}

s := stacks[run.stackName]

if err := s.Change("execute", run.changeName); err != nil {
utils.HandleError(err)
}
err = stacks.MustGet(run.stackName).Change("execute", run.changeName)
utils.HandleError(err)

log.Info(fmt.Sprintf("change-set [%s] execution successful", run.changeName))
log.Info("change-set [%s] execution successful", run.changeName)
},
}

Expand Down Expand Up @@ -191,14 +183,11 @@ var desc = &cobra.Command{
return
}

if _, ok := stacks[run.stackName]; !ok {
if _, ok := stacks.Get(run.stackName); !ok {
utils.HandleError(fmt.Errorf("Stack not found: [%s]", run.stackName))
}

s := stacks[run.stackName]

if err := s.Change("desc", run.changeName); err != nil {
utils.HandleError(err)
}
err = stacks.MustGet(run.stackName).Change("desc", run.changeName)
utils.HandleError(err)
},
}
17 changes: 10 additions & 7 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"regexp"
"sync"

yaml "gopkg.in/yaml.v2"

Expand All @@ -19,9 +20,11 @@ import (
"github.com/spf13/cobra"
)

var t sync.Map

// initialise - adds, logging and repo vars to dependecny functions
var initialise = func(cmd *cobra.Command, args []string) {
log.Debug(fmt.Sprintf("Initialising Command [%s]", cmd.Name()))
log.Debug("initialising command [%s]", cmd.Name())
// add logging
stks.Log = &log
bucket.Log = &log
Expand Down Expand Up @@ -107,7 +110,7 @@ var (
Example: "qaz set-policy <stack name>",
PreRun: initialise,
Run: func(cmd *cobra.Command, args []string) {

var wg sync.WaitGroup
if len(args) == 0 {
utils.HandleError(fmt.Errorf("please specify stack name"))
}
Expand All @@ -118,11 +121,11 @@ var (
for _, s := range args {
wg.Add(1)
go func(s string) {
if _, ok := stacks[s]; !ok {
if _, ok := stacks.Get(s); !ok {
utils.HandleError(fmt.Errorf("Stack [%s] not found in config", s))
}

err := stacks[s].StackPolicy()
err := stacks.MustGet(s).StackPolicy()
utils.HandleError(err)

wg.Done()
Expand Down Expand Up @@ -155,13 +158,13 @@ var (
err := Configure(run.cfgSource, run.cfgRaw)
utils.HandleError(err)

if _, ok := stacks[s]; !ok {
if _, ok := stacks.Get(s); !ok {
utils.HandleError(fmt.Errorf("Stack [%s] not found in config", s))
}

values := stacks[s].TemplateValues[s].(map[string]interface{})
values := stacks.MustGet(s).TemplateValues[s].(map[string]interface{})

log.Debug(fmt.Sprintln("Converting stack outputs to JSON from:", values))
log.Debug("Converting stack outputs to JSON from: %s", values)
output, err := yaml.Marshal(values)
utils.HandleError(err)

Expand Down
Loading

0 comments on commit f8e5d88

Please sign in to comment.