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

Add saucectl imagerunner logs command #735

Merged
merged 7 commits into from
Apr 3, 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
2 changes: 2 additions & 0 deletions cmd/saucectl/saucectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/saucelabs/saucectl/internal/cmd/completion"
"github.com/saucelabs/saucectl/internal/cmd/configure"
"github.com/saucelabs/saucectl/internal/cmd/doctor"
"github.com/saucelabs/saucectl/internal/cmd/imagerunner"
"github.com/saucelabs/saucectl/internal/cmd/ini"
"github.com/saucelabs/saucectl/internal/cmd/jobs"
"github.com/saucelabs/saucectl/internal/cmd/new"
Expand Down Expand Up @@ -71,6 +72,7 @@ func main() {
storage.Command(cmd.PersistentPreRun),
artifacts.Command(cmd.PersistentPreRun),
jobs.Command(cmd.PersistentPreRun),
imagerunner.Command(cmd.PersistentPreRun),
)

if err := cmd.Execute(); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/imagerunner/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package imagerunner

import (
"errors"
"time"

"github.com/saucelabs/saucectl/internal/credentials"
"github.com/saucelabs/saucectl/internal/http"
"github.com/saucelabs/saucectl/internal/region"
"github.com/spf13/cobra"
)

var (
imagerunnerClient http.ImageRunner
)

func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {
var regio string

cmd := &cobra.Command{
Use: "imagerunner",
Short: "Commands for interacting with imagerunner runs",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if preRun != nil {
preRun(cmd, args)
}

if region.FromString(regio) == region.None {
return errors.New("invalid region")
}

creds := credentials.Get()
url := region.FromString(regio).APIBaseURL()

imagerunnerClient = http.NewImageRunner(url, creds, 15*time.Minute)

return nil
},
}

flags := cmd.PersistentFlags()
flags.StringVarP(&regio, "region", "r", "us-west-1", "The Sauce Labs region. Options: us-west-1, eu-central-1.")

cmd.AddCommand(
LogsCommand(),
)
return cmd
}
49 changes: 49 additions & 0 deletions internal/cmd/imagerunner/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package imagerunner

import (
"context"
"fmt"

cmds "github.com/saucelabs/saucectl/internal/cmd"
imgrunner "github.com/saucelabs/saucectl/internal/imagerunner"
"github.com/saucelabs/saucectl/internal/segment"
"github.com/saucelabs/saucectl/internal/usage"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func LogsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "logs <runID>",
Short: "Fetch the logs for an imagerunner run",
PreRun: func(cmd *cobra.Command, args []string) {
tracker := segment.DefaultTracker

go func() {
tracker.Collect(
cases.Title(language.English).String(cmds.FullName(cmd)),
usage.Properties{}.SetFlags(cmd.Flags()),
)
_ = tracker.Close()
}()
},
RunE: func(cmd *cobra.Command, args []string) error {
return exec(args[0])
},
}

return cmd
}

func exec(runID string) error {
log, err := imagerunnerClient.GetLogs(context.Background(), runID)
if err != nil {
if err == imgrunner.ErrResourceNotFound {
return fmt.Errorf("could not find log URL for run with ID (%s): %w", runID, err)
}
return err
}
fmt.Println(log)
return nil
}
2 changes: 1 addition & 1 deletion internal/cmd/storage/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {

cmd := &cobra.Command{
Use: "storage",
Short: "Interact with Sauce Storage.",
Short: "Interact with Sauce Storage",
TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if preRun != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/http/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func (c *ImageRunner) GetLogs(ctx context.Context, id string) (string, error) {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return "", imagerunner.ErrResourceNotFound
}
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("unexpected server response (%d): %s", resp.StatusCode, b)
Expand Down