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
4 changes: 3 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ func assetCommands(r handlers.Runtime, id string) []*cobra.Command {
// platformCommands define the set of commands that can be performed on a
// speific server instance.
func platformCommands(r handlers.Runtime, id string) []*cobra.Command {
apiHandler := handlers.NewApiHandler(r)
h := handlers.NewHandler(r)

return makeRootCommand([]RootCommand{
RootCommand{"api", id, h.ApiCommands, "platform"},
RootCommand{"api", id, apiHandler.Commands, "platform"},
RootCommand{"inspect", id, h.InspectCommands, "platform"},
RootCommand{"start", id, h.StartCommands, "platform"},
RootCommand{"stop", id, h.StopCommands, "platform"},
Expand Down
52 changes: 33 additions & 19 deletions internal/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,86 @@ package handlers
import (
"github.com/itential/ipctl/internal/flags"
"github.com/itential/ipctl/internal/runners"
"github.com/itential/ipctl/pkg/logger"
"github.com/spf13/cobra"
)

type ApiHandler struct {
Runner runners.ApiRunner
Runtime Runtime
Descriptor DescriptorMap
}

func NewApiHandler(r Runtime, desc Descriptors) ApiHandler {
func NewApiHandler(r Runtime) ApiHandler {
return ApiHandler{
Runner: runners.NewApiRunner(r.Client),
Descriptor: desc[apiDescriptor],
Runtime: r,
Descriptor: r.Descriptors[apiDescriptor],
}

}

func (h ApiHandler) newCommand(key string, runtime *Runtime, runner runners.RunnerFunc, options flags.Flagger, opts ...CommandRunnerOption) *cobra.Command {
func (h ApiHandler) newCommand(key string, runner runners.RunnerFunc, options flags.Flagger, opts ...CommandRunnerOption) *cobra.Command {
r := NewCommandRunner(
key,
h.Descriptor,
runner,
runtime,
&h.Runtime,
options,
opts...,
)
return NewCommand(r)
}

// Adds the `api get <path>` command
func (h ApiHandler) Get(runtime *Runtime) *cobra.Command {
cmd := h.newCommand("get", runtime, h.Runner.Get, nil)
func (h ApiHandler) Commands() []*cobra.Command {
logger.Trace()
return []*cobra.Command{
h.Get(),
h.Delete(),
h.Put(),
h.Post(),
h.Patch(),
}
}

// Adds the `api get <path> ...` command
func (h ApiHandler) Get() *cobra.Command {
cmd := h.newCommand("get", h.Runner.Get, nil)
cmd.Args = cobra.ExactArgs(1)
return cmd
}

// Adds the `api delete <path>` command
func (h ApiHandler) Delete(runtime *Runtime) *cobra.Command {
// Adds the `api delete <path> ...` command
func (h ApiHandler) Delete() *cobra.Command {
options := flags.ApiDeleteOptions{}
cmd := h.newCommand("delete", runtime, h.Runner.Delete, &options)
cmd := h.newCommand("delete", h.Runner.Delete, &options)
cmd.Args = cobra.ExactArgs(1)
options.Flags(cmd)
return cmd
}

// Adds the `api put <path>` command
func (h ApiHandler) Put(runtime *Runtime) *cobra.Command {
// Adds the `api put <path> ...` command
func (h ApiHandler) Put() *cobra.Command {
options := &flags.ApiPutOptions{}
cmd := h.newCommand("put", runtime, h.Runner.Put, options)
cmd := h.newCommand("put", h.Runner.Put, options)
cmd.Args = cobra.ExactArgs(1)
options.Flags(cmd)
return cmd
}

// Adds the `api post <path>` command
func (h ApiHandler) Post(runtime *Runtime) *cobra.Command {
// Adds the `api post <path> ...` command
func (h ApiHandler) Post() *cobra.Command {
options := &flags.ApiPostOptions{}
cmd := h.newCommand("post", runtime, h.Runner.Post, options)
cmd := h.newCommand("post", h.Runner.Post, options)
cmd.Args = cobra.ExactArgs(1)
options.Flags(cmd)
return cmd
}

// Adds the `api path <path>` command
func (h ApiHandler) Patch(runtime *Runtime) *cobra.Command {
// Adds the `api path <path> ...` command
func (h ApiHandler) Patch() *cobra.Command {
options := &flags.ApiPatchOptions{}
cmd := h.newCommand("patch", runtime, h.Runner.Post, options)
cmd := h.newCommand("patch", h.Runner.Post, options)
cmd.Args = cobra.ExactArgs(1)
options.Flags(cmd)
return cmd
Expand Down
13 changes: 0 additions & 13 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/google/uuid"
"github.com/itential/ipctl/pkg/client"
"github.com/itential/ipctl/pkg/config"
"github.com/itential/ipctl/pkg/logger"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -225,18 +224,6 @@ func (h Handler) InspectCommands() []*cobra.Command {
return commands
}

func (h Handler) ApiCommands() []*cobra.Command {
logger.Trace()
handler := NewApiHandler(*h.Runtime, h.Descriptors)
var commands = []*cobra.Command{
handler.Get(h.Runtime),
handler.Delete(h.Runtime),
handler.Put(h.Runtime),
handler.Post(h.Runtime),
}
return commands
}

func (h Handler) EditCommands() []*cobra.Command {
var commands []*cobra.Command
for _, ele := range Editors() {
Expand Down