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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD
# Enables support for tools such as https://github.com/rakyll/gotest
TEST_COMMAND ?= go test

TESTARGS ?= ./{cmd,pkg}/...
# The compute tests can sometimes exceed the default 10m limit.
TESTARGS ?= -timeout 15m ./{cmd,pkg}/...

CLI_ENV ?= "development"

Expand Down
2 changes: 2 additions & 0 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func defineCommands(
profileDelete := profile.NewDeleteCommand(profileCmdRoot.CmdClause, globals)
profileList := profile.NewListCommand(profileCmdRoot.CmdClause, globals)
profileSwitch := profile.NewSwitchCommand(profileCmdRoot.CmdClause, globals)
profileToken := profile.NewTokenCommand(profileCmdRoot.CmdClause, globals)
profileUpdate := profile.NewUpdateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals)
purgeCmdRoot := purge.NewRootCommand(app, globals, data)
serviceCmdRoot := service.NewRootCommand(app, globals)
Expand Down Expand Up @@ -558,6 +559,7 @@ func defineCommands(
profileDelete,
profileList,
profileSwitch,
profileToken,
profileUpdate,
purgeCmdRoot,
serviceCmdRoot,
Expand Down
5 changes: 5 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4431,6 +4431,11 @@ COMMANDS
Switch user profile


profile token [<flags>]
Print access token

-n, --name=NAME Print access token for the named profile

profile update [<profile>]
Update user profile

Expand Down
3 changes: 1 addition & 2 deletions pkg/commands/profile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"github.com/fastly/go-fastly/v6/fastly"
)

// CreateCommand is the parent command for all subcommands in this package.
// It should be installed under the primary root command.
// CreateCommand represents a Kingpin command.
type CreateCommand struct {
cmd.Base

Expand Down
121 changes: 121 additions & 0 deletions pkg/commands/profile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,127 @@ func TestSwitch(t *testing.T) {
}
}

func TestToken(t *testing.T) {
var (
configPath string
data []byte
)

// Create temp environment to run test code within.
{
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

// Read the test config.toml data
path, err := filepath.Abs(filepath.Join("./", "testdata", "config.toml"))
if err != nil {
t.Fatal(err)
}
data, err = os.ReadFile(path)
if err != nil {
t.Fatal(err)
}

// Create a new test environment along with a test config.toml file.
rootdir := testutil.NewEnv(testutil.EnvOpts{
T: t,
Write: []testutil.FileIO{
{Src: string(data), Dst: "config.toml"},
},
})
configPath = filepath.Join(rootdir, "config.toml")
defer os.RemoveAll(rootdir)

if err := os.Chdir(rootdir); err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
}

args := testutil.Args
scenarios := []Scenario{
{
TestScenario: testutil.TestScenario{
Name: "validate the active profile token is displayed by default",
Args: args("profile token"),
WantOutput: "123",
},
ConfigFile: config.File{
Profiles: config.Profiles{
"foo": &config.Profile{
Default: true,
Email: "[email protected]",
Token: "123",
},
"bar": &config.Profile{
Default: false,
Email: "[email protected]",
Token: "456",
},
},
},
},
{
TestScenario: testutil.TestScenario{
Name: "validate token is displayed for the specified profile",
Args: args("profile token --name bar"), // we choose a non-default profile
WantOutput: "456",
},
ConfigFile: config.File{
Profiles: config.Profiles{
"foo": &config.Profile{
Default: true,
Email: "[email protected]",
Token: "123",
},
"bar": &config.Profile{
Default: false,
Email: "[email protected]",
Token: "456",
},
},
},
},
{
TestScenario: testutil.TestScenario{
Name: "validate an unrecognised profile causes an error",
Args: args("profile token --name unknown"),
WantError: "profile 'unknown' does not exist",
},
},
}

for _, testcase := range scenarios {
t.Run(testcase.Name, func(t *testing.T) {
var (
err error
stdout bytes.Buffer
)

opts := testutil.NewRunOpts(testcase.Args, &stdout)
opts.APIClient = mock.APIClient(testcase.API)

// We override the config path so that we don't accidentally write over
// our own configuration file.
opts.ConfigPath = configPath

// The read of the config file only really happens in the main()
// function, so for the sake of the test environment we need to construct
// an in-memory representation of the config file we want to be using.
opts.ConfigFile = testcase.ConfigFile

err = app.Run(opts)

t.Log(stdout.String())

testutil.AssertErrorContains(t, err, testcase.WantError)
testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput)
})
}
}

func TestUpdate(t *testing.T) {
var (
configPath string
Expand Down
51 changes: 51 additions & 0 deletions pkg/commands/profile/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package profile

import (
"fmt"
"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/profile"
"github.com/fastly/cli/pkg/text"
)

// TokenCommand represents a Kingpin command.
type TokenCommand struct {
cmd.Base
profile string
}

// NewTokenCommand returns a new command registered in the parent.
func NewTokenCommand(parent cmd.Registerer, globals *config.Data) *TokenCommand {
var c TokenCommand
c.Globals = globals
c.CmdClause = parent.Command("token", "Print access token")
c.CmdClause.Flag("name", "Print access token for the named profile").Short('n').StringVar(&c.profile)
return &c
}

// Exec implements the command interface.
func (c *TokenCommand) Exec(in io.Reader, out io.Writer) (err error) {
if c.profile == "" {
if name, p := profile.Default(c.Globals.File.Profiles); name != "" {
text.Output(out, p.Token)
return nil
}
return fsterr.RemediationError{
Inner: fmt.Errorf("no profiles available"),
Remediation: fsterr.ProfileRemediation,
}
}

if name, p := profile.Get(c.profile, c.Globals.File.Profiles); name != "" {
text.Output(out, p.Token)
return nil
}
msg := fmt.Sprintf(profile.DoesNotExist, c.profile)
return fsterr.RemediationError{
Inner: fmt.Errorf(msg),
Remediation: fsterr.ProfileRemediation,
}
}