Skip to content

Commit a3cfcfb

Browse files
committed
Add --tasks which lists all tasks
1 parent 4b22533 commit a3cfcfb

File tree

6 files changed

+41
-15
lines changed

6 files changed

+41
-15
lines changed

cmd/cli/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
func main() {
1212
opts := app.NewCliOptions()
13-
handleGlobalOptions(&opts)
1413

1514
cfg, err := app.ReadYamlConfig()
1615
if err != nil {
@@ -26,6 +25,9 @@ func main() {
2625
p := app.NewParser(cfg, &opts, &fs)
2726
p.Bootstrap()
2827

28+
// Global options
29+
handleGlobalOptions(&opts, &p)
30+
2931
l := app.NewLockfile(p.GetFilePaths(), &opts, &fs)
3032
l.Bootstrap()
3133

cmd/cli/util.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import (
88
)
99

1010
// handleGlobalOptions executes the global options logic.
11-
func handleGlobalOptions(opts *app.Options) {
12-
err := opts.InitHandler()
13-
if err != nil {
14-
fmt.Println(err)
15-
os.Exit(1)
11+
func handleGlobalOptions(opts *app.Options, p *app.Parseable) {
12+
if code, err := opts.InitHandler(); code != -1 {
13+
if err != nil {
14+
fmt.Println(err)
15+
}
16+
17+
os.Exit(code)
18+
}
19+
20+
if code := opts.TasksHandler(p); code != -1 {
21+
os.Exit(code)
1622
}
1723
}

internal/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var spinnerCfg = yacspin.Config{
3131
}
3232

3333
type Executor struct {
34-
parser Parser
34+
parser Parseable
3535
lockfile Lockfile
3636
spinner *yacspin.Spinner
3737
options Options
@@ -41,7 +41,7 @@ type Executor struct {
4141
}
4242

4343
// Executor constructor.
44-
func NewExecutor(p *Parser, l *Lockfile, opts *Options, proc Process, fs FileSystem, ctx *context.Context) Executor {
44+
func NewExecutor(p *Parseable, l *Lockfile, opts *Options, proc Process, fs FileSystem, ctx *context.Context) Executor {
4545
spinner, _ := yacspin.New(spinnerCfg)
4646

4747
return Executor{

internal/executor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/mock"
1010
)
1111

12-
func getDependencies(t *testing.T, opts *Options) (*Parser, *Lockfile, *tests.Process, FileSystem) {
12+
func getDependencies(t *testing.T, opts *Options) (*Parseable, *Lockfile, *tests.Process, FileSystem) {
1313
fsMock := mockCacheDoesNotExist(t)
1414
fsMock.On("FileExists", mock.Anything).Return(false)
1515
fsMock.On("WriteFile", mock.Anything, mock.Anything, mock.Anything).Return(nil)

internal/options.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package internal
22

33
import (
4+
"fmt"
5+
46
"github.com/docopt/docopt-go"
57
)
68

@@ -13,11 +15,13 @@ Usage:
1315
goke -i | --init
1416
goke -h | --help
1517
goke -v | --version
18+
goke -t | --tasks
1619
1720
Options:
1821
-h --help Show this screen
1922
-v --version Show version
2023
-i --init Creates a goke.yaml file in the current directory
24+
-t --tasks Outputs a list of all task names
2125
-w --watch Run task in watch mode
2226
-c --no-cache Clears the program's cache
2327
-f --force Runs the task even if files have not been changed
@@ -32,6 +36,7 @@ type Options struct {
3236
Quiet bool `docopt:"-q,--quiet"`
3337
Args []string `docopt:"-a,--args"`
3438
Init bool `docopt:"-i,--init"`
39+
Tasks bool `docopt:"-t,--tasks"`
3540
}
3641

3742
func NewCliOptions() Options {
@@ -43,15 +48,28 @@ func NewCliOptions() Options {
4348
return opts
4449
}
4550

46-
func (opts *Options) InitHandler() error {
51+
func (opts *Options) InitHandler() (int, error) {
4752
if !opts.Init {
48-
return nil
53+
return -1, nil
4954
}
5055

5156
err := CreateGokeConfig()
5257
if err != nil && !opts.Quiet {
53-
return err
58+
return 1, err
59+
}
60+
61+
return 0, nil
62+
}
63+
64+
func (opts *Options) TasksHandler(p *Parseable) int {
65+
if !opts.Tasks {
66+
return -1
67+
}
68+
69+
parser := (*p).(*parser)
70+
for taskName := range parser.Tasks {
71+
fmt.Println(taskName)
5472
}
5573

56-
return nil
74+
return 0
5775
}

internal/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"gopkg.in/yaml.v3"
1212
)
1313

14-
type Parser interface {
14+
type Parseable interface {
1515
Bootstrap()
1616
GetGlobal() *Global
1717
GetTask(string) (Task, bool)
@@ -58,7 +58,7 @@ var parserString string
5858

5959
// NewParser creates a parser instance which can be either a blank one,
6060
// or one provided from the cache, which gets deserialized.
61-
func NewParser(cfg string, opts *Options, fs FileSystem) Parser {
61+
func NewParser(cfg string, opts *Options, fs FileSystem) Parseable {
6262
p := parser{}
6363
p.fs = fs
6464
p.config = cfg

0 commit comments

Comments
 (0)