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
45 changes: 28 additions & 17 deletions internal/handlers/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func checkError(err error, runtime *Runtime) {
cmdutils.CheckError(err, runtime.Config.TerminalNoColor)
}

func unableToDisplayOutput(nocolor bool) {
terminal.Error(fmt.Errorf("unable to display response"), nocolor)
}

func NewCommand(c *CommandRunner) *cobra.Command {
desc, exists := c.Descriptors[c.Key]

Expand Down Expand Up @@ -127,28 +131,35 @@ func NewCommand(c *CommandRunner) *cobra.Command {
resp, err := c.Run(req)
checkError(err, c.Runtime)

if c.Runtime.Config.TerminalDefaultOutput == "json" {
checkError(terminal.DisplayJson(resp.Object), c.Runtime)

} else if c.Runtime.Config.TerminalDefaultOutput == "yaml" {
checkError(terminal.DisplayYaml(resp.Object), c.Runtime)

} else if len(resp.Lines) > 0 {
if c.Runtime.Config.TerminalPager {
terminal.DisplayTabWriterStringWithPager(resp.Lines, 3, 3, true)
switch c.Runtime.Config.TerminalDefaultOutput {
case "json":
if resp.Object != nil {
checkError(terminal.DisplayJson(resp.Object), c.Runtime)
} else {
terminal.DisplayTabWriterString(resp.Lines, 3, 3, true)
unableToDisplayOutput(c.Runtime.Config.TerminalNoColor)
}

} else {
if resp.Text != "" {
terminal.Display(resp.String())
case "yaml":
if resp.Object != nil {
checkError(terminal.DisplayYaml(resp.Object), c.Runtime)
} else {
unableToDisplayOutput(c.Runtime.Config.TerminalNoColor)
}
case "human":
if len(resp.Keys) > 0 {
output := strings.Split(resp.String(), "\n")
if c.Runtime.Config.TerminalPager {
terminal.DisplayTabWriterStringWithPager(output, 3, 3, true)
} else {
terminal.DisplayTabWriterString(output, 3, 3, true)
}
} else {
terminal.Display("unable to display response")
output := resp.String()
if output == "" {
unableToDisplayOutput(c.Runtime.Config.TerminalNoColor)
}
terminal.Display(fmt.Sprintf("%s\n", output))
}
terminal.Display("")
}

},
}

Expand Down
65 changes: 38 additions & 27 deletions internal/runners/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
package runners

import (
"errors"
"embed"
"fmt"
"strings"

"github.com/itential/ipctl/pkg/client"
"github.com/itential/ipctl/pkg/config"
"github.com/itential/ipctl/pkg/logger"
"github.com/itential/ipctl/pkg/services"
)

//go:embed templates/accounts/*.tmpl
var content embed.FS

type AccountRunner struct {
config *config.Config
service *services.AccountService
Expand All @@ -27,6 +29,12 @@ func NewAccountRunner(client client.Client, cfg *config.Config) *AccountRunner {
}
}

/*
i******************************************************************************
Reader interface
*******************************************************************************
*/

// Get implements the `get accounts` command
func (r *AccountRunner) Get(in Request) (*Response, error) {
logger.Trace()
Expand All @@ -36,45 +44,48 @@ func (r *AccountRunner) Get(in Request) (*Response, error) {
return nil, err
}

display := []string{"NAME\tSOURCE"}
for _, ele := range accounts {
lines := []string{ele.Username, ele.Provenance}
display = append(display, strings.Join(lines, "\t"))
}

return NewResponse(
"",
WithTable(display),
WithObject(accounts),
), nil
return &Response{
Keys: []string{"username", "provenance"},
Object: accounts,
}, nil
}

// Describe implements the `describe account <name>` command
func (r *AccountRunner) Describe(in Request) (*Response, error) {
logger.Trace()

name := in.Args[0]

accounts, err := r.service.GetAll()
if err != nil {
return nil, err
}

var account *services.Account
account := r.selectAccountByName(in.Args[0], accounts)

for _, ele := range accounts {
if ele.Username == name {
account = &ele
break
}
if account == nil {
return nil, fmt.Errorf("account `%s` does not exist", in.Args[0])
}

if account == nil {
return nil, errors.New(fmt.Sprintf("account `%s` does not exist", name))
tmpl, err := content.ReadFile("describe.tmpl")
if err != nil {
logger.Fatal(err, "failed to load template")
}

return NewResponse(
"",
WithObject(account),
), nil
return &Response{
Object: account,
Template: string(tmpl),
//Template: "Username: {{.Username}}",
}, nil
}

// selectAccountByUsername takes a list of service.Accounts and iterates over
// them looking for the first instance of username. If found, the Account is
// returned. If the username is not found, nil is returend.
func (r *AccountRunner) selectAccountByName(username string, accounts []services.Account) *services.Account {
logger.Trace()
for _, ele := range accounts {
if ele.Username == username {
return &ele
}
}
return nil
}
2 changes: 0 additions & 2 deletions internal/runners/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestGet(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Empty(t, res.Text)
assert.NotEmpty(t, res.Lines)
}

func TestDescribe(t *testing.T) {
Expand All @@ -45,5 +44,4 @@ func TestDescribe(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, res)
assert.Empty(t, res.Text)
assert.NotEmpty(t, res.Lines)
}
4 changes: 1 addition & 3 deletions internal/runners/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ func (r *ActionRunner) Render(in Request) (*Response, error) {
Options: map[string]interface{}{},
})

return NewResponse(
"rendered",
), nil
return notImplemented(in)
}
15 changes: 4 additions & 11 deletions internal/runners/adapter_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,13 @@ func (r *AdapterModelRunner) Get(in Request) (*Response, error) {
return nil, err
}

display := []string{"MODEL"}
for _, ele := range res {
display = append(display, ele)
}

return NewResponse(
"",
WithTable(display),
WithObject(res),
), nil
return &Response{
Object: res,
}, nil

}

func (r *AdapterModelRunner) Describe(in Request) (*Response, error) {
logger.Trace()
return NotImplemented(in)
return notImplemented(in)
}
Loading