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

Allow to get latest logs for a stack #171

Merged
merged 1 commit into from
Aug 7, 2023
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
7 changes: 6 additions & 1 deletion internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ var flagRequiredRun = &cli.StringFlag{

var flagRun = &cli.StringFlag{
Name: "run",
Usage: "`ID` of the run",
Usage: "[Optional] `ID` of the run",
}

var flagRunLatest = &cli.BoolFlag{
tomasmik marked this conversation as resolved.
Show resolved Hide resolved
Name: "run-latest",
Usage: "[Optional] Indicates that the latest run should be used",
}

var flagNoInit = &cli.BoolFlag{
Expand Down
47 changes: 47 additions & 0 deletions internal/cmd/stack/run_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/urfave/cli/v2"

"github.com/spacelift-io/spacectl/client/structs"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
Expand All @@ -17,6 +19,51 @@ import (
// for example to confirm a run.
type actionOnRunState func(state structs.RunState, stackID, runID string) error

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

if (!cliCtx.IsSet(flagRun.Name) && !cliCtx.IsSet(flagRunLatest.Name)) ||
(cliCtx.IsSet(flagRun.Name) && cliCtx.IsSet(flagRunLatest.Name)) {
return errors.New("you must specify either --run or --run-latest")
}

runID := cliCtx.String(flagRun.Name)
if cliCtx.IsSet(flagRunLatest.Name) {
type runsQuery struct {
ID string `graphql:"id"`
}

var query struct {
Stack *struct {
Runs []runsQuery `graphql:"runs(before: $before)"`
} `graphql:"stack(id: $stackId)"`
}

var before *string
if err := authenticated.Client.Query(cliCtx.Context, &query, map[string]interface{}{"stackId": stackID, "before": before}); err != nil {
return errors.Wrap(err, "failed to query run list")
}

if query.Stack == nil {
return fmt.Errorf("failed to lookup logs with flag --run-latest, stack %q not found", stackID)
}

if len(query.Stack.Runs) == 0 {
return errors.New("failed to lookup logs with flag --run-latest, no runs found")
}

runID = query.Stack.Runs[0].ID

fmt.Println("Using latest run", runID)
}

_, err = runLogsWithAction(context.Background(), stackID, runID, nil)
return err
}

func runLogsWithAction(ctx context.Context, stack, run string, acFn actionOnRunState) (terminal *structs.RunStateTransition, err error) {
lines := make(chan string)

Expand Down
14 changes: 3 additions & 11 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package stack

import (
"context"

"github.com/urfave/cli/v2"

"github.com/spacelift-io/spacectl/internal/cmd"
Expand Down Expand Up @@ -133,16 +131,10 @@ func Command() *cli.Command {
Usage: "Show logs for a particular run",
Flags: []cli.Flag{
flagStackID,
flagRequiredRun,
},
Action: func(cliCtx *cli.Context) error {
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}
_, err = runLogsWithAction(context.Background(), stackID, cliCtx.String(flagRequiredRun.Name), nil)
return err
flagRun,
flagRunLatest,
},
Action: runLogs,
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
Expand Down