Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Verbose flag #330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/cmd/formulas.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
subCommand = " SUBCOMMAND"
Group = "group"
dockerFlag = "docker"
verboseFlag = "verbose"
rootCmdName = "root"
)

Expand Down Expand Up @@ -129,7 +130,13 @@ func (f FormulaCommand) execFormulaFunc(repo, path string) func(cmd *cobra.Comma
return err
}

if err := f.formula.Run(d, inputType, docker); err != nil {
verbose, err := cmd.Flags().GetBool(verboseFlag)

if err != nil {
return err
}

if err := f.formula.Run(d, inputType, docker, verbose); err != nil {
return err
}

Expand All @@ -140,4 +147,5 @@ func (f FormulaCommand) execFormulaFunc(repo, path string) func(cmd *cobra.Comma
func addFlags(cmd *cobra.Command) {
formulaFlags := cmd.Flags()
formulaFlags.BoolP(dockerFlag, "d", false, "Use to run formulas inside docker")
formulaFlags.BoolP(verboseFlag, "a", false, "Verbose mode (All). Indicate to a formula that it should show log messages in more detail")
}
4 changes: 2 additions & 2 deletions pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,15 @@ func (s credSettingsMock) ProviderPath() string {
return ""
}

func (s credSettingsMock) CredentialsPath () string {
func (s credSettingsMock) CredentialsPath() string {
return ""
}

type runnerMock struct {
error error
}

func (r runnerMock) Run(def formula.Definition, inputType api.TermInputType, local bool) error {
func (r runnerMock) Run(def formula.Definition, inputType api.TermInputType, local bool, verbose bool) error {
return r.error
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/credential/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,17 @@ func TestProviderPath(t *testing.T) {
}
}

func TestCredentialsPath(t *testing.T){
func TestCredentialsPath(t *testing.T) {
credentials := credSettings.CredentialsPath()
slicedPath := strings.Split(credentials, string(os.PathSeparator))
fmt.Println(slicedPath)
providersDir := slicedPath[len(slicedPath)-1]

if providersDir != "credentials"{
if providersDir != "credentials" {
t.Errorf("Providers path must end on credentials dir")
}
}


func TestProvidersArr(t *testing.T) {
credentials := NewDefaultCredentials()
providersArray := NewProviderArr(credentials)
Expand Down
3 changes: 2 additions & 1 deletion pkg/formula/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
DefaultConfig = "config.json"
PwdEnv = "PWD"
CPwdEnv = "CURRENT_PWD"
VerboseEnv = "VERBOSE_MODE"
BinUnix = "run.sh"
BinWindows = "run.bat"
BinDir = "bin"
Expand Down Expand Up @@ -95,7 +96,7 @@ type PreRunner interface {
}

type Runner interface {
Run(def Definition, inputType api.TermInputType, docker bool) error
Run(def Definition, inputType api.TermInputType, docker bool, verbose bool) error
}

type PostRunner interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/formula/runner/post_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ func (po PostRunnerManager) removeWorkDir(tmpDir string) {
if err := po.dir.Remove(tmpDir); err != nil {
fmt.Sprintln("Error in remove dir")
}
}
}
6 changes: 3 additions & 3 deletions pkg/formula/runner/pre_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestPreRun(t *testing.T) {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
},
batBuild: batBuildMock{
batBuild: batBuildMock{
build: func(formulaPath string) error {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
Expand All @@ -236,7 +236,7 @@ func TestPreRun(t *testing.T) {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
},
batBuild: batBuildMock{
batBuild: batBuildMock{
build: func(formulaPath string) error {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
Expand All @@ -259,7 +259,7 @@ func TestPreRun(t *testing.T) {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
},
batBuild: batBuildMock{
batBuild: batBuildMock{
build: func(formulaPath string) error {
return dirManager.Create(filepath.Join(formulaPath, "bin"))
},
Expand Down
21 changes: 14 additions & 7 deletions pkg/formula/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"os/exec"
"path"
"strconv"

"github.com/mattn/go-isatty"

Expand Down Expand Up @@ -55,20 +56,20 @@ func NewFormulaRunner(
}
}

func (ru RunManager) Run(def formula.Definition, inputType api.TermInputType, docker bool) error {
func (ru RunManager) Run(def formula.Definition, inputType api.TermInputType, docker bool, verbose bool) error {
setup, err := ru.PreRun(def, docker)
if err != nil {
return err
}

var cmd *exec.Cmd
if !docker || setup.ContainerId == "" {
cmd, err = ru.runLocal(setup, inputType)
cmd, err = ru.runLocal(setup, inputType, verbose)
if err != nil {
return err
}
} else {
cmd, err = ru.runDocker(setup, inputType)
cmd, err = ru.runDocker(setup, inputType, verbose)
if err != nil {
return err
}
Expand All @@ -85,7 +86,7 @@ func (ru RunManager) Run(def formula.Definition, inputType api.TermInputType, do
return nil
}

func (ru RunManager) runDocker(setup formula.Setup, inputType api.TermInputType) (*exec.Cmd, error) {
func (ru RunManager) runDocker(setup formula.Setup, inputType api.TermInputType, verbose bool) (*exec.Cmd, error) {
volume := fmt.Sprintf("%s:/app", setup.Pwd)
var args []string
if isatty.IsTerminal(os.Stdout.Fd()) {
Expand All @@ -103,14 +104,14 @@ func (ru RunManager) runDocker(setup formula.Setup, inputType api.TermInputType)
return nil, err
}

if err := ru.setDockerEnvs(cmd); err != nil {
if err := ru.setDockerEnvs(cmd, verbose); err != nil {
return nil, err
}

return cmd, nil
}

func (ru RunManager) runLocal(setup formula.Setup, inputType api.TermInputType) (*exec.Cmd, error) {
func (ru RunManager) runLocal(setup formula.Setup, inputType api.TermInputType, verbose bool) (*exec.Cmd, error) {
formulaRun := path.Join(setup.TmpDir, setup.BinName)
cmd := exec.Command(formulaRun)
cmd.Stdin = os.Stdin
Expand All @@ -123,19 +124,25 @@ func (ru RunManager) runLocal(setup formula.Setup, inputType api.TermInputType)
cmd.Env = append(cmd.Env, pwdEnv)
cmd.Env = append(cmd.Env, cPwdEnv)

verboseEnv := fmt.Sprintf(formula.EnvPattern, formula.VerboseEnv, strconv.FormatBool(verbose))
cmd.Env = append(cmd.Env, verboseEnv)

if err := ru.Inputs(cmd, setup, inputType); err != nil {
return nil, err
}

return cmd, nil
}

func (ru RunManager) setDockerEnvs(cmd *exec.Cmd) error {
func (ru RunManager) setDockerEnvs(cmd *exec.Cmd, verbose bool) error {
pwdEnv := fmt.Sprintf(formula.EnvPattern, formula.PwdEnv, "/app")
cPwdEnv := fmt.Sprintf(formula.EnvPattern, formula.CPwdEnv, "/app")
cmd.Env = append(cmd.Env, pwdEnv)
cmd.Env = append(cmd.Env, cPwdEnv)

verboseEnv := fmt.Sprintf(formula.EnvPattern, formula.VerboseEnv, strconv.FormatBool(verbose))
cmd.Env = append(cmd.Env, verboseEnv)

for _, e := range cmd.Env { // Create a file named .env and add the environment variable inName=inValue
if !ru.file.Exists(envFile) {
if err := ru.file.Write(envFile, []byte(e+"\n")); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/formula/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestRun(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
in := tt.in
runner := NewFormulaRunner(in.postRun, in.inputRun, in.preRun, in.fileManager)
got := runner.Run(in.def, api.Prompt, in.docker)
got := runner.Run(in.def, api.Prompt, in.docker, false)

if tt.out.err != nil && got != nil && tt.out.err.Error() != got.Error() {
t.Errorf("Run(%s) got %v, want %v", tt.name, got, tt.out.err)
Expand Down