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

CLOUDP-248054: Stop copying mongocli config into atlascli #2945

Merged
merged 3 commits into from
May 13, 2024
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
4 changes: 2 additions & 2 deletions build/ci/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ tasks:
shell: bash
script: |
set -Eeou pipefail
mkdir "$XDG_CONFIG_HOME/mongocli"
cat <<EOF > "$XDG_CONFIG_HOME/mongocli/config.toml"
mkdir "$XDG_CONFIG_HOME/atlascli"
cat <<EOF > "$XDG_CONFIG_HOME/atlascli/config.toml"
[e2e]
org_id = "5e429e7706822c6eac4d5971"
public_api_key = "AAUMGJXA"
Expand Down
87 changes: 3 additions & 84 deletions cmd/atlas/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ package main

import (
"fmt"
"io"
"log"
"os"
"path"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/root"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/terminal"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,28 +58,8 @@ func loadConfig() error {
return nil
}

func shouldCopyConfig(atlasConfigPath string) bool {
// Keep backward compatibility and copy if non-tty. If any shows as non-tty, then we can't ask
// questions.
if !terminal.IsTerminal(os.Stdout) || !terminal.IsTerminal(os.Stderr) || !terminal.IsTerminalInput(os.Stdin) {
return true
}

var response bool
question := &survey.Confirm{
Message: fmt.Sprintf("Atlas CLI has found an existing MongoDB CLI configuration file, would you like to copy its content? (destination:%s)", atlasConfigPath),
Default: true,
}

if err := telemetry.TrackAskOne(question, &response); err != nil {
return false
}

return response
}

// createConfigFromMongoCLIConfig creates the atlasCLI config file from the mongocli config file.
func createConfigFromMongoCLIConfig() {
// createConfig creates the atlasCLI config file if not existent.
func createConfig() {
atlasConfigHomePath, err := config.CLIConfigHome()
if err != nil {
return
Expand All @@ -94,64 +71,6 @@ func createConfigFromMongoCLIConfig() {
f.Close()
return
}

p, err := mongoCLIConfigFilePath()
if err != nil {
return
}

in, err := os.Open(p)
if err != nil {
return
}
defer in.Close()

_, err = os.Stat(atlasConfigHomePath) // check if the dir is already there
if err != nil {
defaultPermissions := 0700
if err = os.Mkdir(atlasConfigHomePath, os.FileMode(defaultPermissions)); err != nil {
return
}
}
Comment on lines -109 to -115
Copy link
Collaborator

Choose a reason for hiding this comment

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

have you tried the changes locally with no folder present? I'm wondering if this is still needed when installing on a clean system

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, seems fine, here are some of the commands I tried in case I missed something:

  • other commands
❯ ./bin/atlas deployments list
NAME   TYPE   MDB VER   STATE

To list both local and cloud Atlas deployments, authenticate to your Atlas account using the "atlas login" command.
❯ ./bin/atlas clusters list
Error: 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
❯ ./bin/atlas login

To verify your account, copy your one-time verification code:
KRMH-FY38

Paste the code in the browser when prompted to activate your Atlas CLI. Your code will expire after 10 minutes.

To continue, go to https://account.mongodb.com/account/connect
  • list before deleting / init then list
❯ ./bin/atlas config list
PROFILE NAME
Usage:  atlas config delete <name> [flags]
❯ ./bin/atlas config init
You are configuring a profile for atlas.

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

Enter [?] on any option to get help.

? Public API Key:
? Private API Key: [? for help]
? Default Project ID:
? Default Org ID:
? Default Output Format: plaintext

Your profile is now configured.
You can use [atlas config set] to change these settings at a later time.
❯ ./bin/atlas config list
PROFILE NAME
default

Copy link
Collaborator

Choose a reason for hiding this comment

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

can you do do mv ~/Library/Application\ Support/atlascli ~/Library/Application\ Support/atlasclibck before doing login? that's the part I'm curios, if the folder doesn't exists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

❯ mv ~/Library/Application\ Support/atlascli ~/Library/Application\ Support/atlasclibck
❯ ./bin/atlas login

To verify your account, copy your one-time verification code:
BVMR-QWR3

Paste the code in the browser when prompted to activate your Atlas CLI. Your code will expire after 10 minutes.

To continue, go to https://account.mongodb.com/account/connect
^C%
❯ ./bin/atlas clusters list
Error: 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
❯ git pull
❯ ./bin/atlas config list
PROFILE NAME

Copy link
Collaborator

Choose a reason for hiding this comment

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

To continue, go to https://account.mongodb.com/account/connect
^C%

can you continue and see if the file is created? the thing to check if we need to pre create the folder or will it be created along the file, since you are deleting the code that created the folder before trying to create the file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, worked fine!

❯ ./bin/atlas login

To verify your account, copy your one-time verification code:
Q9K3-J896

Paste the code in the browser when prompted to activate your Atlas CLI. Your code will expire after 10 minutes.

To continue, go to https://account.mongodb.com/account/connect
Successfully logged in as blvaguiar@gmail.com.
Select one default organization and one default project.
? Choose a default project: 623202922f697375cac9414a

You have successfully configured your profile.
You can use [atlas config set] to change your profile settings later.
❯ ./bin/atlas config list
PROFILE NAME
default

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for checking this :shipit:


if !shouldCopyConfig(atlasConfigPath) {
return
}

out, err := os.Create(atlasConfigPath)
if err != nil {
return
}
defer out.Close()

if _, err = io.Copy(out, in); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "There was an error generating %s: %v", atlasConfigPath, err)
return
}

_, _ = fmt.Fprintf(os.Stderr, `AtlasCLI has copied your MongoCLI configuration to: %s

`, atlasConfigPath)
}

func mongoCLIConfigFilePath() (configPath string, err error) {
if configDir, err := config.MongoCLIConfigHome(); err == nil {
configPath = path.Join(configDir, "config.toml")
}

// Check if file exists, if any error is detected try to get older file
if _, err := os.Stat(configPath); err == nil {
return configPath, nil
}

if configDir, err := config.OldMongoCLIConfigHome(); err == nil { //nolint:staticcheck // Deprecated before fully removing support in the future
configPath = path.Join(configDir, "mongocli.toml")
}

if _, err := os.Stat(configPath); err != nil {
return "", err
}
return configPath, nil
}

func trackInitError(e error) {
Expand Down Expand Up @@ -181,7 +100,7 @@ func main() {
}

initTrack()
createConfigFromMongoCLIConfig()
createConfig()
trackInitError(loadConfig())

Execute()
Expand Down
26 changes: 0 additions & 26 deletions internal/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,32 +708,6 @@ func (p *Profile) Save() error {
return viper.WriteConfigAs(p.Filename())
}

// OldMongoCLIConfigHome retrieves configHome path based used by MongoCLI.
//
// Deprecated: MongoCLI versions below v1.24.0 use this path.
func OldMongoCLIConfigHome() (string, error) {
if home := os.Getenv("XDG_CONFIG_HOME"); home != "" {
return home, nil
}

home, err := os.UserHomeDir()
if err != nil {
return "", err
}

return path.Join(home, ".config"), nil
}

// MongoCLIConfigHome retrieves configHome path based used by MongoCLI.
func MongoCLIConfigHome() (string, error) {
home, err := os.UserConfigDir()
if err != nil {
return "", err
}

return path.Join(home, "mongocli"), nil
}

// CLIConfigHome retrieves configHome path.
func CLIConfigHome() (string, error) {
home, err := os.UserConfigDir()
Expand Down
35 changes: 0 additions & 35 deletions internal/config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestMongoCLIConfigHome(t *testing.T) {
expHome, err := os.UserConfigDir()
require.NoError(t, err)

home, err := MongoCLIConfigHome()
require.NoError(t, err)
expected := fmt.Sprintf("%s/mongocli", expHome)
assert.Equal(t, expected, home)
}

func TestOldMongoCLIConfigHome(t *testing.T) {
t.Run("old home with XDG_CONFIG_HOME", func(t *testing.T) {
const xdgHome = "my_config"
t.Setenv("XDG_CONFIG_HOME", xdgHome)
home, err := OldMongoCLIConfigHome()
if err != nil {
t.Fatalf("OldMongoCLIConfigHome() unexpected error: %v", err)
}
if home != xdgHome {
t.Errorf("MongoCLIConfigHome() = %s; want '%s'", home, xdgHome)
}
})
t.Run("old home without XDG_CONFIG_HOME", func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", "")
home, err := OldMongoCLIConfigHome()
if err != nil {
t.Fatalf("OldMongoCLIConfigHome() unexpected error: %v", err)
}
osHome, _ := os.UserHomeDir()
if home != osHome+"/.config" {
t.Errorf("OldMongoCLIConfigHome() = %s; want '%s/.config'", home, osHome)
}
})
}

func TestCLIConfigHome(t *testing.T) {
expHome, err := os.UserConfigDir()
if err != nil {
Expand Down