Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions cmd/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ var runsCmd = &cobra.Command{
},
}

var showLogs bool

var runsAllCmd = &cobra.Command{
Use: "all",
Short: "List all runs",
Run: func(cmd *cobra.Command, args []string) {
list := runs.RetrieveRuns()
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s)", len(list)))
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s)", len(list)), showLogs)
},
}

Expand All @@ -35,7 +37,7 @@ var runsSuccessfulCmd = &cobra.Command{
Short: "List successful runs",
Run: func(cmd *cobra.Command, args []string) {
list := runs.QuerySuccessful(runs.RetrieveRuns()).Result
runs.PrintRuns(list, fmt.Sprintf("Found %d succesful run(s)", len(list)))
runs.PrintRuns(list, fmt.Sprintf("Found %d succesful run(s)", len(list)), showLogs)
},
}

Expand All @@ -44,25 +46,41 @@ var runsFailedCmd = &cobra.Command{
Short: "List failed runs",
Run: func(cmd *cobra.Command, args []string) {
list := runs.QueryFailed(runs.RetrieveRuns()).Result
runs.PrintRuns(list, fmt.Sprintf("Found %d failed run(s)", len(list)))
runs.PrintRuns(list, fmt.Sprintf("Found %d failed run(s)", len(list)), showLogs)
},
}

var timeLayouts = []string{
time.DateTime,
time.RFC3339,
time.DateOnly,
time.TimeOnly,
time.Kitchen,
"15:04",
"2006",
}

func parseTimestamp(input string) (time.Time, error) {
for _, layout := range timeLayouts {
if t, err := time.Parse(layout, input); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("Invalid timestamp: %s", input)
}

var runsBeforeCmd = &cobra.Command{
Use: "before <timestamp>",
Args: cobra.ExactArgs(1),

Short: "List runs before a timestamp.",
Long: `List runs before a timestamp.

Format for timestamp is YEAR-MONTH-DAY HOUR:MINUTE:SECOND`,
Run: func(cmd *cobra.Command, args []string) {
ts, err := time.Parse(time.DateTime, args[0])
ts, err := parseTimestamp(args[0])
if err != nil {
utils.PrintFatal("Error when parsing timestamp: %v", err)
}
list := runs.QueryBefore(runs.RetrieveRuns(), ts).Result
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s) before %s", len(list), args[0]))
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s) before %s", len(list), args[0]), showLogs)
},
}

Expand All @@ -71,24 +89,30 @@ var runsAfterCmd = &cobra.Command{
Args: cobra.ExactArgs(1),

Short: "List runs after a timestamp.",
Long: `List runs after a timestamp.

Format for timestamp is YEAR-MONTH-DAY HOUR:MINUTE:SECOND`,
Run: func(cmd *cobra.Command, args []string) {
ts, err := time.Parse(time.DateTime, args[0])
ts, err := parseTimestamp(args[0])
if err != nil {
utils.PrintFatal("Error when parsing timestamp: %v", err)
}
list := runs.QueryAfter(runs.RetrieveRuns(), ts).Result
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s) after %s", len(list), args[0]))
runs.PrintRuns(list, fmt.Sprintf("Found %d run(s) after %s", len(list), args[0]), showLogs)
},
}

func init() {
runsAllCmd.Flags().BoolVarP(&showLogs, "show-logs", "L", false, "Show run's logs")
runsCmd.AddCommand(runsAllCmd)

runsSuccessfulCmd.Flags().BoolVarP(&showLogs, "show-logs", "L", false, "Show run's logs")
runsCmd.AddCommand(runsSuccessfulCmd)

runsFailedCmd.Flags().BoolVarP(&showLogs, "show-logs", "L", false, "Show run's logs")
runsCmd.AddCommand(runsFailedCmd)

runsBeforeCmd.Flags().BoolVarP(&showLogs, "show-logs", "L", false, "Show run's logs")
runsCmd.AddCommand(runsBeforeCmd)

runsAfterCmd.Flags().BoolVarP(&showLogs, "show-logs", "L", false, "Show run's logs")
runsCmd.AddCommand(runsAfterCmd)

rootCmd.AddCommand(runsCmd)
Expand Down
12 changes: 4 additions & 8 deletions cmd/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -170,14 +169,14 @@ var taskRunCmd = &cobra.Command{
err = cmdStr.Run()
os.Stdout.Write([]byte(stdout.String()))
os.Stderr.Write([]byte(stderr.String()))
cmdSuccess := true

if err != nil {
log.Printf("Error when running task: %v\n", err)
utils.PrintFatal("Command was '%s'", tasks.Tasks[name].Cmd)
cmdSuccess = false
utils.PrintError("Command was '%s'", tasks.Tasks[name].Cmd)
}

if targetTask.StoreRuns {
utils.GlobalLogger.Log(fmt.Sprintf("Storing %s's run", targetTask.Name), utils.LogInfo)
if !utils.PathExists(".foundry/runs") {
os.Mkdir(".foundry/runs", 0755)
}
Expand All @@ -187,17 +186,14 @@ var taskRunCmd = &cobra.Command{
runInfo := runs.RunInfo{
RanBy: "task/" + targetTask.Name,
Logs: taskLogs,
Success: true,
Success: cmdSuccess,
}

runToml, err := toml.Marshal(runInfo)
if err != nil {
utils.PrintFatal("Error when marshaling run: %v", err)
}

utils.GlobalLogger.Log(fmt.Sprintf("TOML for run: %s", string(runToml)), utils.LogInfo)
utils.GlobalLogger.Log(fmt.Sprintf("Name for run: %s.run", time.Now().Format(time.RFC3339)), utils.LogInfo)

err = os.WriteFile(fmt.Sprintf(".foundry/runs/%s.toml", time.Now().Format(time.RFC3339)), runToml, 0644)
if err != nil {
utils.PrintFatal("Error when creating run file: %v", err)
Expand Down
14 changes: 13 additions & 1 deletion core/runs/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RetrieveRuns() []*Run {
return runs
}

func PrintRuns(runs []*Run, firstLine string) {
func PrintRuns(runs []*Run, firstLine string, showLogs bool) {
fmt.Println(firstLine)

for i, run := range runs {
Expand Down Expand Up @@ -103,5 +103,17 @@ func PrintRuns(runs []*Run, firstLine string) {
run.Info.RanBy,
logN,
)
if showLogs {
for j, log := range run.Info.Logs {
if log != "" {
prefix := " ├──"
if j == len(runs)-1 {
prefix = " └──"
}

fmt.Printf("%s \033[32m%s\033[0m\n", prefix, log)
}
}
}
}
}