Skip to content

Commit

Permalink
CLOUDP-248054: Stop copying mongocli config into atlascli (#2945)
Browse files Browse the repository at this point in the history
  • Loading branch information
blva committed May 13, 2024
1 parent 7d13550 commit 52498ac
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 147 deletions.
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
}
}

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

0 comments on commit 52498ac

Please sign in to comment.