|
| 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