Skip to content

Commit 2587e90

Browse files
committed
Address minor comments
1 parent 8fa1e85 commit 2587e90

File tree

3 files changed

+10
-66
lines changed

3 files changed

+10
-66
lines changed

internal/file/file.go

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515
package file
1616

1717
import (
18-
"bytes"
1918
"encoding/json"
2019
"fmt"
2120
"path/filepath"
22-
"strings"
2321

24-
"github.com/mongodb/mongocli/internal/config"
2522
"github.com/mongodb/mongocli/internal/search"
26-
"github.com/mongodb/mongocli/internal/version"
2723
"github.com/spf13/afero"
2824
"gopkg.in/yaml.v2"
2925
)
@@ -39,15 +35,15 @@ const (
3935
var supportedExts = []string{jsonName, yamlName, ymlName}
4036

4137
// configType gets the config type from a given file path.
42-
func configType(filename string) (string, error) {
38+
func configType(filename string, supported []string) (string, error) {
4339
ext := filepath.Ext(filename)
4440

4541
if len(ext) <= 1 {
4642
return "", fmt.Errorf("filename: %s requires valid extension", filename)
4743
}
4844

4945
configType := ext[1:]
50-
if !search.StringInSlice(supportedExts, configType) {
46+
if !search.StringInSlice(supported, configType) {
5147
return "", fmt.Errorf("unsupported file type: %s", configType)
5248
}
5349
return configType, nil
@@ -60,7 +56,7 @@ func Load(fs afero.Fs, filename string, out interface{}) error {
6056
return fmt.Errorf("file not found: %s", filename)
6157
}
6258

63-
configType, err := configType(filename)
59+
configType, err := configType(filename, supportedExts)
6460
if err != nil {
6561
return err
6662
}
@@ -85,57 +81,23 @@ func Load(fs afero.Fs, filename string, out interface{}) error {
8581
}
8682

8783
// Save saves a given data interface into a given file path
88-
// The file should be a valid json or yaml format.
84+
// The file should be a yaml format.
8985
func Save(fs afero.Fs, filePath string, data interface{}) error {
9086
var content []byte
9187

92-
configType, err := configType(filePath)
93-
if err != nil {
88+
if _, err := configType(filePath, []string{yamlName}); err != nil {
9489
return err
9590
}
9691

97-
switch configType {
98-
case yamlName, ymlName:
99-
content, err = yaml.Marshal(data)
100-
if err != nil {
101-
return err
102-
}
103-
case jsonName:
104-
content, err = json.Marshal(data)
105-
if err != nil {
106-
return err
107-
}
108-
}
109-
110-
err = fs.MkdirAll(filepath.Dir(filePath), configPermission)
92+
content, err := yaml.Marshal(data)
11193
if err != nil {
11294
return err
11395
}
11496

115-
err = afero.WriteFile(fs, filePath, content, filePermission)
116-
return err
117-
}
118-
119-
func Path(tool, fileName string) (string, error) {
120-
var path bytes.Buffer
121-
var home string
122-
var err error
123-
124-
if tool == version.AtlasCLI {
125-
home, err = config.AtlasCLIConfigHome()
126-
} else {
127-
home, err = config.MongoCLIConfigHome()
128-
}
97+
err = fs.MkdirAll(filepath.Dir(filePath), configPermission)
12998
if err != nil {
130-
return "", err
131-
}
132-
133-
path.WriteString(home)
134-
// Temporary until config home is changed
135-
if !strings.Contains(home, tool) && tool == version.MongoCLI {
136-
path.WriteString("/" + tool)
99+
return err
137100
}
138101

139-
path.WriteString(fileName)
140-
return path.String(), nil
102+
return afero.WriteFile(fs, filePath, content, filePermission)
141103
}

internal/file/file_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package file_test
1919

2020
import (
21-
"encoding/json"
2221
"fmt"
2322
"testing"
2423

@@ -98,18 +97,6 @@ func TestFile(t *testing.T) {
9897
t.Errorf("Save() unexpected error: %v", err)
9998
}
10099
})
101-
t.Run("save valid json file", func(t *testing.T) {
102-
appFS := afero.NewMemMapFs()
103-
filename := jsonFileName
104-
105-
b := []byte(`{"Name":"MongoDB","Age":100,"Children":["mongocli", "atlascli"]}`)
106-
j, _ := json.Marshal(b)
107-
108-
err := file.Save(appFS, filename, j)
109-
if err != nil {
110-
t.Fatalf("Save() unexpected error: %v", err)
111-
}
112-
})
113100
t.Run("save valid yaml file", func(t *testing.T) {
114101
appFS := afero.NewMemMapFs()
115102
filename := yamlFileName

internal/homebrew/path.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ func homebrewFormulaPath(tool string) (string, error) {
7373

7474
brewFormulaPath := strings.TrimSpace(string(brewFormulaPathBytes))
7575

76-
brewFormulaPath, err = filepath.EvalSymlinks(brewFormulaPath)
77-
if err != nil {
78-
return "", err
79-
}
80-
81-
return brewFormulaPath, nil
76+
return filepath.EvalSymlinks(brewFormulaPath)
8277
}
8378

8479
func executableCurrentPath() (string, error) {

0 commit comments

Comments
 (0)