Skip to content

Commit

Permalink
feat: add --debug-mode (#1056)
Browse files Browse the repository at this point in the history
* feat: expose --debug flag for API requests/responses

* build(deps): bump go-fastly from 8.6.2 to 8.6.3

* fix: rename flag to avoid Kingpin conflict
  • Loading branch information
Integralist committed Oct 31, 2023
1 parent 9369e61 commit 8d2884e
Show file tree
Hide file tree
Showing 32 changed files with 120 additions and 87 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
)

require (
github.com/fastly/go-fastly/v8 v8.6.2
github.com/fastly/go-fastly/v8 v8.6.3
github.com/kennygrant/sanitize v1.2.4
github.com/mholt/archiver v3.1.1+incompatible
github.com/otiai10/copy v1.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj6
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0 h1:90Ly+6UfUypEF6vvvW5rQIv9opIL8CbmW9FT20LDQoY=
github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0/go.mod h1:V+Qd57rJe8gd4eiGzZyg4h54VLHmYVVw54iMnlAMrF8=
github.com/fastly/go-fastly/v8 v8.6.2 h1:adylZjOIfZFB2itXSmZPY4Yf1zGXv9tDy7W37wCcTWA=
github.com/fastly/go-fastly/v8 v8.6.2/go.mod h1:sC3WMOjQxwXk+gRI68ooTJJsI+/6AMA/4JLvrhNgpVk=
github.com/fastly/go-fastly/v8 v8.6.3 h1:tzi0fbV63TjCcvx0kzr30NX4ZyuURj5LE7BB5+NDOXU=
github.com/fastly/go-fastly/v8 v8.6.3/go.mod h1:sC3WMOjQxwXk+gRI68ooTJJsI+/6AMA/4JLvrhNgpVk=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
Expand Down
11 changes: 8 additions & 3 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (

// FastlyAPIClient is a ClientFactory that returns a real Fastly API client
// using the provided token and endpoint.
func FastlyAPIClient(token, endpoint string) (api.Interface, error) {
func FastlyAPIClient(token, endpoint string, debugMode bool) (api.Interface, error) {
client, err := fastly.NewClientForEndpoint(token, endpoint)
if debugMode {
client.DebugMode = true
}
return client, err
}

Expand Down Expand Up @@ -82,6 +85,8 @@ func Run(opts RunOpts) error {
tokenHelp := fmt.Sprintf("Fastly API token (or via %s)", env.Token)
app.Flag("accept-defaults", "Accept default options for all interactive prompts apart from Yes/No confirmations").Short('d').BoolVar(&g.Flags.AcceptDefaults)
app.Flag("auto-yes", "Answer yes automatically to all Yes/No confirmations. This may suppress security warnings").Short('y').BoolVar(&g.Flags.AutoYes)
// IMPORTANT: `--debug` is a built-in Kingpin flag so we can't use that.
app.Flag("debug-mode", "Print API request and response details (NOTE: can disrupt the normal CLI flow output formatting)").BoolVar(&g.Flags.Debug)
app.Flag("endpoint", "Fastly API endpoint").Hidden().StringVar(&g.Flags.Endpoint)
app.Flag("non-interactive", "Do not prompt for user input - suitable for CI processes. Equivalent to --accept-defaults and --auto-yes").Short('i').BoolVar(&g.Flags.NonInteractive)
app.Flag("profile", "Switch account profile for single command execution (see also: 'fastly profile switch')").Short('o').StringVar(&g.Flags.Profile)
Expand Down Expand Up @@ -157,7 +162,7 @@ func Run(opts RunOpts) error {

// NOTE: We return error immediately so there's no issue assigning to global.
// nosemgrep
g.APIClient, err = opts.APIClient(token, endpoint)
g.APIClient, err = opts.APIClient(token, endpoint, g.Flags.Debug)
if err != nil {
g.ErrLog.Add(err)
return fmt.Errorf("error constructing Fastly API client: %w", err)
Expand Down Expand Up @@ -203,7 +208,7 @@ type RunOpts struct {
// the Run helper with it: in the real CLI, we can use NewClient from the Fastly
// API client library via RealClient; in tests, we can provide a mock API
// interface via MockClient.
type APIClientFactory func(token, endpoint string) (api.Interface, error)
type APIClientFactory func(token, endpoint string, debugMode bool) (api.Interface, error)

// Versioners represents all supported versioner types.
type Versioners struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/azureblob/azureblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/azureblob"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateBlobStorageInput(t *testing.T) {
Expand Down Expand Up @@ -202,7 +203,7 @@ func createCommandRequired() *azureblob.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &azureblob.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -242,7 +243,7 @@ func createCommandAll() *azureblob.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &azureblob.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/bigquery"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateBigQueryInput(t *testing.T) {
Expand Down Expand Up @@ -195,7 +196,7 @@ func createCommandRequired() *bigquery.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &bigquery.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -237,7 +238,7 @@ func createCommandAll() *bigquery.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &bigquery.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/cloudfiles/cloudfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/cloudfiles"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateCloudfilesInput(t *testing.T) {
Expand Down Expand Up @@ -202,7 +203,7 @@ func createCommandRequired() *cloudfiles.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &cloudfiles.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -242,7 +243,7 @@ func createCommandAll() *cloudfiles.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &cloudfiles.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/datadog"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateDatadogInput(t *testing.T) {
Expand Down Expand Up @@ -183,7 +184,7 @@ func createCommandOK() *datadog.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &datadog.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -226,7 +227,7 @@ func createCommandRequired() *datadog.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &datadog.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/digitalocean/digitalocean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/digitalocean"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateDigitalOceanInput(t *testing.T) {
Expand Down Expand Up @@ -202,7 +203,7 @@ func createCommandRequired() *digitalocean.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &digitalocean.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -242,7 +243,7 @@ func createCommandAll() *digitalocean.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &digitalocean.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/elasticsearch"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateElasticsearchInput(t *testing.T) {
Expand Down Expand Up @@ -202,7 +203,7 @@ func createCommandRequired() *elasticsearch.CreateCommand {
globals.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &elasticsearch.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -241,7 +242,7 @@ func createCommandAll() *elasticsearch.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &elasticsearch.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/ftp/ftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/ftp"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateFTPInput(t *testing.T) {
Expand Down Expand Up @@ -199,7 +200,7 @@ func createCommandRequired() *ftp.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &ftp.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -239,7 +240,7 @@ func createCommandAll() *ftp.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &ftp.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/gcs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/gcs"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateGCSInput(t *testing.T) {
Expand Down Expand Up @@ -198,7 +199,7 @@ func createCommandRequired() *gcs.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &gcs.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -238,7 +239,7 @@ func createCommandAll() *gcs.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &gcs.CreateCommand{
Base: cmd.Base{
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/logging/googlepubsub/googlepubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"testing"

"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/commands/logging/googlepubsub"
"github.com/fastly/cli/pkg/config"
Expand All @@ -12,7 +14,6 @@ import (
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/go-fastly/v8/fastly"
)

func TestCreateGooglePubSubInput(t *testing.T) {
Expand Down Expand Up @@ -190,7 +191,7 @@ func createCommandRequired() *googlepubsub.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &googlepubsub.CreateCommand{
Base: cmd.Base{
Expand Down Expand Up @@ -231,7 +232,7 @@ func createCommandAll() *googlepubsub.CreateCommand {
g.APIClient, _ = mock.APIClient(mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
})("token", "endpoint")
})("token", "endpoint", false)

return &googlepubsub.CreateCommand{
Base: cmd.Base{
Expand Down

0 comments on commit 8d2884e

Please sign in to comment.