Skip to content
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
22 changes: 17 additions & 5 deletions cmd/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Parser interface {
// DefaultParser implements all default behavior for using kool.yml files.
type DefaultParser struct {
targetFiles []string
lookedUp map[string]bool
}

// NewParser initializes a Parser to be used for handling kool.yml scripts.
Expand All @@ -29,16 +30,27 @@ func NewParser() Parser {
func (p *DefaultParser) AddLookupPath(rootPath string) (err error) {
var koolFile string

if _, err = os.Stat(path.Join(rootPath, "kool.yml")); err == nil {
koolFile = path.Join(rootPath, "kool.yml")
} else if _, err = os.Stat(path.Join(rootPath, "kool.yaml")); err == nil {
koolFile = path.Join(rootPath, "kool.yaml")
if p.lookedUp == nil {
p.lookedUp = make(map[string]bool)
}

ymlPath := path.Join(rootPath, "kool.yml")
yamlPath := path.Join(rootPath, "kool.yaml")

if _, err = os.Stat(ymlPath); err == nil {
koolFile = ymlPath
} else if _, err = os.Stat(yamlPath); err == nil {
koolFile = yamlPath
}

if koolFile == "" {
err = ErrKoolYmlNotFound
} else {
p.targetFiles = append(p.targetFiles, koolFile)
if !p.lookedUp[koolFile] {
p.targetFiles = append(p.targetFiles, koolFile)
}

p.lookedUp[koolFile] = true
}

return
Expand Down
9 changes: 7 additions & 2 deletions cmd/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ func TestParserAddLooupPath(t *testing.T) {

if err == nil || ErrKoolYmlNotFound.Error() != err.Error() {
t.Errorf("expected ErrKoolYmlNotFound; got %s", err)
return
}

workDir, _ := os.Getwd()
if err = p.AddLookupPath(path.Join(workDir, "testing_files")); err != nil {
t.Errorf("unexpected error; error: %s", err)
return
}

_ = p.AddLookupPath(path.Join(workDir, "testing_files"))
_ = p.AddLookupPath(path.Join(workDir, "testing_files"))

if commands, _ := p.Parse("testing"); len(commands) != 1 {
t.Errorf("expecting to get only one command, got '%v'", len(commands))
}
}

Expand Down
75 changes: 41 additions & 34 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func init() {
)

rootCmd.AddCommand(runCmd)

SetRunUsageFunc(run, runCmd)
}

// NewKoolRun creates a new handler for run logic with default dependencies
Expand All @@ -51,6 +53,11 @@ func (r *KoolRun) Execute(originalArgs []string) (err error) {
args []string
)

// look for kool.yml on current working directory
_ = r.parser.AddLookupPath(r.envStorage.Get("PWD"))
// look for kool.yml on kool folder within user home directory
_ = r.parser.AddLookupPath(path.Join(r.envStorage.Get("HOME"), "kool"))

script = originalArgs[0]
args = originalArgs[1:]

Expand Down Expand Up @@ -97,45 +104,45 @@ func NewRunCommand(run *KoolRun) (runCmd *cobra.Command) {
// after a non-flag arg, stop parsing flags
runCmd.Flags().SetInterspersed(false)

// look for kool.yml on current working directory
_ = run.parser.AddLookupPath(run.envStorage.Get("PWD"))
// look for kool.yml on kool folder within user home directory
_ = run.parser.AddLookupPath(path.Join(run.envStorage.Get("HOME"), "kool"))

var (
usageTempl string
err error
)

if usageTempl, err = getRunUsageTemplate(run, runCmd); err == nil {
runCmd.SetUsageTemplate(usageTempl)
} else if run.envStorage.IsTrue("KOOL_VERBOSE") {
run.Println("$ got an error trying to add available scripts to command usage template; error:", err.Error())
}

return
}

func getRunUsageTemplate(run *KoolRun, cmd *cobra.Command) (templ string, err error) {
var (
sb strings.Builder
scripts []string
)

if scripts, err = run.parser.ParseAvailableScripts(); err != nil {
return
}
// SetRunUsageFunc overrides usage function
func SetRunUsageFunc(run *KoolRun, runCmd *cobra.Command) {
originalUsageText := runCmd.UsageString()
runCmd.SetUsageFunc(getRunUsageFunc(run, originalUsageText))
}

sb.WriteString(cmd.UsageTemplate())
sb.WriteString("\n")
sb.WriteString("Available Scripts:\n")
func getRunUsageFunc(run *KoolRun, originalUsageText string) func(*cobra.Command) error {
return func(cmd *cobra.Command) (err error) {
var (
sb strings.Builder
scripts []string
)

// look for kool.yml on current working directory
_ = run.parser.AddLookupPath(run.envStorage.Get("PWD"))
// look for kool.yml on kool folder within user home directory
_ = run.parser.AddLookupPath(path.Join(run.envStorage.Get("HOME"), "kool"))

if scripts, err = run.parser.ParseAvailableScripts(); err != nil {
if run.envStorage.IsTrue("KOOL_VERBOSE") {
run.Println("$ got an error trying to add available scripts to command usage template; error:", err.Error())
}
return
}

for _, script := range scripts {
sb.WriteString(" ")
sb.WriteString(script)
sb.WriteString(originalUsageText)
Comment thread
fabriciojs marked this conversation as resolved.
sb.WriteString("\n")
}
sb.WriteString("Available Scripts:\n")

templ = sb.String()
return
for _, script := range scripts {
sb.WriteString(" ")
sb.WriteString(script)
sb.WriteString("\n")
}

run.Println(sb.String())
return
}
}
27 changes: 22 additions & 5 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,21 @@ func TestNewRunCommandUsageTemplate(t *testing.T) {
f := newFakeKoolRun([]builder.Command{}, nil)
f.parser.(*parser.FakeParser).MockScripts = []string{"testing_script"}
cmd := NewRunCommand(f)
SetRunUsageFunc(f, cmd)

usageTemplate := cmd.UsageTemplate()
cmd.SetArgs([]string{"--help"})

if !strings.Contains(usageTemplate, "testing_script") {
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing run command; error: %v", err)
}

if !f.out.(*shell.FakeOutputWriter).CalledPrintln {
t.Error("did not call Println for command usage")
}

usage := fmt.Sprintln(f.out.(*shell.FakeOutputWriter).Out...)

if !strings.Contains(usage, "testing_script") {
t.Error("did not find testing_script as available script on usage text")
}
}
Expand All @@ -248,10 +259,17 @@ func TestNewRunCommandFailingUsageTemplate(t *testing.T) {
f.envStorage.(*environment.FakeEnvStorage).Envs["KOOL_VERBOSE"] = "1"

cmd := NewRunCommand(f)
SetRunUsageFunc(f, cmd)

usageTemplate := cmd.UsageTemplate()
cmd.SetArgs([]string{"--help"})

if strings.Contains(usageTemplate, "testing_script") {
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing run command; error: %v", err)
}

output := strings.TrimSpace(fmt.Sprintln(f.out.(*shell.FakeOutputWriter).Out...))

if strings.Contains(output, "testing_script") {
t.Error("should not find testing_script as available script on usage text due to error on parsing scripts")
}

Expand All @@ -260,7 +278,6 @@ func TestNewRunCommandFailingUsageTemplate(t *testing.T) {
}

expected := "$ got an error trying to add available scripts to command usage template; error: error parse avaliable scripts"
output := strings.TrimSpace(fmt.Sprintln(f.out.(*shell.FakeOutputWriter).Out...))

if expected != output {
t.Errorf("expecting message '%s', got '%s'", expected, output)
Expand Down