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
120 changes: 70 additions & 50 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
package cmd

import (
"fmt"
"strings"

"github.com/google/uuid"
"github.com/itential/ipctl/internal/cmdutils"
"github.com/itential/ipctl/internal/handlers"
"github.com/itential/ipctl/pkg/logger"
"github.com/spf13/cobra"
)

type GroupCommand struct {
Use string
Id string
type RootCommand struct {
Name string
Group string
Run func() []*cobra.Command
Descriptor string
}

func addCommandGroup(cmd *cobra.Command, r handlers.Runtime, title string, f func(handlers.Runtime, string) []*cobra.Command) {
// addRootCommand adds a new top level command to the application. Root
// commands typically do not implement functionality, rather provide a command
// tree for more specific commands.
func addRootCommand(cmd *cobra.Command, r handlers.Runtime, title string, f func(handlers.Runtime, string) []*cobra.Command) {
id := uuid.New().String()
children := f(r, id)
if len(children) > 0 {
Expand All @@ -30,41 +36,46 @@ func addCommandGroup(cmd *cobra.Command, r handlers.Runtime, title string, f fun
}
}

func makeGroupCommand(groupCommands []GroupCommand) []*cobra.Command {
var commands []*cobra.Command
// makeRootCommand will create a new root command for the application. Root
// commands are top level commands that implement addiitonal subcommands and
// therefore do not direclty perform any actions. The function accepts a
// single argument `rootCommands` which is an array of RootCommand instances.
func makeRootCommand(rootCommands []RootCommand) []*cobra.Command {
descriptors := loadDescriptors("descriptors")

for _, ele := range groupCommands {
c := makeCommand(
ele.Use,
ele.Id,
ele.Run,
ele.Descriptor,
)
var commands []*cobra.Command

if c != nil {
commands = append(commands, c)
for _, ele := range rootCommands {
if desc, exists := descriptors[ele.Descriptor]; exists {
c := makeChildCommand(ele, desc)
if c != nil {
commands = append(commands, c)
}
} else {
logger.Fatal(fmt.Errorf("missing root command descriptor: %s", ele.Descriptor), "")
}
}

return commands
}

func makeCommand(name, group string, f func() []*cobra.Command, desc string) *cobra.Command {
descriptors := loadDescriptors("descriptors")[desc]

// makeChildCommand creates a single command attached to a root command. Child
// commands are typically handed off to a handler for further implementation of
// the command action.
func makeChildCommand(root RootCommand, desc map[string]cmdutils.Descriptor) *cobra.Command {
var cmd = &cobra.Command{
Use: name,
GroupID: group,
Use: root.Name,
GroupID: root.Group,

Short: strings.Split(descriptors[name].Description, "\n")[0],
Long: descriptors[name].Description,
Short: strings.Split(desc[root.Name].Description, "\n")[0],
Long: desc[root.Name].Description,

Example: descriptors[name].Example,
Example: desc[root.Name].Example,

Hidden: descriptors[name].Hidden,
Hidden: desc[root.Name].Hidden,
}

if descriptors[name].IncludeGroups {
if desc[root.Name].IncludeGroups {
cmd.AddGroup(
&cobra.Group{ID: "admin-essentials", Title: "Admin Essentials Commands:"},
&cobra.Group{ID: "automation-studio", Title: "Automation Studio Commands:"},
Expand All @@ -74,60 +85,69 @@ func makeCommand(name, group string, f func() []*cobra.Command, desc string) *co
)
}

children := f()
children := root.Run()

if len(children) == 0 {
return nil
}

cmd.AddCommand(f()...)
cmd.AddCommand(children...)

return cmd
}

// assetCommands define the aggregate set of commands for working with assets
func assetCommands(r handlers.Runtime, id string) []*cobra.Command {
h := handlers.NewHandler(r)
return makeGroupCommand([]GroupCommand{
GroupCommand{"get", id, h.GetCommands, "asset"},
GroupCommand{"describe", id, h.DescribeCommands, "asset"},
return makeRootCommand([]RootCommand{
RootCommand{"get", id, h.GetCommands, "asset"},
RootCommand{"describe", id, h.DescribeCommands, "asset"},

GroupCommand{"create", id, h.CreateCommands, "asset"},
GroupCommand{"delete", id, h.DeleteCommands, "asset"},
RootCommand{"create", id, h.CreateCommands, "asset"},
RootCommand{"delete", id, h.DeleteCommands, "asset"},

GroupCommand{"copy", id, h.CopyCommands, "asset"},
RootCommand{"copy", id, h.CopyCommands, "asset"},

GroupCommand{"clear", id, h.ClearCommands, "asset"},
RootCommand{"clear", id, h.ClearCommands, "asset"},

GroupCommand{"edit", id, h.EditCommands, "asset"},
RootCommand{"edit", id, h.EditCommands, "asset"},

GroupCommand{"import", id, h.ImportCommands, "asset"},
GroupCommand{"export", id, h.ExportCommands, "asset"},
RootCommand{"import", id, h.ImportCommands, "asset"},
RootCommand{"export", id, h.ExportCommands, "asset"},
})
}

// platformCommands define the set of commands that can be performed on a
// speific server instance.
func platformCommands(r handlers.Runtime, id string) []*cobra.Command {
h := handlers.NewHandler(r)
return makeGroupCommand([]GroupCommand{
GroupCommand{"api", id, h.ApiCommands, "platform"},
GroupCommand{"inspect", id, h.InspectCommands, "platform"},
GroupCommand{"start", id, h.StartCommands, "platform"},
GroupCommand{"stop", id, h.StopCommands, "platform"},
GroupCommand{"restart", id, h.RestartCommands, "platform"},
return makeRootCommand([]RootCommand{
RootCommand{"api", id, h.ApiCommands, "platform"},
RootCommand{"inspect", id, h.InspectCommands, "platform"},
RootCommand{"start", id, h.StartCommands, "platform"},
RootCommand{"stop", id, h.StopCommands, "platform"},
RootCommand{"restart", id, h.RestartCommands, "platform"},
})
}

// dataserCommands provide a set of commands for performing batch operations on
// specific asset types
func datasetCommands(r handlers.Runtime, id string) []*cobra.Command {
h := handlers.NewHandler(r)
return makeGroupCommand([]GroupCommand{
GroupCommand{"load", id, h.LoadCommands, "dataset"},
GroupCommand{"dump", id, h.DumpCommands, "dataset"},
return makeRootCommand([]RootCommand{
RootCommand{"load", id, h.LoadCommands, "dataset"},
RootCommand{"dump", id, h.DumpCommands, "dataset"},
})
}

// pluginCommands are commands that extend the functionality of the
// application.
func pluginCommands(r handlers.Runtime, id string) []*cobra.Command {
h := handlers.NewHandler(r)
return makeGroupCommand([]GroupCommand{
GroupCommand{"local-aaa", id, h.LocalAAACommands, "localaaa"},
GroupCommand{"client", id, h.LocalClientCommands, "localclient"},
localAAAHandler := handlers.NewLocalAAAHandler(r)
localClientHandler := handlers.NewLocalClientHandler(r)

return makeRootCommand([]RootCommand{
RootCommand{"local-aaa", id, localAAAHandler.Commands, "localaaa"},
RootCommand{"client", id, localClientHandler.Commands, "localclient"},
})
}
4 changes: 2 additions & 2 deletions cmd/descriptors/asset.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Itential Inc. All Rights Reserved
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
---
create:
description: |
Expand Down
4 changes: 2 additions & 2 deletions cmd/descriptors/dataset.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Itential Inc. All Rights Reserved
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
---
dump:
description: |
Expand Down
7 changes: 5 additions & 2 deletions cmd/descriptors/localaaa.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright 2024 Itential Inc. All Rights Reserved
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
---
local-aaa:
description: |
Manage local AAA

The local-aaa plugin provides commands for managing user and group
accounts defined in Local AAA.
4 changes: 2 additions & 2 deletions cmd/descriptors/localclient.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Itential Inc. All Rights Reserved
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
---
client:
description: |
Expand Down
4 changes: 2 additions & 2 deletions cmd/descriptors/platform.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Itential Inc. All Rights Reserved
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
---
api:
description: |
Expand Down
13 changes: 0 additions & 13 deletions cmd/descriptors/repo.yaml

This file was deleted.

13 changes: 9 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ const description = `Manage Itential Platform
Find more information at: https://docs.itential.com
`

// loadCommands will load the command tree for the application. All top level
// comamnds are defined by this function except for the `version` command which
// is defined below.
func loadCommands(cmd *cobra.Command, runtime handlers.Runtime) {
addCommandGroup(cmd, runtime, "Asset Commands:", assetCommands)
addRootCommand(cmd, runtime, "Asset Commands:", assetCommands)
if runtime.Config.FeaturesDatasetsEnabled {
addCommandGroup(cmd, runtime, "Dataset Commands:", datasetCommands)
addRootCommand(cmd, runtime, "Dataset Commands:", datasetCommands)
}
addCommandGroup(cmd, runtime, "Platform Commands:", platformCommands)
addCommandGroup(cmd, runtime, "Plugin Commands:", pluginCommands)
addRootCommand(cmd, runtime, "Platform Commands:", platformCommands)
addRootCommand(cmd, runtime, "Plugin Commands:", pluginCommands)
}

// versionCommand is a top level command that display's the current application
// version.
func versionCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "version",
Expand Down
43 changes: 8 additions & 35 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ type Handler struct {
}

type Runtime struct {
Client client.Client
Config *config.Config
Verbose bool
Client client.Client
Config *config.Config
Descriptors Descriptors
Verbose bool
}

var commands []any

func NewRuntime(c client.Client, cfg *config.Config) Runtime {
descriptors := loadDescriptors()
return Runtime{
Client: c,
Config: cfg,
Client: c,
Config: cfg,
Descriptors: descriptors,
}
}

Expand Down Expand Up @@ -74,9 +77,6 @@ func NewHandler(r Runtime) Handler {
NewModelHandler(r, descriptors),

NewServerHandler(r, descriptors),

NewLocalAAAHandler(r, descriptors),
NewLocalClientHandler(r, descriptors),
)

return Handler{
Expand Down Expand Up @@ -269,30 +269,3 @@ func (h Handler) LoadCommands() []*cobra.Command {
}
return commands
}

func (h Handler) LocalAAACommands() []*cobra.Command {
p, err := h.Runtime.Config.ActiveProfile()
if err != nil {
logger.Fatal(err, "")
}

if p.MongoUrl != "" {
handler := NewLocalAAAHandler(*h.Runtime, h.Descriptors)
logger.Info("adding LocalAAA commands")
return []*cobra.Command{
handler.Get(h.Runtime),
handler.Create(h.Runtime),
handler.Delete(h.Runtime),
}
}

return nil
}

func (h Handler) LocalClientCommands() []*cobra.Command {
handler := NewLocalClientHandler(*h.Runtime, h.Descriptors)
var commands = []*cobra.Command{
handler.Show(h.Runtime),
}
return commands
}
Loading