From bf9d48c65ac2ceb34857763e0ce9fbbe6ee6379f Mon Sep 17 00:00:00 2001 From: Enno Gotthold Date: Tue, 29 Oct 2024 11:33:41 +0100 Subject: [PATCH] Event: Add command to read and list events --- cmd/event.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 cmd/event.go diff --git a/cmd/event.go b/cmd/event.go new file mode 100644 index 0000000..8a4882a --- /dev/null +++ b/cmd/event.go @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2024 Enno Gotthold +// SPDX-FileCopyrightText: Copyright SUSE LLC + +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "time" +) + +var eventCmd = &cobra.Command{ + Use: "event", + Short: "Show and query events for their status.", + Run: func(cmd *cobra.Command, args []string) { + _ = cmd.Help() + }, +} + +var eventStatusCmd = &cobra.Command{ + Use: "status", + RunE: func(cmd *cobra.Command, args []string) error { + generateCobblerClient() + + eventId, err := cmd.Flags().GetString("event-id") + if err != nil { + return err + } + + event, err := Client.GetTaskStatus(eventId) + if err != nil { + return err + } + fmt.Println(event.State) + return nil + }, +} + +var eventListCmd = &cobra.Command{ + Use: "list", + RunE: func(cmd *cobra.Command, args []string) error { + generateCobblerClient() + + user, err := cmd.Flags().GetString("user") + if err != nil { + return err + } + + events, err := Client.GetEvents(user) + if err != nil { + return err + } + idWidth := 0 + stateWidth := 10 + stateTimeWidth := 24 // Fixed width + nameWidth := 0 + for _, event := range events { + if len(event.ID) > idWidth { + idWidth = len(event.ID) + } + if len(event.Name) > nameWidth { + nameWidth = len(event.Name) + } + if len(event.State) > stateWidth { + stateWidth = len(event.State) + } + } + fmt.Printf("%*s | %*s | %*s | %*s | %s \n", idWidth, "ID", nameWidth, "Name", stateWidth, "Task State", stateTimeWidth, "Time (last transitioned)", "Read by Who") + for _, event := range events { + stateTimeStruct, err := covertFloatToUtcTime(event.StateTime) + if err != nil { + return err + } + fmt.Printf("%*s | %*s | %*s | %*s | %s \n", idWidth, event.ID, nameWidth, event.Name, stateWidth, event.State, stateTimeWidth, stateTimeStruct.Format(time.DateTime), event.ReadByWho) + } + return nil + }, +} + +var eventLogCmd = &cobra.Command{ + Use: "log", + RunE: func(cmd *cobra.Command, args []string) error { + generateCobblerClient() + + eventId, err := cmd.Flags().GetString("event-id") + if err != nil { + return err + } + + eventLog, err := Client.GetEventLog(eventId) + if err != nil { + return err + } + fmt.Println(eventLog) + return nil + }, +} + +func init() { + rootCmd.AddCommand(eventCmd) + + eventCmd.AddCommand(eventStatusCmd) + eventCmd.AddCommand(eventListCmd) + eventCmd.AddCommand(eventLogCmd) + + // local flags for status + eventStatusCmd.Flags().String("event-id", "", "the event ID of the background task") + _ = eventStatusCmd.MarkFlagRequired("event-id") + + // local flags for list + eventListCmd.Flags().String("user", "", "giving this parameter will show only events the user hasn't seen yet") + + // local flags for log + eventLogCmd.Flags().String("event-id", "", "the event ID of the background task") + _ = eventStatusCmd.MarkFlagRequired("event-id") +} diff --git a/go.mod b/go.mod index 30f47a8..7884c39 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cobbler/cli go 1.22 require ( - github.com/cobbler/cobblerclient v0.5.3 + github.com/cobbler/cobblerclient v0.5.4 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index d612b75..fcea715 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/cobbler/cobblerclient v0.5.3 h1:43STm8izD++BqHJIpxpYrHvNXXQvCtmcmlyO2gskNmg= -github.com/cobbler/cobblerclient v0.5.3/go.mod h1:n6b8fTUOlg7BdMl6FeifUm4Uk1JY6/tlTlOClV4x2Wc= +github.com/cobbler/cobblerclient v0.5.4 h1:ix+z9gwfUxv28ANz7giPGt4ICufR83rUm9E2q9dCwuw= +github.com/cobbler/cobblerclient v0.5.4/go.mod h1:n6b8fTUOlg7BdMl6FeifUm4Uk1JY6/tlTlOClV4x2Wc= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=