add JSON output to arthur

This commit is contained in:
Max Erenberg 2023-04-04 03:10:11 -04:00 committed by Mirror
parent af8beafd9e
commit 8ac217e5e5
3 changed files with 23 additions and 7 deletions

View File

@ -21,7 +21,7 @@ type RepoStatusInfo struct {
Name string `json:"name"`
IsRunning bool `json:"is_running"`
LastAttemptStartTime int64 `json:"last_attempt_time"`
LastAttemptExit int `json:"last_attempt_exit"`
LastAttemptExit string `json:"last_attempt_exit"`
LastSuccessfulAttemptStartTime int64 `json:"last_successful_attempt_time"`
NextSyncTime int64 `json:"next_sync_time"`
}
@ -72,7 +72,7 @@ func SendStatus(conn net.Conn) {
Name: name,
IsRunning: repo.State.IsRunning,
LastAttemptStartTime: repo.State.LastAttemptStartTime,
LastAttemptExit: repo.State.LastAttemptExit,
LastAttemptExit: config.StatusToString(repo.State.LastAttemptExit),
LastSuccessfulAttemptStartTime: repo.State.LastSuccessfulAttemptStartTime,
}
if repo.State.LastAttemptStartTime != 0 {

View File

@ -106,22 +106,25 @@ func TestSendStatus(t *testing.T) {
IsRunning: true,
LastAttemptStartTime: 1620000000,
NextSyncTime: 1620604803,
LastAttemptExit: "completed",
},
{
Name: "eeeee",
IsRunning: true,
LastAttemptStartTime: 1600000000,
NextSyncTime: 1602592000,
LastAttemptExit: "completed",
},
{
Name: "lnux",
LastAttemptStartTime: 1640000000,
NextSyncTime: 1640086400,
LastAttemptExit: "completed",
},
},
}
if !reflect.DeepEqual(&expected, &statusInfo) {
t.Errorf("Expected:\n%v\nGot:%v\n", &expected, &statusInfo)
t.Errorf("Expected:\n%+v\nGot:%+v\n", &expected, &statusInfo)
}
}

View File

@ -12,14 +12,13 @@ import (
"time"
serverArthurPkg "git.csclub.uwaterloo.ca/public/merlin/arthur"
"git.csclub.uwaterloo.ca/public/merlin/config"
)
// var DEFAULT_SOCKET_PATH = "/home/mirror/merlin/merlin.sock"
var DEFAULT_SOCKET_PATH = "/mirror/merlin/run/merlin-go.sock"
var HELP_MESSAGE = `USAGE:
arthur [-h|--help] [--sock <socket_path>] COMMAND
arthur [-h|--help] [-json] [-sock <socket_path>] COMMAND
COMMANDS:
sync:[repo]
@ -42,18 +41,22 @@ func main() {
sockPath := flag.String("sock", DEFAULT_SOCKET_PATH, "alternate socket file")
var shouldOutputJson bool
flag.BoolVar(&shouldOutputJson, "json", false, "JSON output")
flag.Parse()
if flag.NArg() < 1 {
os.Exit(1)
}
command := os.Args[1]
conn, err := net.Dial("unix", *sockPath)
if err != nil {
log.Fatal(err)
}
_, err = conn.Write([]byte(os.Args[1]))
_, err = conn.Write([]byte(command))
if err != nil {
log.Fatal(err)
}
@ -70,6 +73,16 @@ func main() {
log.Fatal(err)
}
if command == "status" && shouldOutputJson {
jsonOutput, jsonErr := json.MarshalIndent(&statusInfo, "", " ")
if jsonErr == nil {
fmt.Println(string(jsonOutput))
} else {
log.Fatal(jsonErr)
}
return
}
writer := tabwriter.NewWriter(os.Stdout, 5, 5, 5, ' ', 0)
// print out the state of each repo in the config (last and next sync time + if it is currently running)
fmt.Fprintf(writer, "Repository\tLast Synced\tNext Expected Sync\tLast Exit\tRunning\n")
@ -78,7 +91,7 @@ func main() {
repo.Name,
unixTimeToStr(repo.LastAttemptStartTime),
unixTimeToStr(repo.NextSyncTime),
config.StatusToString(repo.LastAttemptExit),
repo.LastAttemptExit,
repo.IsRunning,
)
}