Skip to content

Commit

Permalink
Refactored utility funcitons to util | Fixed variable naming | Revert…
Browse files Browse the repository at this point in the history
…ed to WriteFile
  • Loading branch information
ShahNewazKhan committed Nov 9, 2019
1 parent a6f34c4 commit 0b089a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ RUN mkdir -p /tmp/protoc && \

RUN GO111MODULE=off go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

RUN curl -kLo /tmp/terraform.zip "https://releases.hashicorp.com/terraform/0.12.12/terraform_0.12.12_linux_amd64.zip" && \
RUN curl -sSLo /tmp/terraform.zip "https://releases.hashicorp.com/terraform/0.12.12/terraform_0.12.12_linux_amd64.zip" && \
unzip -d /usr/local/bin/ /tmp/terraform.zip

RUN chmod +x /usr/local/bin/* && \
Expand Down
40 changes: 11 additions & 29 deletions internal/generate/kubernetes/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/manifoldco/promptui"
"gopkg.in/yaml.v2"
)
Expand All @@ -32,22 +34,12 @@ func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGr
t.Kubernetes.TemplateFiles(data, false, wg)
}

func getCwd() string {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Getting working directory failed: %v\n", err)
panic(err)
}

return dir
}

// Execute terrafrom init & plan
func Execute(config *config.Commit0Config) {
if config.Infrastructure.AWS.EKS.Deploy {
log.Println("Preparing aws environment...")

dir := getCwd()
dir := util.GetCwd()

if fileExists(fmt.Sprintf("%s/secrets.yaml", dir)) {
log.Println("secrets.yaml exists ...")
Expand All @@ -65,7 +57,7 @@ func Execute(config *config.Commit0Config) {

func execute(cmd *exec.Cmd, envars []string) {

dir := getCwd()
dir := util.GetCwd()
cmd.Dir = fmt.Sprintf("%s/kubernetes/terraform/environments/staging", dir)

stdoutPipe, _ := cmd.StdoutPipe()
Expand All @@ -74,7 +66,6 @@ func execute(cmd *exec.Cmd, envars []string) {
var errStdout, errStderr error

if envars != nil {
log.Println("Setting envars to cmd ...")
cmd.Env = envars
}

Expand Down Expand Up @@ -115,7 +106,7 @@ func getAwsEnvars(awsSecrets Secrets) []string {

func readSecrets() Secrets {

dir := getCwd()
dir := util.GetCwd()

secretsFile := fmt.Sprintf("%s/secrets.yaml", dir)

Expand All @@ -137,34 +128,25 @@ func readSecrets() Secrets {
func writeSecrets(s Secrets) {
secretsYaml, err := yaml.Marshal(&s)

fmt.Printf("SECRETS: %v", string(secretsYaml))

if err != nil {
log.Fatalf("error: %v", err)
panic(err)
}

dir, err := os.Getwd()
dir := util.GetCwd()

if err != nil {
log.Fatalf("Getting working directory failed: %v\n", err)
panic(err)
}

secretsFile := fmt.Sprintf("%s/secrets.yaml", dir)
// err = ioutil.WriteFile(secretsFile, []byte(secretsYaml), 0644)
f, err := os.Create(secretsFile)
secretsFile := filepath.Join(dir, "secrets.yaml")
err = ioutil.WriteFile(secretsFile, []byte(secretsYaml), 0644)

if err != nil {
log.Fatalf("error: %v", err)
panic(err)
}

defer f.Close()

n3, err := f.WriteString(string(secretsYaml))
f.Sync()
log.Printf("Wrote %d bytes to %v", n3, secretsFile)

}

func promptCredentials() Secrets {
Expand All @@ -180,8 +162,8 @@ func promptCredentials() Secrets {

validateSAK := func(input string) error {
// 40 base64 characters
var awsAccessKeyIDPat = regexp.MustCompile(`^[A-Za-z0-9/+=]{40}$`)
if !awsAccessKeyIDPat.MatchString(input) {
var awsSecretAccessKeyPat = regexp.MustCompile(`^[A-Za-z0-9/+=]{40}$`)
if !awsSecretAccessKeyPat.MatchString(input) {
return errors.New("Invalid aws_secret_access_key")
}
return nil
Expand Down
10 changes: 10 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ var FuncMap = template.FuncMap{
"ToLower": strings.ToLower,
}

func GetCwd() string {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Getting working directory failed: %v\n", err)
panic(err)
}

return dir
}

func createTemplatedFile(fullFilePath string, template *template.Template, wg *sync.WaitGroup, data interface{}) {
f, err := os.Create(fullFilePath)
if err != nil {
Expand Down

0 comments on commit 0b089a6

Please sign in to comment.