Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spacectl/stack): add stack lock and unlock commands #76

Merged
merged 1 commit into from
Aug 26, 2022
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
58 changes: 58 additions & 0 deletions internal/cmd/stack/lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package stack

import (
"fmt"

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

type stackLockMutation struct {
Stack struct {
ID string `graphql:"id"`
} `graphql:"stackLock(id: $stack, note: $note)"`
}
type stackUnlockMutation struct {
Stack struct {
ID string `graphql:"id"`
} `graphql:"stackUnlock(id: $stack)"`
}

var flagStackLockNote = &cli.StringFlag{
Name: "name",
Usage: "Description of why the lock was acquired.",
Required: false,
}

func lock(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)
note := cliCtx.String(flagStackLockNote.Name)

if nArgs := cliCtx.NArg(); nArgs != 0 {
return fmt.Errorf("expected zero arguments but got %d", nArgs)
}

var mutation stackLockMutation
variables := map[string]interface{}{
"stack": graphql.ID(stackID),
"note": graphql.String(note),
}

return authenticated.Client.Mutate(cliCtx.Context, &mutation, variables)
}

func unlock(cliCtx *cli.Context) error {
stackID := cliCtx.String(flagStackID.Name)

if nArgs := cliCtx.NArg(); nArgs != 0 {
return fmt.Errorf("expected zero arguments but got %d", nArgs)
}

var mutation stackUnlockMutation
variables := map[string]interface{}{
"stack": graphql.ID(stackID),
}

return authenticated.Client.Mutate(cliCtx.Context, &mutation, variables)
}
21 changes: 21 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ func Command() *cli.Command {
Before: authenticated.Ensure,
ArgsUsage: "COMMAND",
},
{
Name: "lock",
Usage: "Locks a stack for exclusive use.",
Flags: []cli.Flag{
flagStackID,
flagStackLockNote,
},
Action: lock,
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
{
Name: "unlock",
Usage: "Unlocks a stack.",
Flags: []cli.Flag{
flagStackID,
},
Action: unlock,
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
},
}
}