Skip to content

Commit

Permalink
feat(cloud): Add option to prompt select metro (#1891)
Browse files Browse the repository at this point in the history
Reviewed-by: Alexander Jung <alex@unikraft.io>
Approved-by: Alexander Jung <alex@unikraft.io>
  • Loading branch information
nderjung authored Sep 16, 2024
2 parents 859184a + cf75c64 commit 4847d17
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (opts *DeployOptions) Run(ctx context.Context, args []string) error {
if len(insts) == 1 && opts.Output == "" {
// No need to check for error, we check if-nil inside PrettyPrintInstance.
svc, _ := svcResp.FirstOrErr()
utils.PrettyPrintInstance(ctx, insts[0], svc, !opts.NoStart)
utils.PrettyPrintInstance(ctx, opts.Metro, insts[0], svc, !opts.NoStart)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions internal/cli/kraft/cloud/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

log.G(cmd.Context()).WithField("metro", opts.Metro).Debug("using")
return nil
}

Expand All @@ -815,7 +814,7 @@ func (opts *CreateOptions) Run(ctx context.Context, args []string) error {

// No need to check for error, we check if-nil inside PrettyPrintInstance.
svc, _ := svcResp.FirstOrErr()
utils.PrettyPrintInstance(ctx, insts[0], svc, opts.Start)
utils.PrettyPrintInstance(ctx, opts.Metro, insts[0], svc, opts.Start)

return nil
}
Expand Down
39 changes: 39 additions & 0 deletions internal/cli/kraft/cloud/utils/populate_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ import (
"os"

"github.com/spf13/cobra"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/tui/selection"

unikraftcloud "sdk.kraft.cloud"
)

type Metro struct {
Location string
Code string
}

var _ fmt.Stringer = (*Metro)(nil)

func (m Metro) String() string {
return fmt.Sprintf("%s (%s)", m.Code, m.Location)
}

func PopulateMetroToken(cmd *cobra.Command, metro, token *string) error {
*metro = cmd.Flag("metro").Value.String()
if *metro == "" {
Expand All @@ -25,6 +40,30 @@ func PopulateMetroToken(cmd *cobra.Command, metro, token *string) error {
*metro = os.Getenv("UKC_METRO")
}

if *metro == "" && !config.G[config.KraftKit](cmd.Context()).NoPrompt {
client := unikraftcloud.NewMetrosClient()

metros, err := client.List(cmd.Context(), false)
if err != nil {
return fmt.Errorf("could not list metros: %w", err)
}

candidates := make([]Metro, len(metros))
for i, m := range metros {
candidates[i].Code = m.Code
candidates[i].Location = m.Location
}

candidate, err := selection.Select("metro not explicitly set: which one would you like to use?", candidates...)
if err != nil {
return err
}

*metro = candidate.Code

log.G(cmd.Context()).Infof("run `export UKC_METRO=%s` or use the `--metro` flag to skip this prompt in the future", *metro)
}

if *metro == "" {
return fmt.Errorf("unikraft cloud metro is unset, try setting `UKC_METRO`, or use the `--metro` flag")
}
Expand Down
6 changes: 5 additions & 1 deletion internal/cli/kraft/cloud/utils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ func PrintCertificates(ctx context.Context, format string, resp kcclient.Service
}

// PrettyPrintInstance outputs a single instance and information about it.
func PrettyPrintInstance(ctx context.Context, instance kcinstances.GetResponseItem, service *kcservices.GetResponseItem, autoStart bool) {
func PrettyPrintInstance(ctx context.Context, metro string, instance kcinstances.GetResponseItem, service *kcservices.GetResponseItem, autoStart bool) {
out := iostreams.G(ctx).Out

var title string
Expand Down Expand Up @@ -1099,6 +1099,10 @@ func PrettyPrintInstance(ctx context.Context, instance kcinstances.GetResponseIt
Key: "uuid",
Value: instance.UUID,
},
{
Key: "metro",
Value: metro,
},
{
Key: "state",
Value: color(string(instance.State)),
Expand Down

0 comments on commit 4847d17

Please sign in to comment.