From 52498ac27a424048641d99999def9c05e5425ab3 Mon Sep 17 00:00:00 2001 From: Bianca Lisle <40155621+blva@users.noreply.github.com> Date: Mon, 13 May 2024 12:04:50 +0100 Subject: [PATCH] CLOUDP-248054: Stop copying mongocli config into atlascli (#2945) --- build/ci/evergreen.yml | 4 +- cmd/atlas/atlas.go | 87 ++------------------------------- internal/config/profile.go | 26 ---------- internal/config/profile_test.go | 35 ------------- 4 files changed, 5 insertions(+), 147 deletions(-) diff --git a/build/ci/evergreen.yml b/build/ci/evergreen.yml index 01881117c3..6294151326 100644 --- a/build/ci/evergreen.yml +++ b/build/ci/evergreen.yml @@ -167,8 +167,8 @@ tasks: shell: bash script: | set -Eeou pipefail - mkdir "$XDG_CONFIG_HOME/mongocli" - cat < "$XDG_CONFIG_HOME/mongocli/config.toml" + mkdir "$XDG_CONFIG_HOME/atlascli" + cat < "$XDG_CONFIG_HOME/atlascli/config.toml" [e2e] org_id = "5e429e7706822c6eac4d5971" public_api_key = "AAUMGJXA" diff --git a/cmd/atlas/atlas.go b/cmd/atlas/atlas.go index 5e7b3688b2..567e742d49 100644 --- a/cmd/atlas/atlas.go +++ b/cmd/atlas/atlas.go @@ -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" ) @@ -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 @@ -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 - } - } - - 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) { @@ -181,7 +100,7 @@ func main() { } initTrack() - createConfigFromMongoCLIConfig() + createConfig() trackInitError(loadConfig()) Execute() diff --git a/internal/config/profile.go b/internal/config/profile.go index 277908a857..2b190aecfc 100644 --- a/internal/config/profile.go +++ b/internal/config/profile.go @@ -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() diff --git a/internal/config/profile_test.go b/internal/config/profile_test.go index 12d5f729ef..e6ebed5d62 100644 --- a/internal/config/profile_test.go +++ b/internal/config/profile_test.go @@ -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 {