-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathbeta_cmd.go
More file actions
78 lines (63 loc) · 2.28 KB
/
beta_cmd.go
File metadata and controls
78 lines (63 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package beta
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stateful/runme/v3/internal/cmd/beta/server"
"github.com/stateful/runme/v3/internal/config"
"github.com/stateful/runme/v3/internal/config/autoconfig"
)
type commonFlags struct {
categories []string
filename string
}
func BetaCmd() *cobra.Command {
cFlags := &commonFlags{}
cmd := cobra.Command{
Use: "beta",
Short: "Experimental runme commands.",
Long: `The new version of the runme command-line interface.
All commands are experimental and not yet ready for production use.
All commands use the runme.yaml configuration file.`,
Hidden: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.Invoke(func(cfg *config.Config) error {
// Override the filename if provided.
if cFlags.filename != "" {
cfg.Core.Filename = cFlags.filename
}
// Add a filter to run only tasks from the specified categories.
if len(cFlags.categories) > 0 {
cfg.Repo.Filters = append(cfg.Repo.Filters, &config.Filter{
Type: config.FilterTypeBlock,
Condition: `len(intersection(categories, extra.categories)) > 0`,
Extra: map[string]interface{}{"categories": cFlags.categories},
})
}
return nil
})
},
}
// The idea for persistent flags on the "beta" command is to
// interpret them in PersistentPreRunE() and merge with [config.Config].
// Use them sparingly and only for the cases when it does not make sense
// to alter the configuration file.
pFlags := cmd.PersistentFlags()
pFlags.StringVar(&cFlags.filename, "filename", "", "Name of the Markdown file to run blocks from.")
pFlags.StringSliceVar(&cFlags.categories, "category", nil, "Run blocks only from listed categories.")
// Hide all persistent flags from the root command.
// "beta" is a completely different set of commands and
// should not inherit any flags from the root command.
originalUsageFunc := cmd.UsageFunc()
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
pflags := cmd.Root().PersistentFlags()
pflags.VisitAll(func(f *pflag.Flag) {
f.Hidden = true
})
return originalUsageFunc(cmd)
})
cmd.AddCommand(listCmd(cFlags))
cmd.AddCommand(printCmd(cFlags))
cmd.AddCommand(server.Cmd())
cmd.AddCommand(runCmd(cFlags))
return &cmd
}