Compare commits
No commits in common. "bf8d261cf3e27124f0959a23e4b520911498e625" and "b8a63f39f50ddf1a402acdb1eb698ae5c2e7b5f6" have entirely different histories.
bf8d261cf3
...
b8a63f39f5
@ -1,3 +0,0 @@
|
||||
kind: Added
|
||||
body: Hooks for Commands.[name]. Error, success, and final. [#12]
|
||||
time: 2024-11-14T21:01:48.823426401-06:00
|
@ -1,3 +0,0 @@
|
||||
kind: Fixed
|
||||
body: make command logger be used for errors, not just when running the command
|
||||
time: 2024-11-14T21:13:06.404134926-06:00
|
@ -7,4 +7,4 @@ steps:
|
||||
release:
|
||||
image: golangci/golangci-lint:v1.53.3
|
||||
commands:
|
||||
- golangci-lint run -v --timeout 5m
|
||||
- golangci-lint run -v
|
@ -391,11 +391,12 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
||||
// cmdListWorker
|
||||
func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<- string, opts *ConfigOpts) {
|
||||
// iterate over list to run
|
||||
res := CmdListResults{}
|
||||
for list := range jobs {
|
||||
res := CmdListResults{}
|
||||
fieldsMap := make(map[string]interface{})
|
||||
fieldsMap["list"] = list.Name
|
||||
var cmdLogger zerolog.Logger
|
||||
|
||||
cmdLog := opts.Logger.Info()
|
||||
|
||||
var count int // count of how many commands have been executed
|
||||
var cmdsRan []string // store the commands that have been executed
|
||||
@ -407,9 +408,17 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
|
||||
fieldsMap["cmd"] = opts.Cmds[cmd].Name
|
||||
cmdToRun := opts.Cmds[cmd]
|
||||
cmdLog.Fields(fieldsMap).Send()
|
||||
|
||||
cmdLogger = cmdToRun.generateLogger(opts)
|
||||
cmdLogger.Info().Fields(fieldsMap).Send()
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", cmd).Str("Host", "local machine").
|
||||
Logger()
|
||||
|
||||
if cmdToRun.Host != nil {
|
||||
cmdLogger = opts.Logger.With().
|
||||
Str("backy-cmd", cmd).Str("Host", *cmdToRun.Host).
|
||||
Logger()
|
||||
}
|
||||
|
||||
outputArr, runOutErr := cmdToRun.RunCmd(cmdLogger, opts)
|
||||
|
||||
@ -447,17 +456,17 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
tmpErr := msgTemps.err.Execute(&errMsg, errStruct)
|
||||
|
||||
if tmpErr != nil {
|
||||
cmdLogger.Err(tmpErr).Send()
|
||||
opts.Logger.Err(tmpErr).Send()
|
||||
}
|
||||
|
||||
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed", list.Name), errMsg.String())
|
||||
|
||||
if notifySendErr != nil {
|
||||
cmdLogger.Err(notifySendErr).Send()
|
||||
opts.Logger.Err(notifySendErr).Send()
|
||||
}
|
||||
}
|
||||
|
||||
cmdLogger.Err(runOutErr).Send()
|
||||
opts.Logger.Err(runOutErr).Send()
|
||||
|
||||
break
|
||||
} else {
|
||||
@ -480,14 +489,14 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, results chan<-
|
||||
tmpErr := msgTemps.success.Execute(&successMsg, successStruct)
|
||||
|
||||
if tmpErr != nil {
|
||||
cmdLogger.Err(tmpErr).Send()
|
||||
opts.Logger.Err(tmpErr).Send()
|
||||
break
|
||||
}
|
||||
|
||||
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeeded", list.Name), successMsg.String())
|
||||
|
||||
if err != nil {
|
||||
cmdLogger.Err(err).Send()
|
||||
opts.Logger.Err(err).Send()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -529,7 +538,7 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
||||
}
|
||||
close(listChan)
|
||||
|
||||
for a := 1; a <= configListsLen; a++ {
|
||||
for a := 0; a <= configListsLen; a++ {
|
||||
l := <-results
|
||||
|
||||
opts.Logger.Debug().Msg(l)
|
||||
@ -537,15 +546,11 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
||||
if l != "" {
|
||||
// execute error hooks
|
||||
opts.Logger.Debug().Msg("hooks are working")
|
||||
opts.Cmds[l].ExecuteHooks("error", opts)
|
||||
} else {
|
||||
// execute success hooks
|
||||
opts.Cmds[l].ExecuteHooks("success", opts)
|
||||
|
||||
}
|
||||
|
||||
// execute final hooks
|
||||
opts.Cmds[l].ExecuteHooks("final", opts)
|
||||
|
||||
}
|
||||
|
||||
@ -555,18 +560,20 @@ func (opts *ConfigOpts) RunListConfig(cron string) {
|
||||
func (config *ConfigOpts) ExecuteCmds(opts *ConfigOpts) {
|
||||
for _, cmd := range opts.executeCmds {
|
||||
cmdToRun := opts.Cmds[cmd]
|
||||
cmdLogger := cmdToRun.generateLogger(opts)
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", cmd).
|
||||
Logger()
|
||||
_, runErr := cmdToRun.RunCmd(cmdLogger, opts)
|
||||
if runErr != nil {
|
||||
opts.Logger.Err(runErr).Send()
|
||||
|
||||
cmdToRun.ExecuteHooks("error", opts)
|
||||
ExecuteHooks(*cmdToRun, "error", opts)
|
||||
} else {
|
||||
|
||||
cmdToRun.ExecuteHooks("success", opts)
|
||||
ExecuteHooks(*cmdToRun, "success", opts)
|
||||
}
|
||||
|
||||
cmdToRun.ExecuteHooks("final", opts)
|
||||
ExecuteHooks(*cmdToRun, "final", opts)
|
||||
}
|
||||
|
||||
opts.closeHostConnections()
|
||||
@ -610,7 +617,7 @@ func (c *ConfigOpts) closeHostConnections() {
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
func ExecuteHooks(cmd Command, hookType string, opts *ConfigOpts) {
|
||||
switch hookType {
|
||||
case "error":
|
||||
for _, v := range cmd.Hooks.Error {
|
||||
@ -621,35 +628,5 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
errCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
|
||||
case "success":
|
||||
for _, v := range cmd.Hooks.Success {
|
||||
successCmd := opts.Cmds[v]
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).
|
||||
Logger()
|
||||
successCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
case "final":
|
||||
for _, v := range cmd.Hooks.Final {
|
||||
finalCmd := opts.Cmds[v]
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).
|
||||
Logger()
|
||||
finalCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *Command) generateLogger(opts *ConfigOpts) zerolog.Logger {
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", cmd.Name).Str("Host", "local machine").
|
||||
Logger()
|
||||
|
||||
if cmd.Host != nil {
|
||||
cmdLogger = opts.Logger.With().
|
||||
Str("backy-cmd", cmd.Name).Str("Host", *cmd.Host).
|
||||
Logger()
|
||||
|
||||
}
|
||||
return cmdLogger
|
||||
}
|
||||
|
@ -298,14 +298,12 @@ func ReadConfig(opts *ConfigOpts) *ConfigOpts {
|
||||
|
||||
// process commands
|
||||
if err := processCmds(opts); err != nil {
|
||||
logging.ExitWithMSG(err.Error(), 1, &opts.Logger)
|
||||
log.Panic().Err(err).Send()
|
||||
}
|
||||
|
||||
if len(opts.executeLists) > 0 {
|
||||
for l := range opts.CmdConfigLists {
|
||||
if !contains(opts.executeLists, l) {
|
||||
delete(opts.CmdConfigLists, l)
|
||||
}
|
||||
for l := range opts.CmdConfigLists {
|
||||
if !contains(opts.executeLists, l) {
|
||||
delete(opts.CmdConfigLists, l)
|
||||
}
|
||||
}
|
||||
|
||||
@ -449,6 +447,9 @@ func GetVaultKey(str string, opts *ConfigOpts, log zerolog.Logger) string {
|
||||
func processCmds(opts *ConfigOpts) error {
|
||||
// process commands
|
||||
for cmdName, cmd := range opts.Cmds {
|
||||
cmd.hookRefs = map[string]map[string]*Command{}
|
||||
cmd.hookRefs["error"] = map[string]*Command{}
|
||||
cmd.hookRefs["success"] = map[string]*Command{}
|
||||
|
||||
if cmd.Name == "" {
|
||||
cmd.Name = cmdName
|
||||
@ -457,19 +458,19 @@ func processCmds(opts *ConfigOpts) error {
|
||||
hooks := cmd.Hooks
|
||||
// resolve hooks
|
||||
if hooks != nil {
|
||||
opts.Logger.Debug().Msg("Hooks found")
|
||||
|
||||
processHookSuccess := processHooks(cmd, hooks.Error, opts, "error")
|
||||
if processHookSuccess != nil {
|
||||
return processHookSuccess
|
||||
errHook, hookRefs, processHookErr := processHooks(hooks.Error, opts.Cmds, "error")
|
||||
if !processHookErr {
|
||||
return fmt.Errorf("error in command %s hook list: hook command %s not found", cmd.Name, errHook)
|
||||
}
|
||||
processHookSuccess = processHooks(cmd, hooks.Success, opts, "success")
|
||||
if processHookSuccess != nil {
|
||||
return processHookSuccess
|
||||
}
|
||||
processHookSuccess = processHooks(cmd, hooks.Final, opts, "final")
|
||||
if processHookSuccess != nil {
|
||||
return processHookSuccess
|
||||
cmd.hookRefs["error"] = hookRefs
|
||||
|
||||
successHook, SuccessHookRefs, processHookSuccess := processHooks(hooks.Error, opts.Cmds, "error")
|
||||
if !processHookSuccess {
|
||||
return fmt.Errorf("error in command %s hook list: hook command %s not found", cmd.Name, successHook)
|
||||
}
|
||||
cmd.hookRefs["success"] = SuccessHookRefs
|
||||
}
|
||||
|
||||
// resolve hosts
|
||||
@ -502,31 +503,34 @@ func processCmds(opts *ConfigOpts) error {
|
||||
//
|
||||
// Returns the following:
|
||||
//
|
||||
// An error, if any, if the command is not found
|
||||
func processHooks(cmd *Command, hooks []string, opts *ConfigOpts, hookType string) error {
|
||||
// 1. command string
|
||||
// 2. each hook type's command map
|
||||
// 2. a bool which determines if the command is valid
|
||||
func processHooks(hooks []string, cmds map[string]*Command, hookType string) (hook string, hookRefs map[string]*Command, hookCmdFound bool) {
|
||||
// fmt.Printf("%v\n", hooks)
|
||||
// for _, v := range cmds {
|
||||
|
||||
// fmt.Printf("CmdName=%v\n", v.Name)
|
||||
// fmt.Printf("Cmd=%v\n", v.Cmd)
|
||||
// }
|
||||
// initialize hook type
|
||||
var hookCmdFound bool
|
||||
cmd.hookRefs = map[string]map[string]*Command{}
|
||||
cmd.hookRefs[hookType] = map[string]*Command{}
|
||||
|
||||
for _, hook := range hooks {
|
||||
|
||||
hookRefs = make(map[string]*Command)
|
||||
// hookRefs[hookType] = map[string]*Command{}
|
||||
for _, hook = range hooks {
|
||||
var hookCmd *Command
|
||||
// TODO: match by Command.Name
|
||||
|
||||
hookCmd, hookCmdFound = opts.Cmds[hook]
|
||||
hookCmd, hookCmdFound = cmds[hook]
|
||||
|
||||
if !hookCmdFound {
|
||||
return fmt.Errorf("error in command %s hook %s list: command %s not found", cmd.Name, hookType, hook)
|
||||
return
|
||||
}
|
||||
|
||||
cmd.hookRefs[hookType][hook] = hookCmd
|
||||
hookRefs[hook] = hookCmd
|
||||
|
||||
// Recursive, decide if this is good
|
||||
// if hookCmd.hookRefs == nil {
|
||||
// }
|
||||
// hookRef[hookType][h] = hookCmd
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
@ -197,9 +197,9 @@ type (
|
||||
}
|
||||
|
||||
Hooks struct {
|
||||
Error []string `yaml:"error,omitempty"`
|
||||
Success []string `yaml:"success,omitempty"`
|
||||
Final []string `yaml:"final,omitempty"`
|
||||
Error []string `yaml:"error,omitempty"`
|
||||
SuccessHooks []string `yaml:"success,omitempty"`
|
||||
FinalHooks []string `yaml:"final,omitempty"`
|
||||
}
|
||||
|
||||
CmdListResults struct {
|
||||
|
Loading…
Reference in New Issue
Block a user