15
15
package file
16
16
17
17
import (
18
- "bytes"
19
18
"encoding/json"
20
19
"fmt"
21
20
"path/filepath"
22
- "strings"
23
21
24
- "github.com/mongodb/mongocli/internal/config"
25
22
"github.com/mongodb/mongocli/internal/search"
26
- "github.com/mongodb/mongocli/internal/version"
27
23
"github.com/spf13/afero"
28
24
"gopkg.in/yaml.v2"
29
25
)
@@ -39,15 +35,15 @@ const (
39
35
var supportedExts = []string {jsonName , yamlName , ymlName }
40
36
41
37
// 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 ) {
43
39
ext := filepath .Ext (filename )
44
40
45
41
if len (ext ) <= 1 {
46
42
return "" , fmt .Errorf ("filename: %s requires valid extension" , filename )
47
43
}
48
44
49
45
configType := ext [1 :]
50
- if ! search .StringInSlice (supportedExts , configType ) {
46
+ if ! search .StringInSlice (supported , configType ) {
51
47
return "" , fmt .Errorf ("unsupported file type: %s" , configType )
52
48
}
53
49
return configType , nil
@@ -60,7 +56,7 @@ func Load(fs afero.Fs, filename string, out interface{}) error {
60
56
return fmt .Errorf ("file not found: %s" , filename )
61
57
}
62
58
63
- configType , err := configType (filename )
59
+ configType , err := configType (filename , supportedExts )
64
60
if err != nil {
65
61
return err
66
62
}
@@ -85,57 +81,23 @@ func Load(fs afero.Fs, filename string, out interface{}) error {
85
81
}
86
82
87
83
// 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.
89
85
func Save (fs afero.Fs , filePath string , data interface {}) error {
90
86
var content []byte
91
87
92
- configType , err := configType (filePath )
93
- if err != nil {
88
+ if _ , err := configType (filePath , []string {yamlName }); err != nil {
94
89
return err
95
90
}
96
91
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 )
111
93
if err != nil {
112
94
return err
113
95
}
114
96
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 )
129
98
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
137
100
}
138
101
139
- path .WriteString (fileName )
140
- return path .String (), nil
102
+ return afero .WriteFile (fs , filePath , content , filePermission )
141
103
}
0 commit comments