Skip to content

Commit

Permalink
feature(blueprints): blueprint show
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Sep 20, 2024
1 parent f1c18ad commit 91b84fa
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/cmd/blueprint/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ func Command() *cli.Command {
),
ArgsUsage: cmd.EmptyArgsUsage,
},
{
Name: "show",
Usage: "Shows detailed information about a specific blueprint",
Flags: []cli.Flag{
flagRequiredBlueprintID,
cmd.FlagOutputFormat,
cmd.FlagNoColor,
},
Action: (&showCommand{}).show,
Before: cmd.PerformAllBefore(cmd.HandleNoColor, authenticated.Ensure),
ArgsUsage: cmd.EmptyArgsUsage,
},
},
}
}
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/blueprint/flags.go
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
package blueprint

import "github.com/urfave/cli/v2"

var flagRequiredBlueprintID = &cli.StringFlag{
Name: "blueprint-id",
Aliases: []string{"b-id"},
Usage: "[Required] `ID` of the blueprint",
Required: true,
}
158 changes: 158 additions & 0 deletions internal/cmd/blueprint/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package blueprint

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/internal/cmd"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
"github.com/urfave/cli/v2"
)

type showQuery struct {
Blueprint *struct {
ID string `graphql:"id" json:"id,omitempty"`
Deleted bool `graphql:"deleted" json:"deleted,omitempty"`
Name string `graphql:"name" json:"name,omitempty"`
Description string `graphql:"description" json:"description,omitempty"`
CreatedAt int `graphql:"createdAt" json:"createdAt,omitempty"`
UpdatedAt int `graphql:"updatedAt" json:"updatedAt,omitempty"`
State string `graphql:"state" json:"state,omitempty"`
Inputs []struct {
ID string `graphql:"id" json:"id,omitempty"`
Name string `graphql:"name" json:"name,omitempty"`
Default string `graphql:"default" json:"default,omitempty"`
Description string `graphql:"description" json:"description,omitempty"`
Options []string `graphql:"options" json:"options,omitempty"`
Type string `graphql:"type" json:"type,omitempty"`
}
Space struct {
ID string `graphql:"id" json:"id,omitempty"`
Name string `graphql:"name" json:"name,omitempty"`
AccessLevel string `graphql:"accessLevel" json:"accessLevel,omitempty"`
}
Labels []string `graphql:"labels" json:"labels,omitempty"`
RawTemplate string `graphql:"rawTemplate" json:"rawTemplate,omitempty"`
} `graphql:"blueprint(id: $blueprintId)" json:"blueprint,omitempty"`
}

type showCommand struct{}

func (c *showCommand) show(cliCtx *cli.Context) error {
blueprintID := cliCtx.String(flagRequiredBlueprintID.Name)

outputFormat, err := cmd.GetOutputFormat(cliCtx)
if err != nil {
return err
}

var query showQuery
variables := map[string]interface{}{
"blueprintId": graphql.ID(blueprintID),
}

if err := authenticated.Client.Query(cliCtx.Context, &query, variables); err != nil {
return errors.Wrapf(err, "failed to query for blueprint ID %q", blueprintID)
}

if query.Blueprint == nil {
return fmt.Errorf("blueprint with ID %q not found", blueprintID)
}

switch outputFormat {
case cmd.OutputFormatTable:
return c.showBlueprintTable(query)
case cmd.OutputFormatJSON:
return cmd.OutputJSON(query.Blueprint)
}

return fmt.Errorf("unknown output format: %v", outputFormat)
}

func (c *showCommand) showBlueprintTable(query showQuery) error {
c.outputBlueprintNameSection(query)

if err := c.outputInputs(query); err != nil {
return err
}

if err := c.outputSpace(query); err != nil {
return err
}

c.outputRawTemplate(query)

return nil
}

func (c *showCommand) outputBlueprintNameSection(query showQuery) {
pterm.DefaultSection.WithLevel(1).Print(query.Blueprint.Name)

if len(query.Blueprint.Labels) > 0 {
pterm.DefaultSection.WithLevel(2).Println("Labels")
pterm.DefaultParagraph.Println(fmt.Sprintf("[%s]", strings.Join(query.Blueprint.Labels, "], [")))
}

if query.Blueprint.Description != "" {
pterm.DefaultSection.WithLevel(2).Println("Description")
pterm.DefaultParagraph.Println(query.Blueprint.Description)
}

if query.Blueprint.CreatedAt != 0 {
pterm.DefaultSection.WithLevel(2).Println("Created at")
pterm.DefaultParagraph.Println(cmd.HumanizeUnixSeconds(query.Blueprint.CreatedAt))
}

if query.Blueprint.UpdatedAt != 0 {
pterm.DefaultSection.WithLevel(2).Println("Updated at")
pterm.DefaultParagraph.Println(cmd.HumanizeUnixSeconds(query.Blueprint.UpdatedAt))
}

if query.Blueprint.State != "" {
pterm.DefaultSection.WithLevel(2).Println("State")
pterm.DefaultParagraph.Println(cmd.HumanizeBlueprintState(query.Blueprint.State))
}
}

func (c *showCommand) outputInputs(query showQuery) error {
if len(query.Blueprint.Inputs) == 0 {
return nil
}

pterm.DefaultSection.WithLevel(2).Println("Inputs")

tableData := [][]string{{"Name", "ID", "Description", "Default", "Options", "Type"}}
for _, input := range query.Blueprint.Inputs {

tableData = append(tableData, []string{
input.Name,
input.ID,
input.Description,
input.Default,
strings.Join(input.Options, ", "),
input.Type,
})
}

return cmd.OutputTable(tableData, true)
}

func (c *showCommand) outputSpace(query showQuery) error {
pterm.DefaultSection.WithLevel(2).Println("Space")
tableData := [][]string{
{"Name", query.Blueprint.Space.Name},
{"ID", query.Blueprint.Space.ID},
{"Access Level", query.Blueprint.Space.AccessLevel},
}

return cmd.OutputTable(tableData, false)
}

func (c *showCommand) outputRawTemplate(query showQuery) {
pterm.DefaultSection.WithLevel(2).Println("Template")

pterm.Println(query.Blueprint.RawTemplate)
}
2 changes: 1 addition & 1 deletion internal/cmd/profile/login_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func getCredentialsType(ctx *cli.Context) (session.CredentialsType, error) {
return 0, err
}

return session.CredentialsType(result + 1), nil
return session.CredentialsType(result + 1), nil //nolint: gosec
}

func readEndpoint(ctx *cli.Context, reader *bufio.Reader) (string, error) {
Expand Down

0 comments on commit 91b84fa

Please sign in to comment.