Skip to content

Commit e85e255

Browse files
committed
Tweaks
1 parent 2bf21f3 commit e85e255

7 files changed

Lines changed: 46 additions & 320 deletions

File tree

commands/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ var AddCommands AddCommandsFN = func(root *cobra.Command) {
3434
AddKoolPreset(root)
3535
AddKoolRestart(root)
3636
AddKoolRun(root)
37-
AddKoolScripts(root)
3837
AddKoolSelfUpdate(root)
3938
AddKoolShare(root)
4039
AddKoolStart(root)

commands/root_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ func TestAddCommands(t *testing.T) {
249249
"preset": false,
250250
"restart": false,
251251
"run": false,
252-
"scripts": false,
253252
"self-update": false,
254253
"share": false,
255254
"start": false,

commands/run.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"encoding/json"
45
"errors"
56
"kool-dev/kool/core/builder"
67
"kool-dev/kool/core/environment"
@@ -15,6 +16,7 @@ import (
1516
// KoolRunFlags holds the flags for the run command
1617
type KoolRunFlags struct {
1718
EnvVariables []string
19+
JSON bool
1820
}
1921

2022
// KoolRun holds handlers and functions to implement the run command logic
@@ -48,7 +50,7 @@ func AddKoolRun(root *cobra.Command) {
4850
func NewKoolRun() *KoolRun {
4951
return &KoolRun{
5052
*newDefaultKoolService(),
51-
&KoolRunFlags{[]string{}},
53+
&KoolRunFlags{[]string{}, false},
5254
parser.NewParser(),
5355
environment.NewEnvStorage(),
5456
shell.NewPromptSelect(),
@@ -58,7 +60,15 @@ func NewKoolRun() *KoolRun {
5860

5961
// Execute runs the run logic with incoming arguments.
6062
func (r *KoolRun) Execute(originalArgs []string) (err error) {
63+
// look for kool.yml on current working directory
64+
_ = r.parser.AddLookupPath(r.env.Get("PWD"))
65+
// look for kool.yml on kool folder within user home directory
66+
_ = r.parser.AddLookupPath(path.Join(r.env.Get("HOME"), "kool"))
67+
6168
if len(originalArgs) == 0 {
69+
if r.Flags.JSON {
70+
return r.printScriptsJSON("")
71+
}
6272
r.shell.Info("\nAvailable scripts:\n")
6373
scripts := compListScripts("", r)
6474
for _, cmd := range scripts {
@@ -74,11 +84,6 @@ func (r *KoolRun) Execute(originalArgs []string) (err error) {
7484
args []string = originalArgs[1:]
7585
)
7686

77-
// look for kool.yml on current working directory
78-
_ = r.parser.AddLookupPath(r.env.Get("PWD"))
79-
// look for kool.yml on kool folder within user home directory
80-
_ = r.parser.AddLookupPath(path.Join(r.env.Get("HOME"), "kool"))
81-
8287
if err = r.parseScript(script); err != nil {
8388
return
8489
}
@@ -125,6 +130,7 @@ A single-line SCRIPT can be run with optional arguments.`,
125130
}
126131

127132
runCmd.Flags().StringArrayVarP(&run.Flags.EnvVariables, "env", "e", []string{}, "Environment variables.")
133+
runCmd.Flags().BoolVar(&run.Flags.JSON, "json", false, "Output available scripts as JSON (use without script argument)")
128134

129135
// after a non-flag arg, stop parsing flags
130136
runCmd.Flags().SetInterspersed(false)
@@ -246,3 +252,31 @@ func compListScripts(toComplete string, run *KoolRun) (scripts []string) {
246252

247253
return
248254
}
255+
256+
func (r *KoolRun) printScriptsJSON(filter string) (err error) {
257+
var details []parser.ScriptDetail
258+
if details, err = r.parser.ParseAvailableScriptsDetails(filter); err != nil {
259+
return
260+
}
261+
262+
if details == nil {
263+
details = []parser.ScriptDetail{}
264+
}
265+
266+
for i := range details {
267+
if details[i].Comments == nil {
268+
details[i].Comments = []string{}
269+
}
270+
if details[i].Commands == nil {
271+
details[i].Commands = []string{}
272+
}
273+
}
274+
275+
var payload []byte
276+
if payload, err = json.Marshal(details); err != nil {
277+
return
278+
}
279+
280+
r.Shell().Println(string(payload))
281+
return nil
282+
}

commands/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
func newFakeKoolRun(mockParsedCommands map[string][]builder.Command, mockParseError map[string]error) *KoolRun {
1919
return &KoolRun{
2020
*(newDefaultKoolService().Fake()),
21-
&KoolRunFlags{[]string{}},
21+
&KoolRunFlags{[]string{}, false},
2222
&parser.FakeParser{MockParsedCommands: mockParsedCommands, MockParseError: mockParseError},
2323
environment.NewFakeEnvStorage(),
2424
&shell.FakePromptSelect{},

commands/scripts.go

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)