From 2adb17c3c4d7687ce633e440044646f1fe334919 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 13 Sep 2023 23:50:58 +0400 Subject: [PATCH] Introduce "orchard get worker" command (#127) --- internal/command/get/get.go | 1 + internal/command/get/worker.go | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 internal/command/get/worker.go diff --git a/internal/command/get/get.go b/internal/command/get/get.go index 6266140..238f1cb 100644 --- a/internal/command/get/get.go +++ b/internal/command/get/get.go @@ -18,6 +18,7 @@ func NewCommand() *cobra.Command { newGetClusterSettingsCommand(), newGetServiceAccountCommand(), newGetVMCommand(), + newGetWorkerCommand(), ) return command diff --git a/internal/command/get/worker.go b/internal/command/get/worker.go new file mode 100644 index 0000000..927fa7c --- /dev/null +++ b/internal/command/get/worker.go @@ -0,0 +1,85 @@ +package get + +import ( + "fmt" + "github.com/cirruslabs/orchard/internal/structpath" + "github.com/cirruslabs/orchard/pkg/client" + "github.com/dustin/go-humanize" + "github.com/gosuri/uitable" + "github.com/samber/lo" + "github.com/spf13/cobra" + "strings" + "time" +) + +func newGetWorkerCommand() *cobra.Command { + command := &cobra.Command{ + Use: "worker NAME", + Short: "Retrieve a worker and it's fields", + RunE: runGetWorker, + Args: cobra.ExactArgs(1), + } + + return command +} + +func runGetWorker(cmd *cobra.Command, args []string) error { + name := args[0] + + client, err := client.New() + if err != nil { + return err + } + + // Ability to retrieve resource fields (e.g. "orchard get worker dev-mini/name") + splits := strings.Split(name, "/") + var path []string + if len(splits) > 1 { + name = splits[0] + path = splits[1:] + } + + worker, err := client.Workers().Get(cmd.Context(), name) + if err != nil { + return err + } + + // Ability to retrieve resource fields (e.g. "orchard get worker dev-mini/name") + if len(path) != 0 { + result, ok := structpath.Lookup(*worker, path) + if !ok { + return fmt.Errorf("%w: failed to find the specified field \"%s\" or the field is not a string", + ErrGetFailed, strings.Join(path, "/")) + } + + fmt.Println(result) + + return nil + } + + table := uitable.New() + + table.AddRow("Name", worker.Name) + + createdAtInfo := humanize.RelTime(worker.CreatedAt, time.Now(), "ago", "in the future") + table.AddRow("Created", createdAtInfo) + + lastSeenInfo := humanize.RelTime(worker.LastSeen, time.Now(), "ago", "in the future") + table.AddRow("Last seen", lastSeenInfo) + + table.AddRow("Machine ID", worker.MachineID) + table.AddRow("Scheduling paused", worker.SchedulingPaused) + + var resourcesInfo string + if len(worker.Resources) != 0 { + resourceDescriptions := lo.MapToSlice(worker.Resources, func(key string, value uint64) string { + return fmt.Sprintf("%s: %d", key, value) + }) + resourcesInfo = strings.Join(resourceDescriptions, "\n") + } + table.AddRow("Resources", nonEmptyOrNone(resourcesInfo)) + + fmt.Println(table) + + return nil +}