From eaa6e91092282dbd5cc004624445e24e226ab82f Mon Sep 17 00:00:00 2001 From: Oleg Isakov Date: Wed, 12 Mar 2025 14:35:33 +0200 Subject: [PATCH 1/2] make created/updated fields as non default for list cmd --- internal/output/entities/hosts.go | 16 ++++++++-------- internal/output/entities/ssh_keys.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/output/entities/hosts.go b/internal/output/entities/hosts.go index 448f44f..251d768 100644 --- a/internal/output/entities/hosts.go +++ b/internal/output/entities/hosts.go @@ -54,8 +54,8 @@ func RegisterHostDefinition() { {ID: "PrivateIPv4Address", Name: "PrivateIPv4Address", Path: "PrivateIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, }, eType: HostType, } @@ -80,8 +80,8 @@ func RegisterDedicatedServerDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, getConfigurationDetailsField(), }, eType: DedicatedServerType, @@ -107,8 +107,8 @@ func RegisterKubernetesBaremetalNodeDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, getConfigurationDetailsField(), }, eType: KubernetesBaremetalNodeType, @@ -134,8 +134,8 @@ func RegisterSBMServerDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, getConfigurationDetailsField(), }, eType: SBMServerType, diff --git a/internal/output/entities/ssh_keys.go b/internal/output/entities/ssh_keys.go index b91ccfa..a8d6190 100644 --- a/internal/output/entities/ssh_keys.go +++ b/internal/output/entities/ssh_keys.go @@ -17,8 +17,8 @@ func RegisterSSHKeyDefinition() { {ID: "Name", Name: "Name", Path: "Name", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, {ID: "Fingerprint", Name: "Fingerprint", Path: "Fingerprint", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, }, eType: SSHKeyType, } From 7f03adf7c891ffa142e208b590a0b0ee7561d132 Mon Sep 17 00:00:00 2001 From: Oleg Isakov Date: Wed, 12 Mar 2025 22:52:23 +0200 Subject: [PATCH 2/2] allow to set default fields for each cmd --- cmd/base/hooks.go | 4 ++++ internal/output/entities/hosts.go | 32 +++++++++++++++++---------- internal/output/entities/interface.go | 31 +++++++++++++++++++++++--- internal/output/entities/ssh_keys.go | 10 ++++++--- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/cmd/base/hooks.go b/cmd/base/hooks.go index a8fcea7..627968a 100644 --- a/cmd/base/hooks.go +++ b/cmd/base/hooks.go @@ -90,6 +90,10 @@ func CheckFormatterFlags(cmdContext *CmdContext, entities map[string]entities.En if entity == nil { return fmt.Errorf("can't find entity") } + if err := entity.SetCmdDefaultFields(cmd.Name()); err != nil { + return err + } + if fieldList { formatter.ListEntityFields(entity.GetFields()) os.Exit(0) diff --git a/internal/output/entities/hosts.go b/internal/output/entities/hosts.go index 251d768..6a98586 100644 --- a/internal/output/entities/hosts.go +++ b/internal/output/entities/hosts.go @@ -12,6 +12,10 @@ var ( DedicatedServerType = reflect.TypeOf(serverscom.DedicatedServer{}) KubernetesBaremetalNodeType = reflect.TypeOf(serverscom.KubernetesBaremetalNode{}) SBMServerType = reflect.TypeOf(serverscom.SBMServer{}) + HostListDefaultFields = []string{"ID", "Type", "Title", "Status"} + CmdDefaultFields = map[string][]string{ + "list": HostListDefaultFields, + } ) func getConfigurationDetailsField() Field { @@ -54,10 +58,11 @@ func RegisterHostDefinition() { {ID: "PrivateIPv4Address", Name: "PrivateIPv4Address", Path: "PrivateIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, }, - eType: HostType, + cmdDefaultFields: CmdDefaultFields, + eType: HostType, } if err := Registry.Register(hostEntity); err != nil { log.Fatal(err) @@ -80,11 +85,12 @@ func RegisterDedicatedServerDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, getConfigurationDetailsField(), }, - eType: DedicatedServerType, + cmdDefaultFields: CmdDefaultFields, + eType: DedicatedServerType, } if err := Registry.Register(serverEntity); err != nil { log.Fatal(err) @@ -107,11 +113,12 @@ func RegisterKubernetesBaremetalNodeDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, getConfigurationDetailsField(), }, - eType: KubernetesBaremetalNodeType, + cmdDefaultFields: CmdDefaultFields, + eType: KubernetesBaremetalNodeType, } if err := Registry.Register(serverEntity); err != nil { log.Fatal(err) @@ -134,11 +141,12 @@ func RegisterSBMServerDefinition() { {ID: "PublicIPv4Address", Name: "PublicIPv4Address", Path: "PublicIPv4Address", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, {ID: "ScheduledRelease", Name: "ScheduledRelease", Path: "ScheduledRelease", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, getConfigurationDetailsField(), }, - eType: SBMServerType, + cmdDefaultFields: CmdDefaultFields, + eType: SBMServerType, } if err := Registry.Register(serverEntity); err != nil { log.Fatal(err) diff --git a/internal/output/entities/interface.go b/internal/output/entities/interface.go index a494400..86595e6 100644 --- a/internal/output/entities/interface.go +++ b/internal/output/entities/interface.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "reflect" + "slices" ) // RegistryInterface represents the interface for the EntityRegistry @@ -20,13 +21,15 @@ type EntityInterface interface { GetType() reflect.Type GetDefaultFields() []string GetFields() []Field - Validate([]string) error + Validate(fields []string) error + SetCmdDefaultFields(cmd string) error } // Entity represents the base entity structure type Entity struct { - fields []Field - eType reflect.Type + fields []Field + cmdDefaultFields map[string][]string + eType reflect.Type } // HandlerFunc represents a handler function for rendering fields @@ -131,6 +134,28 @@ func (e *Entity) Validate(fields []string) error { return nil } +// SetCmdDefaultFields sets the default fields for an entity based on cmd +func (e *Entity) SetCmdDefaultFields(cmd string) error { + if defaultFields, ok := e.cmdDefaultFields[cmd]; ok { + fieldSet := make(map[string]struct{}, len(e.fields)) + for i, f := range e.fields { + e.fields[i].Default = false + fieldSet[f.ID] = struct{}{} + } + + for _, df := range defaultFields { + if _, exists := fieldSet[df]; !exists { + return fmt.Errorf("can't find field %s in entity field set", df) + } + } + + for i := range e.fields { + e.fields[i].Default = slices.Contains(defaultFields, e.fields[i].ID) + } + } + return nil +} + // fieldInChildFields checks if a field is in the list of child fields func (e *Entity) fieldInChildFields(childFields []Field, fieldID string) bool { for _, child := range childFields { diff --git a/internal/output/entities/ssh_keys.go b/internal/output/entities/ssh_keys.go index a8d6190..58a50d0 100644 --- a/internal/output/entities/ssh_keys.go +++ b/internal/output/entities/ssh_keys.go @@ -8,7 +8,8 @@ import ( ) var ( - SSHKeyType = reflect.TypeOf(serverscom.SSHKey{}) + SSHKeyType = reflect.TypeOf(serverscom.SSHKey{}) + SSHKeyListDefaultFields = []string{"Name", "Fingerprint"} ) func RegisterSSHKeyDefinition() { @@ -17,8 +18,11 @@ func RegisterSSHKeyDefinition() { {ID: "Name", Name: "Name", Path: "Name", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, {ID: "Fingerprint", Name: "Fingerprint", Path: "Fingerprint", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, {ID: "Labels", Name: "Labels", Path: "Labels", ListHandlerFunc: mapHandler, PageViewHandlerFunc: mapHandler}, - {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, - {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler}, + {ID: "Created", Name: "Created", Path: "Created", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + {ID: "Updated", Name: "Updated", Path: "Updated", ListHandlerFunc: timeHandler, PageViewHandlerFunc: timeHandler, Default: true}, + }, + cmdDefaultFields: map[string][]string{ + "list": SSHKeyListDefaultFields, }, eType: SSHKeyType, }