Skip to content

CLOUDP-329797: Make atlas config init alias of atlas auth login #4044

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

Merged
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
84 changes: 0 additions & 84 deletions docs/command/atlas-config-init.txt

This file was deleted.

2 changes: 0 additions & 2 deletions docs/command/atlas-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Related Commands
* :ref:`atlas-config-delete` - Delete a profile.
* :ref:`atlas-config-describe` - Return the profile you specify.
* :ref:`atlas-config-edit` - Opens the config file with the default text editor.
* :ref:`atlas-config-init` - Configure a profile to store access settings for your MongoDB deployment.
* :ref:`atlas-config-list` - Return a list of available profiles by name.
* :ref:`atlas-config-rename` - Rename a profile.
* :ref:`atlas-config-set` - Configure specific properties of a profile.
Expand All @@ -76,7 +75,6 @@ Related Commands
delete </command/atlas-config-delete>
describe </command/atlas-config-describe>
edit </command/atlas-config-edit>
init </command/atlas-config-init>
list </command/atlas-config-list>
rename </command/atlas-config-rename>
set </command/atlas-config-set>
Expand Down
1 change: 0 additions & 1 deletion internal/cli/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ func LoginBuilder() *cobra.Command {
opts.SyncWithOAuthAccessProfile(defaultProfile),
opts.InitFlow(defaultProfile),
opts.LoginPreRun(cmd.Context()),
validate.NoAPIKeys,
validate.NoAccessToken,
)
},
Expand Down
3 changes: 1 addition & 2 deletions internal/cli/commonerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ var (
errAsymmetricShardUnsupported = errors.New("trying to run a cluster wide scaling command on an independent shard scaling cluster. Use --autoScalingMode 'independentShardScaling' instead")
ErrUnauthorized = errors.New(`this action requires authentication

To log in using your Atlas username and password, run: atlas auth login
To set credentials using API keys, run: atlas config init`)
To log in using your Atlas username and password or to set credentials using API keys, run: atlas auth login`)
ErrInvalidRefreshToken = errors.New(`session expired

Please note that your session expires periodically.
Expand Down
100 changes: 6 additions & 94 deletions internal/cli/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,106 +15,18 @@
package config

import (
"context"
"fmt"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/prompt"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/auth"
"github.com/spf13/cobra"
)

const atlas = "atlas"

type initOpts struct {
cli.DigestConfigOpts
gov bool
}

func (opts *initOpts) SetUpAccess() {
opts.Service = config.CloudService
if opts.gov {
opts.Service = config.CloudGovService
}

opts.SetUpServiceAndKeys()
}

func (opts *initOpts) Run(ctx context.Context) error {
_, _ = fmt.Fprintf(opts.OutWriter, `You are configuring a profile for %s.

All values are optional and you can use environment variables (MONGODB_ATLAS_*) instead.

Enter [?] on any option to get help.

`, atlas)

q := prompt.AccessQuestions()
if err := telemetry.TrackAsk(q, opts); err != nil {
return err
}
opts.SetUpAccess()

if err := opts.InitStore(ctx); err != nil {
return err
}

if config.IsAccessSet() {
if err := opts.AskOrg(); err != nil {
return err
}
if err := opts.AskProject(); err != nil {
return err
}
} else {
q := prompt.TenantQuestions()
if err := telemetry.TrackAsk(q, opts); err != nil {
return err
}
}
opts.SetUpProject()
opts.SetUpOrg()

if err := telemetry.TrackAsk(opts.DefaultQuestions(), opts); err != nil {
return err
}
opts.SetUpOutput()

if err := config.Save(); err != nil {
return err
}

_, _ = fmt.Fprintf(opts.OutWriter, "\nYour profile is now configured.\n")
if config.Name() != config.DefaultProfile {
_, _ = fmt.Fprintf(opts.OutWriter, "To use this profile, you must set the flag [-%s %s] for every command.\n", flag.ProfileShort, config.Name())
}
_, _ = fmt.Fprintf(opts.OutWriter, "You can use [%s config set] to change these settings at a later time.\n", atlas)
return nil
}

func InitBuilder() *cobra.Command {
opts := &initOpts{}
cmd := &cobra.Command{
Use: "init",
Short: "Configure a profile to store access settings for your MongoDB deployment.",
Example: ` # To configure the tool to work with Atlas:
cmd := auth.LoginBuilder()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are doing this, does it make sense to deprecate this command?

cmd.Use = "init"
cmd.Example = ` # To configure the tool to work with Atlas:
atlas config init

# To configure the tool to work with Atlas for Government:
atlas config init --gov`,
PreRun: func(cmd *cobra.Command, _ []string) {
opts.OutWriter = cmd.OutOrStdout()
},
RunE: func(cmd *cobra.Command, _ []string) error {
return opts.Run(cmd.Context())
},
Args: require.NoArgs,
}
cmd.Flags().BoolVar(&opts.gov, flag.Gov, false, usage.Gov)

atlas config init --gov`
cmd.Deprecated = "Please use the 'atlas auth login' command instead."
return cmd
}
9 changes: 9 additions & 0 deletions test/e2e/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func TestConfig(t *testing.T) {
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
if _, err = c.ExpectString("Select authentication type"); err != nil {
t.Fatal(err)
}
if _, err := c.Send("\x1B[B"); err != nil {
t.Fatalf("Send(Down) = %v", err)
}
if _, err := c.SendLine(""); err != nil {
t.Fatalf("SendLine() = %v", err)
}

if _, err = c.ExpectString("Public API Key"); err != nil {
t.Fatal(err)
Expand Down
Loading