Skip to content

Commit

Permalink
adds apply command
Browse files Browse the repository at this point in the history
Runs `make` on every created project folder [related to #132]
  • Loading branch information
thdaraujo committed Jun 10, 2020
1 parent eb68ca4 commit 6799dc6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
33 changes: 30 additions & 3 deletions cmd/apply.go
Expand Up @@ -3,8 +3,12 @@ package cmd
import (
"fmt"
"log"
"os/exec"
"path/filepath"
"strings"

"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/util"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,15 +44,18 @@ Only a single environment may be suitable for an initial test, but for a real sy
}

// @TODO : Pass environments to make commands

var config interface{}
makeAll(config, applyEnvironments)
},
}

// promptEnvironments Prompts the user for the environments to apply against and returns a slice of strings representing the environments
func promptEnvironments() []string {
items := map[string][]string{
"Staging ": []string{"staging"},
"Production": []string{"production"},
"Both Staging and Production": []string{"staging", "production"},
"Staging ": {"staging"},
"Production": {"production"},
"Both Staging and Production": {"staging", "production"},
}

var labels []string
Expand All @@ -67,3 +74,23 @@ func promptEnvironments() []string {
}
return items[providerResult]
}

func makeAll(config interface{}, envars []string) error {
environmentArg := fmt.Sprintf("ENVIRONMENT=%s", strings.Join(envars, ","))
projects := projectPaths(config)

for _, project := range projects {
absPath, err := filepath.Abs(project)
if err != nil {
return err
}
output := util.ExecuteCommandOutput(exec.Command("make", environmentArg), absPath, envars)
fmt.Println(output)
}
return nil
}

// @TODO extract project paths from some config/yaml
func projectPaths(someConfig interface{}) []string {
return []string{"foo", "bar", "baz"}
}
12 changes: 12 additions & 0 deletions cmd/apply_test.go
@@ -0,0 +1,12 @@
package cmd_test

import (
"testing"
)

// @TODO mock os.exec and check execution log/transcript?
func TestApply(t *testing.T) {
if false {
t.Fatalf("apply failed!")
}
}
3 changes: 3 additions & 0 deletions internal/util/util.go
Expand Up @@ -10,6 +10,7 @@ import (
"path"
"strings"
"text/template"

"github.com/google/uuid"
)

Expand Down Expand Up @@ -83,6 +84,8 @@ func ExecuteCommand(cmd *exec.Cmd, pathPrefix string, envars []string) {
}
}

// ExecuteCommandOutput runs the command and returns its
// combined standard output and standard error.
func ExecuteCommandOutput(cmd *exec.Cmd, pathPrefix string, envars []string) string {
dir := GetCwd()

Expand Down

0 comments on commit 6799dc6

Please sign in to comment.