Skip to content

Commit 32596ec

Browse files
Add new info command
1 parent 2f163cd commit 32596ec

3 files changed

Lines changed: 213 additions & 0 deletions

File tree

commands/captainhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ func init() {
7171
rootCmd.AddCommand(setupInitCommand())
7272
rootCmd.AddCommand(setupInstallCommand())
7373
rootCmd.AddCommand(setupUninstallCommand())
74+
rootCmd.AddCommand(setupInfoCommand())
7475
rootCmd.AddCommand(hookCommand)
7576
}

commands/info.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package commands
2+
3+
import (
4+
"github.com/captainhook-go/captainhook/exec"
5+
"github.com/captainhook-go/captainhook/git"
6+
"github.com/captainhook-go/captainhook/io"
7+
"github.com/spf13/cobra"
8+
"os"
9+
)
10+
11+
func setupInfoCommand() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "info",
14+
Short: "Displays configuration details",
15+
Long: "Displays configuration details",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
listActions, _ := cmd.Flags().GetBool("list-actions")
18+
listConditions, _ := cmd.Flags().GetBool("list-conditions")
19+
listOptions, _ := cmd.Flags().GetBool("list-options")
20+
extended, _ := cmd.Flags().GetBool("extended")
21+
22+
conf, err := setUpConfig(cmd)
23+
if err != nil {
24+
DisplayCommandError(err)
25+
}
26+
27+
repo, errRepo := git.NewRepository(conf.GitDirectory())
28+
if errRepo != nil {
29+
DisplayCommandError(errRepo)
30+
}
31+
32+
appIO := io.NewDefaultIO(conf.Verbosity(), map[string]string{}, map[string]string{})
33+
34+
info := exec.NewConfigInfo(appIO, conf, repo)
35+
info.Display("actions", listActions)
36+
info.Display("conditions", listConditions)
37+
info.Display("options", listOptions)
38+
info.Extended(extended)
39+
instError := info.Run()
40+
41+
if instError != nil {
42+
os.Exit(1)
43+
}
44+
},
45+
}
46+
47+
setUpInfoFlags(cmd)
48+
configurationAware(cmd)
49+
repositoryAware(cmd)
50+
51+
return cmd
52+
}
53+
54+
func setUpInfoFlags(cmd *cobra.Command) {
55+
cmd.Flags().BoolP("list-actions", "a", false, "list actions")
56+
cmd.Flags().BoolP("list-conditions", "p", false, "list all conditions")
57+
cmd.Flags().BoolP("list-options", "o", false, "list all options and args")
58+
cmd.Flags().BoolP("extended", "e", false, "show detailed information")
59+
}

exec/config_info.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package exec
2+
3+
import (
4+
"fmt"
5+
"github.com/captainhook-go/captainhook/configuration"
6+
"github.com/captainhook-go/captainhook/git"
7+
"github.com/captainhook-go/captainhook/info"
8+
"github.com/captainhook-go/captainhook/io"
9+
"sort"
10+
"strings"
11+
)
12+
13+
// ConfigInfo
14+
// blah blah
15+
type ConfigInfo struct {
16+
appIO io.IO
17+
config *configuration.Configuration
18+
repo git.Repo
19+
show map[string]bool
20+
isExtended bool
21+
hook string
22+
}
23+
24+
// Display allows to change the displayed details
25+
func (c *ConfigInfo) Display(what string, show bool) {
26+
if show {
27+
c.show[what] = show
28+
}
29+
}
30+
31+
// Extended gives you an installation status
32+
func (c *ConfigInfo) Extended(ext bool) {
33+
c.isExtended = ext
34+
}
35+
36+
// Hook restricts the info to show only information of given hooks
37+
func (c *ConfigInfo) Hook(hook string) {
38+
c.hook = hook
39+
}
40+
41+
func (c *ConfigInfo) Run() error {
42+
hooks := info.GetValidHooks()
43+
sort.Strings(hooks)
44+
for _, hook := range hooks {
45+
c.displayHook(c.config.HookConfig(hook))
46+
}
47+
return nil
48+
}
49+
50+
func (c *ConfigInfo) displayHook(config *configuration.Hook) {
51+
if c.shouldHookBeDisplayed(config.Name()) {
52+
c.appIO.Write("<ok>"+config.Name()+"</ok>", !c.isExtended, io.NORMAL)
53+
c.displayExtended(config)
54+
c.displayActions(config)
55+
}
56+
}
57+
58+
func (c *ConfigInfo) displayExtended(config *configuration.Hook) {
59+
if c.isExtended {
60+
c.appIO.Write(
61+
" "+strings.Repeat("-", 50-len(config.Name()))+
62+
"--[enabled: "+c.yesOrNo(config.IsEnabled())+
63+
", installed: "+c.yesOrNo(c.repo.HookExists(config.Name()))+"]",
64+
true,
65+
io.NORMAL,
66+
)
67+
}
68+
}
69+
70+
func (c *ConfigInfo) displayActions(config *configuration.Hook) {
71+
for _, action := range config.GetActions() {
72+
c.displayaction(action)
73+
}
74+
}
75+
76+
func (c *ConfigInfo) displayaction(action *configuration.Action) {
77+
c.appIO.Write(" - <info>"+action.Run()+"</info>", true, io.NORMAL)
78+
c.displayOptions(action.Options())
79+
c.displayConditions(action.Conditions(), "")
80+
}
81+
82+
func (c *ConfigInfo) displayOptions(opts *configuration.Options) {
83+
if len(opts.All()) == 0 {
84+
return
85+
}
86+
if c.shouldDisplay("options") {
87+
c.appIO.Write(" <comment>Options:</comment>", true, io.NORMAL)
88+
for key, value := range opts.All() {
89+
c.displayOption(key, value, "")
90+
}
91+
}
92+
}
93+
94+
func (c *ConfigInfo) displayOption(key string, value interface{}, prefix string) {
95+
c.appIO.Write(prefix+" - "+key+": "+fmt.Sprintf("%v", value), true, io.NORMAL)
96+
}
97+
98+
func (c *ConfigInfo) displayConditions(conditions []*configuration.Condition, prefix string) {
99+
if len(conditions) == 0 {
100+
return
101+
}
102+
if c.shouldDisplay("conditions") {
103+
if prefix == "" {
104+
c.appIO.Write(" <comment>Conditions:</comment>", true, io.NORMAL)
105+
}
106+
for _, condition := range conditions {
107+
c.displayCondition(condition, prefix)
108+
}
109+
}
110+
}
111+
112+
func (c *ConfigInfo) displayCondition(condition *configuration.Condition, prefix string) {
113+
c.appIO.Write(prefix+" - "+condition.Run(), true, io.NORMAL)
114+
if c.shouldDisplay("options") {
115+
if len(condition.Options().All()) == 0 {
116+
return
117+
}
118+
c.appIO.Write(prefix+" <comment>Args:</comment>", true, io.NORMAL)
119+
for key, value := range condition.Options().All() {
120+
c.displayOption(key, value, prefix+" ")
121+
}
122+
}
123+
}
124+
125+
func (c *ConfigInfo) shouldHookBeDisplayed(hook string) bool {
126+
return c.config.IsHookEnabled(hook)
127+
}
128+
129+
func (c *ConfigInfo) shouldDisplay(configPart string) bool {
130+
if len(c.show) == 0 {
131+
return true
132+
}
133+
_, ok := c.show[configPart]
134+
return ok
135+
}
136+
137+
func (c *ConfigInfo) yesOrNo(val bool) string {
138+
if val {
139+
return "✅ "
140+
}
141+
return "❌ "
142+
}
143+
144+
func NewConfigInfo(appIO io.IO, config *configuration.Configuration, repo git.Repo) *ConfigInfo {
145+
return &ConfigInfo{
146+
appIO: appIO,
147+
config: config,
148+
repo: repo,
149+
show: map[string]bool{},
150+
isExtended: false,
151+
hook: "",
152+
}
153+
}

0 commit comments

Comments
 (0)