Skip to content

Commit

Permalink
Try to determine --id instead of always asking for it (#125)
Browse files Browse the repository at this point in the history
* Try to determine `--id` instead of always asking for it
* Always prompt if user needs to select a stack
  • Loading branch information
tomasmik authored Mar 6, 2023
1 parent 997c913 commit d36ea75
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 67 deletions.
5 changes: 4 additions & 1 deletion internal/cmd/stack/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func disable(cliCtx *cli.Context) error {
}

func enableDisable[T any](cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

if nArgs := cliCtx.NArg(); nArgs != 0 {
return fmt.Errorf("expected zero arguments but got %d", nArgs)
Expand Down
21 changes: 16 additions & 5 deletions internal/cmd/stack/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func setVar(cliCtx *cli.Context) error {
envName := cliCtx.Args().Get(0)
envValue := cliCtx.Args().Get(1)

stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
ConfigElement struct {
Expand Down Expand Up @@ -121,7 +124,10 @@ func (e *listEnvCommand) listEnv(cliCtx *cli.Context) error {
return err
}

stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var query listEnvQuery
variables := map[string]interface{}{
Expand Down Expand Up @@ -240,10 +246,12 @@ func mountFile(cliCtx *cli.Context) error {
nArgs := cliCtx.NArg()

envName := cliCtx.Args().Get(0)
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var fileContent []byte
var err error

switch nArgs {
case 1:
Expand Down Expand Up @@ -291,7 +299,10 @@ func mountFile(cliCtx *cli.Context) error {
}

func deleteEnvironment(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

if nArgs := cliCtx.NArg(); nArgs != 1 {
return fmt.Errorf("expecting environment delete as only one agument, got %d instead", nArgs)
Expand Down
8 changes: 5 additions & 3 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package stack

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

// flagStackID is flag used for passing the ID for a stack.
//
// It should never be retreived direcly but rather through the getStackID func.
var flagStackID = &cli.StringFlag{
Name: "id",
Usage: "[Required] User-facing `ID` (slug) of the stack",
Required: true,
Name: "id",
Usage: "[Optional] User-facing `ID` (slug) of the stack, if not provided stack search is used lookup the stack ID by the current directory and repository name",
}

var flagCommitSHA = &cli.StringFlag{
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import (

func localPreview() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
ctx := context.Background()

if !cliCtx.Bool(flagNoFindRepositoryRoot.Name) {
Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/stack/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ var flagStackLockNote = &cli.StringFlag{
}

func lock(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
note := cliCtx.String(flagStackLockNote.Name)

if nArgs := cliCtx.NArg(); nArgs != 0 {
Expand All @@ -43,7 +46,10 @@ func lock(cliCtx *cli.Context) error {
}

func unlock(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

if nArgs := cliCtx.NArg(); nArgs != 0 {
return fmt.Errorf("expected zero arguments but got %d", nArgs)
Expand Down
47 changes: 6 additions & 41 deletions internal/cmd/stack/open_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"

"github.com/manifoldco/promptui"
"github.com/pkg/browser"
"github.com/pkg/errors"
"github.com/shurcooL/graphql"
Expand Down Expand Up @@ -46,57 +45,23 @@ func openCommandInBrowser(cliCtx *cli.Context) error {
return err
}

return findAndSelect(cliCtx.Context, &stackSearchParams{
return findAndOpenStackInBrowser(cliCtx.Context, &stackSearchParams{
count: count,
projectRoot: subdir,
repositoryName: name,
branch: branch,
})
}

func findAndSelect(ctx context.Context, p *stackSearchParams) error {
stacks, err := searchStacks(ctx, p)
if err != nil {
return err
}

items := []string{}
found := map[string]string{}
for _, stack := range stacks {
items = append(items, stack.Name)
found[stack.Name] = stack.ID
}

if len(found) == 0 {
return errors.New("Didn't find stacks")
}

selected := items[0]
if len(items) > 1 {
if len(items) == p.count {
fmt.Printf("Search results exceeded maximum capacity (%d) some stacks might be missing\n", p.count)
}

prompt := promptui.Select{
Label: fmt.Sprintf("Found %d stacks, select one", len(items)),
Items: items,
Size: 10,
StartInSearchMode: len(items) > 5,
Searcher: func(input string, index int) bool {
return strings.Contains(items[index], input)
},
}

_, result, err := prompt.Run()
if err != nil {
return err
}
selected = result
func findAndOpenStackInBrowser(ctx context.Context, p *stackSearchParams) error {
got, err := findAndSelectStack(ctx, p, false)
if errors.Is(err, errNoStackFound) {
return errors.New("No stacks using the provided search parameters, maybe it's in a different subdir?")
}

return browser.OpenURL(authenticated.Client.URL(
"/stack/%s",
found[selected],
got,
))
}

Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type showOutputsQuery struct {
type showOutputsStackCommand struct{}

func (c *showOutputsStackCommand) showOutputs(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
outputID := cliCtx.String(flagOutputID.Name)

outputFormat, err := cmd.GetOutputFormat(cliCtx)
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/run_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (

func runConfirm() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
RunConfirm struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/run_discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (

func runDiscard() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
RunDiscard struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/run_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func runList(cliCtx *cli.Context) error {
return err
}

stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
maxResults := cliCtx.Int(flagMaxResults.Name)

switch outputFormat {
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/run_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
)

func runRetry(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
runID := cliCtx.String(flagRequiredRun.Name)

var mutation struct {
Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/stack/run_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ var flagRunReviewNote = &cli.StringFlag{
}

func runApprove(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
runID := cliCtx.String(flagRequiredRun.Name)
note := cliCtx.String(flagRunReviewNote.Name)

Expand All @@ -36,7 +39,10 @@ func runApprove(cliCtx *cli.Context) error {
}

func runReject(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
runID := cliCtx.String(flagRequiredRun.Name)
note := cliCtx.String(flagRunReviewNote.Name)

Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/stack/run_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (

func runTrigger(spaceliftType, humanType string) cli.ActionFunc {
return func(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
RunTrigger struct {
Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/stack/set_current_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
)

func setCurrentCommit(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
SetCurrentCommmit struct {
Expand All @@ -39,7 +42,7 @@ func setCurrentCommit(cliCtx *cli.Context) error {
return errors.New("no tracked commit set on the Stack")
}

_, err := fmt.Printf("Current commit set to %q: (SHA %s)\n", commit.Message, commit.Hash)
_, err = fmt.Printf("Current commit set to %q: (SHA %s)\n", commit.Message, commit.Hash)

return err
}
5 changes: 4 additions & 1 deletion internal/cmd/stack/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ type showStackQuery struct {
type showStackCommand struct{}

func (c *showStackCommand) showStack(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

outputFormat, err := cmd.GetOutputFormat(cliCtx)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ func Command() *cli.Command {
flagRequiredRun,
},
Action: func(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
_, err := runLogs(context.Background(), stackID, cliCtx.String(flagRequiredRun.Name))
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
_, err = runLogs(context.Background(), stackID, cliCtx.String(flagRequiredRun.Name))
return err
},
Before: authenticated.Ensure,
Expand Down
Loading

0 comments on commit d36ea75

Please sign in to comment.