generated from brevdev/seed
/
importideconfig.go
220 lines (191 loc) · 6 KB
/
importideconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package importideconfig
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/hashicorp/go-multierror"
"github.com/tidwall/gjson"
"golang.org/x/text/encoding/charmap"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/spf13/cobra"
)
// startLong = "[internal] test"
var startExample = "[internal] test"
type ImportIDEConfigStore interface {
GetCurrentUser() (*entity.User, error)
UpdateUser(userID string, updatedUser *entity.UpdateUser) (*entity.User, error)
GetWindowsDir() (string, error)
}
func NewCmdImportIDEConfig(t *terminal.Terminal, s ImportIDEConfigStore) *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{"housekeeping": ""},
Use: "import-ide-config",
DisableFlagsInUseLine: true,
Short: "Import your IDE config",
Long: "Import your IDE config like vscode extensions",
Example: startExample,
// Args: cmderrors.TransformToValidationError(cobra.MinimumNArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
return breverrors.WrapAndTrace(RunImportIDEConfig(t, s))
},
}
return cmd
}
type ImportIDEConfigError struct {
Err error
}
func (e *ImportIDEConfigError) Error() string {
return e.Err.Error()
}
func importIDEConfigError(err error) error {
return &ImportIDEConfigError{Err: err}
}
func RunImportIDEConfig(_ *terminal.Terminal, store ImportIDEConfigStore) error {
fmt.Println("updating vscode extensions...")
homedir, err := os.UserHomeDir()
if err != nil {
return breverrors.WrapAndTrace(importIDEConfigError(err))
}
windowsUserDir, _ := store.GetWindowsDir()
// if err != nil {
// // todo multierror
// }
var extensions []entity.VscodeExtensionMetadata
me := multierror.Append(nil)
for _, dir := range []string{homedir, windowsUserDir} {
if dir == "" {
continue
}
exts, err2 := getExtensions(dir)
if err2 != nil {
me = multierror.Append(me, err2)
}
extensions = append(extensions, exts...)
}
if me.ErrorOrNil() != nil && len(extensions) == 0 {
return breverrors.WrapAndTrace(importIDEConfigError(errors.New("no vscode extensions found")))
}
// todo print me (multierror) if not nil
user, err := store.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(importIDEConfigError(err))
}
_, err = store.UpdateUser(user.ID, &entity.UpdateUser{
IdeConfig: entity.IDEConfig{
VSCode: entity.VSCodeConfig{
Extensions: extensions,
},
},
})
if err != nil {
return breverrors.WrapAndTrace(importIDEConfigError(err))
}
return nil
}
// Create a VSCodeMetadataObject from package.json file
func createVSCodeMetadataObject(homedir string, path string) (*entity.VscodeExtensionMetadata, error) {
segments := strings.Split(path, "/")
if !strings.Contains(segments[0], ".vscode") &&
segments[1] != "extensions" && segments[3] != "package.json" {
return nil, errors.New("extension could not be imported") // TODO: return this as a metric!!!
}
contents, err := catFile(homedir + "/" + path)
if err != nil {
return nil, err
} else {
repoBlock := gjson.Get(contents, "repository").String()
repoURL := gjson.Get(repoBlock, "url").String()
return &entity.VscodeExtensionMetadata{
Name: gjson.Get(contents, "name").String(),
DisplayName: gjson.Get(contents, "displayName").String(),
Version: gjson.Get(contents, "version").String(),
Publisher: gjson.Get(contents, "publisher").String(),
Description: gjson.Get(contents, "description").String(),
Repository: repoURL,
}, nil
}
}
func catFile(filePath string) (string, error) {
gocmd := exec.Command("cat", filePath) // #nosec G204
in, err := gocmd.Output()
if err != nil {
return "", breverrors.WrapAndTrace(err, "error reading file "+filePath)
} else {
d := charmap.CodePage850.NewDecoder()
out, err := d.Bytes(in)
if err != nil {
return "", breverrors.WrapAndTrace(err, "error reading file "+filePath)
}
return string(out), nil
}
}
func appendPath(a string, b string) string {
if a == "." {
return b
}
return path.Join(a, b)
}
func getExtensions(homedir string) ([]entity.VscodeExtensionMetadata, error) {
var extensions []entity.VscodeExtensionMetadata
// intentionally reading from .vscode and not .vscode_extensions because if they want the extension, it should be installed locally
paths, err := recursivelyFindFile([]string{"package.json"}, homedir+"/.vscode/extensions")
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
var err1 error
paths, err1 = recursivelyFindFile([]string{"package.json"}, homedir+"/.vscode-server/extensions")
if err1 != nil {
return nil, multierror.Append(err, err1)
}
} else {
return nil, breverrors.WrapAndTrace(err)
}
}
for _, v := range paths {
pathWithoutHome := strings.Split(v, homedir+"/")[1]
// of the format
// .vscode / extensions / extension_name / package.json
// 1 2 3 4s
if len(strings.Split(pathWithoutHome, "/")) == 4 {
obj, err0 := createVSCodeMetadataObject(homedir, pathWithoutHome)
if err0 != nil {
return nil, err0
}
extensions = append(extensions, *obj)
}
}
return extensions, nil
}
// Returns list of paths to file
func recursivelyFindFile(filenames []string, path string) ([]string, error) {
var paths []string
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
for _, f := range files {
dir, err := os.Stat(appendPath(path, f.Name()))
if err != nil {
return nil, breverrors.WrapAndTrace(err)
} else {
for _, filename := range filenames {
if filename == f.Name() {
paths = append(paths, appendPath(path, f.Name()))
}
}
if dir.IsDir() {
res, err := recursivelyFindFile(filenames, appendPath(path, f.Name()))
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
paths = append(paths, res...)
}
}
}
return paths, nil
}