Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
664 changes: 332 additions & 332 deletions pkg/app/commands.go

Large diffs are not rendered by default.

70 changes: 36 additions & 34 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/fastly/cli/pkg/env"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/github"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/lookup"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/profile"
"github.com/fastly/cli/pkg/revision"
Expand Down Expand Up @@ -44,12 +46,12 @@ func Run(opts RunOpts) error {
md.File.SetOutput(opts.Stdout)
_ = md.File.Read(manifest.Filename)

// The globals will hold generally-applicable configuration parameters
// The g will hold generally-applicable configuration parameters
// from a variety of sources, and is provided to each concrete command.
globals := config.Data{
g := global.Data{
Env: opts.Env,
ErrLog: opts.ErrLog,
File: opts.ConfigFile,
Config: opts.ConfigFile,
HTTPClient: opts.HTTPClient,
Manifest: md,
Output: opts.Stdout,
Expand Down Expand Up @@ -84,17 +86,17 @@ func Run(opts RunOpts) error {
//
// NOTE: Short flags CAN be safely reused across commands.
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(&globals.Flag.AcceptDefaults)
app.Flag("auto-yes", "Answer yes automatically to all Yes/No confirmations. This may suppress security warnings").Short('y').BoolVar(&globals.Flag.AutoYes)
app.Flag("endpoint", "Fastly API endpoint").Hidden().StringVar(&globals.Flag.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(&globals.Flag.NonInteractive)
app.Flag("profile", "Switch account profile for single command execution (see also: 'fastly profile switch')").Short('o').StringVar(&globals.Flag.Profile)
app.Flag("quiet", "Silence all output except direct command output. This won't prevent interactive prompts (see: --accept-defaults, --auto-yes, --non-interactive)").Short('q').BoolVar(&globals.Flag.Quiet)
app.Flag("token", tokenHelp).Short('t').StringVar(&globals.Flag.Token)
app.Flag("verbose", "Verbose logging").Short('v').BoolVar(&globals.Flag.Verbose)

commands := defineCommands(app, &globals, md, opts)
command, name, err := processCommandInput(opts, app, &globals, commands)
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)
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)
app.Flag("quiet", "Silence all output except direct command output. This won't prevent interactive prompts (see: --accept-defaults, --auto-yes, --non-interactive)").Short('q').BoolVar(&g.Flags.Quiet)
app.Flag("token", tokenHelp).Short('t').StringVar(&g.Flags.Token)
app.Flag("verbose", "Verbose logging").Short('v').BoolVar(&g.Flags.Verbose)

commands := defineCommands(app, &g, md, opts)
command, name, err := processCommandInput(opts, app, &g, commands)
if err != nil {
return err
}
Expand All @@ -111,22 +113,22 @@ func Run(opts RunOpts) error {
return nil
}

if globals.Flag.Quiet {
if g.Flags.Quiet {
md.File.SetQuiet(true)
}

token, source := globals.Token()
token, source := g.Token()

if globals.Verbose() {
if g.Verbose() {
displayTokenSource(
source,
opts.Stdout,
env.Token,
determineProfile(md.File.Profile, globals.Flag.Profile, globals.File.Profiles),
determineProfile(md.File.Profile, g.Flags.Profile, g.Config.Profiles),
)
}

token, err = profile.Init(token, &md, &globals, opts.Stdin, opts.Stdout)
token, err = profile.Init(token, &md, &g, opts.Stdin, opts.Stdout)
if err != nil {
return err
}
Expand All @@ -135,10 +137,10 @@ func Run(opts RunOpts) error {
// to assert if they are not too open or have been altered outside of the
// application and warn if so.
segs := strings.Split(name, " ")
if source == config.SourceFile && (len(segs) > 0 && segs[0] != "profile") {
if source == lookup.SourceFile && (len(segs) > 0 && segs[0] != "profile") {
if fi, err := os.Stat(config.FilePath); err == nil {
if mode := fi.Mode().Perm(); mode > config.FilePermissions {
if !globals.Flag.Quiet {
if !g.Flags.Quiet {
text.Warning(opts.Stdout, "Unprotected configuration file.")
fmt.Fprintf(opts.Stdout, "Permissions for '%s' are too open\n", config.FilePath)
fmt.Fprintf(opts.Stdout, "It is recommended that your configuration file is NOT accessible by others.\n")
Expand All @@ -148,12 +150,12 @@ func Run(opts RunOpts) error {
}
}

endpoint, source := globals.Endpoint()
if globals.Verbose() {
endpoint, source := g.Endpoint()
if g.Verbose() {
switch source {
case config.SourceEnvironment:
case lookup.SourceEnvironment:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint (via %s): %s\n", env.Endpoint, endpoint)
case config.SourceFile:
case lookup.SourceFile:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint (via config file): %s\n", endpoint)
default:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint: %s\n", endpoint)
Expand All @@ -162,25 +164,25 @@ func Run(opts RunOpts) error {

// NOTE: We return error immediately so there's no issue assigning to global.
// nosemgrep
globals.APIClient, err = opts.APIClient(token, endpoint)
g.APIClient, err = opts.APIClient(token, endpoint)
if err != nil {
globals.ErrLog.Add(err)
g.ErrLog.Add(err)
return fmt.Errorf("error constructing Fastly API client: %w", err)
}

// NOTE: We return error immediately so there's no issue assigning to global.
// nosemgrep
globals.RTSClient, err = fastly.NewRealtimeStatsClientForEndpoint(token, fastly.DefaultRealtimeStatsEndpoint)
g.RTSClient, err = fastly.NewRealtimeStatsClientForEndpoint(token, fastly.DefaultRealtimeStatsEndpoint)
if err != nil {
globals.ErrLog.Add(err)
g.ErrLog.Add(err)
return fmt.Errorf("error constructing Fastly realtime stats client: %w", err)
}

if opts.Versioners.CLI != nil && name != "update" && !version.IsPreRelease(revision.AppVersion) {
f := update.CheckAsync(
revision.AppVersion,
opts.Versioners.CLI,
globals.Flag.Quiet,
g.Flags.Quiet,
)
defer f(opts.Stdout) // ...and the printing function second, so we hit the timeout
}
Expand Down Expand Up @@ -216,13 +218,13 @@ type Versioners struct {
}

// displayTokenSource prints the token source.
func displayTokenSource(source config.Source, out io.Writer, token, profileSource string) {
func displayTokenSource(source lookup.Source, out io.Writer, token, profileSource string) {
switch source {
case config.SourceFlag:
case lookup.SourceFlag:
fmt.Fprintf(out, "Fastly API token provided via --token\n")
case config.SourceEnvironment:
case lookup.SourceEnvironment:
fmt.Fprintf(out, "Fastly API token provided via %s\n", token)
case config.SourceFile:
case lookup.SourceFile:
fmt.Fprintf(out, "Fastly API token provided via config file (profile: %s)\n", profileSource)
default:
fmt.Fprintf(out, "Fastly API token not provided\n")
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"text/template"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/kingpin"
)
Expand Down Expand Up @@ -208,7 +208,7 @@ const VerboseUsageTemplate = `{{define "FormatCommands" -}}
func processCommandInput(
opts RunOpts,
app *kingpin.Application,
globals *config.Data,
g *global.Data,
commands []cmd.Command,
) (command cmd.Command, cmdName string, err error) {
// As the `help` command model gets privately added as a side-effect of
Expand All @@ -219,15 +219,15 @@ func processCommandInput(
if cmd.ArgsIsHelpJSON(opts.Args) {
j, err := UsageJSON(app)
if err != nil {
globals.ErrLog.Add(err)
g.ErrLog.Add(err)
return command, cmdName, err
}
fmt.Fprintf(opts.Stdout, "%s", j)
return command, strings.Join(opts.Args, ""), nil
}

// Use partial application to generate help output function.
help := displayHelp(globals.ErrLog, opts.Args, app, opts.Stdout, io.Discard)
help := displayHelp(g.ErrLog, opts.Args, app, opts.Stdout, io.Discard)

// Handle parse errors and display contextual usage if possible. Due to bugs
// and an obsession for lots of output side-effects in the kingpin.Parse
Expand Down Expand Up @@ -407,7 +407,7 @@ var metadata []byte
type commandsMetadata map[string]any

// UsageJSON returns a structured representation of the application usage
// documentation in JSON format. This is useful for machine consumtion.
// documentation in JSON format. This is useful for machine consumption.
func UsageJSON(app *kingpin.Application) (string, error) {
var data commandsMetadata
err := json.Unmarshal(metadata, &data)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"io"

"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/env"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
Expand Down Expand Up @@ -52,7 +52,7 @@ type Globals struct {
// Base is stuff that should be included in every concrete command.
type Base struct {
CmdClause *kingpin.CmdClause
Globals *config.Data
Globals *global.Data
}

// Name implements the Command interface, and returns the FullCommand from the
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/acl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand {
func NewCreateCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *CreateCommand {
c := CreateCommand{
Base: cmd.Base{
Globals: globals,
Globals: g,
},
manifest: data,
manifest: m,
}

c.CmdClause = parent.Command("create", "Create a new ACL attached to the specified service version").Alias("add")
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
Out: out,
ServiceNameFlag: c.serviceName,
ServiceVersionFlag: c.serviceVersion,
VerboseMode: c.Globals.Flag.Verbose,
VerboseMode: c.Globals.Flags.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/acl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteCommand {
func NewDeleteCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DeleteCommand {
c := DeleteCommand{
Base: cmd.Base{
Globals: globals,
Globals: g,
},
manifest: data,
manifest: m,
}
c.CmdClause = parent.Command("delete", "Delete an ACL from the specified service version").Alias("remove")

Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
Out: out,
ServiceNameFlag: c.serviceName,
ServiceVersionFlag: c.serviceVersion,
VerboseMode: c.Globals.Flag.Verbose,
VerboseMode: c.Globals.Flags.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/acl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/go-fastly/v7/fastly"
)

// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeCommand {
func NewDescribeCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *DescribeCommand {
c := DescribeCommand{
Base: cmd.Base{
Globals: globals,
Globals: g,
},
manifest: data,
manifest: m,
}
c.CmdClause = parent.Command("describe", "Retrieve a single ACL by name for the version and service").Alias("get")

Expand Down Expand Up @@ -78,7 +78,7 @@ func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
Out: out,
ServiceNameFlag: c.serviceName,
ServiceVersionFlag: c.serviceVersion,
VerboseMode: c.Globals.Flag.Verbose,
VerboseMode: c.Globals.Flags.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/acl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand {
func NewListCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *ListCommand {
c := ListCommand{
Base: cmd.Base{
Globals: globals,
Globals: g,
},
manifest: data,
manifest: m,
}
c.CmdClause = parent.Command("list", "List ACLs")

Expand Down Expand Up @@ -77,7 +77,7 @@ func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
Out: out,
ServiceNameFlag: c.serviceName,
ServiceVersionFlag: c.serviceVersion,
VerboseMode: c.Globals.Flag.Verbose,
VerboseMode: c.Globals.Flags.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/acl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/global"
)

// RootCommand is the parent command for all subcommands in this package.
Expand All @@ -15,9 +15,9 @@ type RootCommand struct {
}

// NewRootCommand returns a new command registered in the parent.
func NewRootCommand(parent cmd.Registerer, globals *config.Data) *RootCommand {
func NewRootCommand(parent cmd.Registerer, g *global.Data) *RootCommand {
var c RootCommand
c.Globals = globals
c.Globals = g
c.CmdClause = parent.Command("acl", "Manipulate Fastly ACLs (Access Control Lists)")
return &c
}
Expand Down
Loading