fix error hooks and add listing everything
This commit is contained in:
@@ -944,7 +944,6 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
cmdLogger.Info().Msgf("Running error hook command %s", v)
|
||||
// URGENT: Never returns
|
||||
_, _ = errCmd.RunCmd(cmdLogger, opts)
|
||||
return
|
||||
}
|
||||
|
||||
case "success":
|
||||
|
||||
@@ -101,6 +101,10 @@ func (r *RemoteFileCommandExecutor) copyFile(source, destination string, Perms f
|
||||
return nil
|
||||
}
|
||||
|
||||
if sshClient == nil {
|
||||
return fmt.Errorf("SSH Client is nil")
|
||||
}
|
||||
|
||||
client, err := sftp.NewClient(sshClient)
|
||||
|
||||
if err != nil {
|
||||
|
||||
+50
-12
@@ -36,6 +36,8 @@ func (opts *ConfigOpts) ListCommand(cmd string) {
|
||||
// print the command's information
|
||||
if cmdFound {
|
||||
|
||||
print("Backy Command: ")
|
||||
println(cmd)
|
||||
println("Command: ")
|
||||
|
||||
print(cmdInfo.Cmd)
|
||||
@@ -45,17 +47,57 @@ func (opts *ConfigOpts) ListCommand(cmd string) {
|
||||
print(v) // print command arg
|
||||
}
|
||||
|
||||
// is it remote or local
|
||||
if !IsHostLocal(cmdInfo.Host) {
|
||||
if cmdInfo.Type.String() != "" {
|
||||
println()
|
||||
print("Host: ", cmdInfo.Host)
|
||||
print("Type: ", cmdInfo.Type.String())
|
||||
println()
|
||||
}
|
||||
|
||||
if cmdInfo.Hosts != nil {
|
||||
for n, h := range cmdInfo.Hosts {
|
||||
println()
|
||||
fmt.Printf("Host %d: %s", n, h)
|
||||
println()
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
println()
|
||||
print("Host: Runs on Local Machine\n\n")
|
||||
// is it remote or local
|
||||
if !IsHostLocal(cmdInfo.Host) {
|
||||
println()
|
||||
print("Host: ", cmdInfo.Host)
|
||||
println()
|
||||
|
||||
} else {
|
||||
|
||||
println()
|
||||
print("Host: Runs on Local Machine\n\n")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(cmdInfo.Environment) > 0 {
|
||||
fmt.Print("Environment: ")
|
||||
for _, env := range cmdInfo.Environment {
|
||||
fmt.Printf("%s ", env)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
if cmdInfo.PackageManager != "" {
|
||||
fmt.Printf("\nPackage Manager: %s\n", cmdInfo.PackageManager)
|
||||
if len(cmdInfo.Packages) > 0 {
|
||||
for _, pkg := range cmdInfo.Packages {
|
||||
fmt.Printf("Package: %v\n", pkg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cmdInfo.Username != "" || cmdInfo.UserID != "" {
|
||||
fmt.Println("\nUser Settings:")
|
||||
fmt.Printf("Username: %s\n", cmdInfo.Username)
|
||||
fmt.Printf("UserID: %s\n", cmdInfo.UserID)
|
||||
}
|
||||
|
||||
if cmdInfo.Dir != nil {
|
||||
@@ -64,11 +106,6 @@ func (opts *ConfigOpts) ListCommand(cmd string) {
|
||||
println()
|
||||
}
|
||||
|
||||
if cmdInfo.Type.String() != "" {
|
||||
print("Type: ", cmdInfo.Type.String())
|
||||
println()
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
fmt.Printf("Command %s not found. Check spelling.\n", cmd)
|
||||
@@ -100,9 +137,10 @@ func (opts *ConfigOpts) ListCommandList(list string) {
|
||||
println("List: ", list)
|
||||
println()
|
||||
|
||||
for _, v := range listInfo.Order {
|
||||
for n, cmd := range listInfo.Order {
|
||||
println()
|
||||
opts.ListCommand(v)
|
||||
fmt.Printf("Command %d: \n", n+1)
|
||||
opts.ListCommand(cmd)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
@@ -2,6 +2,7 @@ package backy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
@@ -75,6 +76,17 @@ func (metricFile *MetricFile) SaveToFile() error {
|
||||
}
|
||||
|
||||
func LoadMetricsFromFile(filename string) (*MetricFile, error) {
|
||||
if err := testFile(filename); err != nil {
|
||||
|
||||
_, createErr := os.Create(filename)
|
||||
if createErr != nil {
|
||||
return nil, fmt.Errorf("error creating file and root cause: %w", err)
|
||||
}
|
||||
metricData := NewMetricsFromFile(filename)
|
||||
data, _ := json.MarshalIndent(metricData, "", " ")
|
||||
os.WriteFile(filename, data, 0600)
|
||||
return metricData, nil
|
||||
}
|
||||
jsonData, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
+57
-62
@@ -1,67 +1,62 @@
|
||||
package backy
|
||||
|
||||
// import (
|
||||
// "testing"
|
||||
// "time"
|
||||
// )
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// func TestAddingMetricsForCommand(t *testing.T) {
|
||||
func TestAddingMetricsForCommand(t *testing.T) {
|
||||
// Create a new MetricFile
|
||||
metricFile := NewMetricsFromFile("test_metrics.json")
|
||||
metricFile, err := LoadMetricsFromFile(metricFile.Filename)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load metrics from file: %v", err)
|
||||
}
|
||||
// Add metrics for a command
|
||||
commandName := "test_command"
|
||||
if metricFile.CommandMetrics != nil {
|
||||
if _, exists := metricFile.CommandMetrics[commandName]; !exists {
|
||||
metricFile.CommandMetrics[commandName] = NewMetrics()
|
||||
}
|
||||
}
|
||||
// Update the metrics for the command
|
||||
executionTime := 1.8 // Example execution time in seconds
|
||||
success := true // Example success status
|
||||
metricFile.CommandMetrics[commandName].Update(success, executionTime, time.Now())
|
||||
// Check if the metrics were updated correctly
|
||||
if metricFile.CommandMetrics[commandName].SuccessfulExecutions > 50 {
|
||||
t.Errorf("Expected 1 successful execution, got %d", metricFile.CommandMetrics[commandName].SuccessfulExecutions)
|
||||
}
|
||||
if metricFile.CommandMetrics[commandName].TotalExecutions > 50 {
|
||||
t.Errorf("Expected 1 total execution, got %d", metricFile.CommandMetrics[commandName].TotalExecutions)
|
||||
}
|
||||
if metricFile.CommandMetrics[commandName].TotalExecutionTime != executionTime {
|
||||
t.Errorf("Expected execution time %f, got %f", executionTime, metricFile.CommandMetrics[commandName].TotalExecutionTime)
|
||||
}
|
||||
err = metricFile.SaveToFile()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to save metrics to file: %v", err)
|
||||
}
|
||||
listName := "test_list"
|
||||
if _, exists := metricFile.ListMetrics[listName]; !exists {
|
||||
metricFile.ListMetrics[listName] = NewMetrics()
|
||||
}
|
||||
// Update the metrics for the list
|
||||
metricFile.ListMetrics[listName].Update(success, executionTime, time.Now())
|
||||
if metricFile.ListMetrics[listName].SuccessfulExecutions > 50 {
|
||||
t.Errorf("Expected 1 successful execution for list, got %d", metricFile.ListMetrics[listName].SuccessfulExecutions)
|
||||
}
|
||||
if metricFile.ListMetrics[listName].TotalExecutions > 50 {
|
||||
t.Errorf("Expected 1 total execution for list, got %d", metricFile.ListMetrics[listName].TotalExecutions)
|
||||
}
|
||||
if metricFile.ListMetrics[listName].TotalExecutionTime > executionTime {
|
||||
t.Errorf("Expected execution time %f for list, got %f", executionTime, metricFile.ListMetrics[listName].TotalExecutionTime)
|
||||
}
|
||||
|
||||
// // Create a new MetricFile
|
||||
// metricFile := NewMetricsFromFile("test_metrics.json")
|
||||
// Save the metrics to a file
|
||||
err = metricFile.SaveToFile()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to save metrics to file: %v", err)
|
||||
}
|
||||
|
||||
// metricFile, err := LoadMetricsFromFile(metricFile.Filename)
|
||||
// if err != nil {
|
||||
// t.Errorf("Failed to load metrics from file: %v", err)
|
||||
// }
|
||||
|
||||
// // Add metrics for a command
|
||||
// commandName := "test_command"
|
||||
// if _, exists := metricFile.CommandMetrics[commandName]; !exists {
|
||||
// metricFile.CommandMetrics[commandName] = NewMetrics()
|
||||
// }
|
||||
|
||||
// // Update the metrics for the command
|
||||
// executionTime := 1.8 // Example execution time in seconds
|
||||
// success := true // Example success status
|
||||
// metricFile.CommandMetrics[commandName].Update(success, executionTime, time.Now())
|
||||
|
||||
// // Check if the metrics were updated correctly
|
||||
// if metricFile.CommandMetrics[commandName].SuccessfulExecutions > 50 {
|
||||
// t.Errorf("Expected 1 successful execution, got %d", metricFile.CommandMetrics[commandName].SuccessfulExecutions)
|
||||
// }
|
||||
// if metricFile.CommandMetrics[commandName].TotalExecutions > 50 {
|
||||
// t.Errorf("Expected 1 total execution, got %d", metricFile.CommandMetrics[commandName].TotalExecutions)
|
||||
// }
|
||||
// // if metricFile.CommandMetrics[commandName].TotalExecutionTime != executionTime {
|
||||
// // t.Errorf("Expected execution time %f, got %f", executionTime, metricFile.CommandMetrics[commandName].TotalExecutionTime)
|
||||
// // }
|
||||
|
||||
// err = metricFile.SaveToFile()
|
||||
// if err != nil {
|
||||
// t.Errorf("Failed to save metrics to file: %v", err)
|
||||
// }
|
||||
|
||||
// listName := "test_list"
|
||||
// if _, exists := metricFile.ListMetrics[listName]; !exists {
|
||||
// metricFile.ListMetrics[listName] = NewMetrics()
|
||||
// }
|
||||
// // Update the metrics for the list
|
||||
// metricFile.ListMetrics[listName].Update(success, executionTime, time.Now())
|
||||
// if metricFile.ListMetrics[listName].SuccessfulExecutions > 50 {
|
||||
// t.Errorf("Expected 1 successful execution for list, got %d", metricFile.ListMetrics[listName].SuccessfulExecutions)
|
||||
// }
|
||||
// if metricFile.ListMetrics[listName].TotalExecutions > 50 {
|
||||
// t.Errorf("Expected 1 total execution for list, got %d", metricFile.ListMetrics[listName].TotalExecutions)
|
||||
// }
|
||||
// // if metricFile.ListMetrics[listName].TotalExecutionTime > executionTime {
|
||||
// // t.Errorf("Expected execution time %f for list, got %f", executionTime, metricFile.ListMetrics[listName].TotalExecutionTime)
|
||||
// // }
|
||||
|
||||
// // Save the metrics to a file
|
||||
// err = metricFile.SaveToFile()
|
||||
// if err != nil {
|
||||
// t.Errorf("Failed to save metrics to file: %v", err)
|
||||
// }
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
+3
-2
@@ -723,7 +723,8 @@ func (command *Command) prepareScriptBuffer() (*bytes.Buffer, error) {
|
||||
|
||||
buffer.WriteString(command.Cmd)
|
||||
for _, arg := range command.Args {
|
||||
buffer.WriteString(" " + arg)
|
||||
buffer.WriteString(" ")
|
||||
buffer.WriteString(arg)
|
||||
}
|
||||
buffer.WriteByte('\n')
|
||||
return &buffer, nil
|
||||
@@ -847,7 +848,7 @@ func DoesHostHaveHostName(host string) (bool, string) {
|
||||
}
|
||||
|
||||
func IsHostLocal(host string) bool {
|
||||
host = strings.ToLower(host)
|
||||
host = strings.ToLower(strings.TrimSpace(host))
|
||||
return host == "127.0.0.1" || host == "localhost" || host == ""
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
Command list {{.listName }} failed.
|
||||
Backy Command: {{.CmdName}}.
|
||||
|
||||
The command run was {{.CmdName}}.
|
||||
The command executed: {{.Command}} {{ if .Args }} {{- range .Args}} {{.}} {{end}} {{end}}
|
||||
|
||||
The command executed was {{.Command}} {{ if .Args }} {{- range .Args}} {{.}} {{end}} {{end}}
|
||||
{{ if .Err }} Error: {{ .Err }}{{ end }}
|
||||
|
||||
{{ if .Err }} The error was {{ .Err }}{{ end }}
|
||||
|
||||
{{ if .Output }} The output was: {{- range .Output}} {{.}} {{end}} {{end}}
|
||||
{{ if .Output }} Output: {{- range .Output}} {{.}} {{end}} {{end}}
|
||||
|
||||
{{ if .CmdsRan }}
|
||||
The following commands ran:
|
||||
Commands executed (last one failed):
|
||||
{{- range .CmdsRan}}
|
||||
- {{. -}}
|
||||
{{end}}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
Command list {{ .listName }} completed successfully.
|
||||
|
||||
The following commands ran:
|
||||
Commands executed:
|
||||
{{- range .CmdsRan}}
|
||||
- {{. -}}
|
||||
{{end}}
|
||||
|
||||
@@ -226,6 +226,11 @@ func CheckConfigValues(config *koanf.Koanf, file string) {
|
||||
func collectOutput(buf *bytes.Buffer, commandName string, logger, globalLogger zerolog.Logger, wantOutput bool) []string {
|
||||
var outputArr []string
|
||||
copyBuf := bytes.NewBuffer(buf.Bytes())
|
||||
if buf.Len() == 0 {
|
||||
logger.Info().Str("cmd", commandName).Msg("NO OUTPUT!!!!!!!")
|
||||
globalLogger.Info().Str("cmd", commandName).Msg("NO OUTPUT!!!!!!!")
|
||||
return outputArr
|
||||
}
|
||||
scanner := bufio.NewScanner(copyBuf)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
Reference in New Issue
Block a user