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
16 changes: 12 additions & 4 deletions internal/output/entities/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,7 +61,8 @@ func RegisterHostDefinition() {
{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)
Expand All @@ -84,7 +89,8 @@ func RegisterDedicatedServerDefinition() {
{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)
Expand All @@ -111,7 +117,8 @@ func RegisterKubernetesBaremetalNodeDefinition() {
{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)
Expand All @@ -138,7 +145,8 @@ func RegisterSBMServerDefinition() {
{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)
Expand Down
31 changes: 28 additions & 3 deletions internal/output/entities/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"reflect"
"slices"
)

// RegistryInterface represents the interface for the EntityRegistry
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion internal/output/entities/ssh_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

var (
SSHKeyType = reflect.TypeOf(serverscom.SSHKey{})
SSHKeyType = reflect.TypeOf(serverscom.SSHKey{})
SSHKeyListDefaultFields = []string{"Name", "Fingerprint"}
)

func RegisterSSHKeyDefinition() {
Expand All @@ -20,6 +21,9 @@ func RegisterSSHKeyDefinition() {
{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,
}
if err := Registry.Register(sshEntity); err != nil {
Expand Down
Loading