generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
importideconfig.go
179 lines (157 loc) · 4.98 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
package importideconfig
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"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)
}
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
}
func RunImportIDEConfig(_ *terminal.Terminal, store ImportIDEConfigStore) error {
fmt.Println("updating vscode extensions...")
homedir, err := os.UserHomeDir()
if err != nil {
return breverrors.WrapAndTrace(err)
}
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 multierror.Append(err, err1)
}
} else {
return 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 err0
}
extensions = append(extensions, *obj)
}
}
user, err := store.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(err)
}
_, err = store.UpdateUser(user.ID, &entity.UpdateUser{
IdeConfig: entity.IDEConfig{
VSCode: entity.VSCodeConfig{
Extensions: extensions,
},
},
})
if err != nil {
return breverrors.WrapAndTrace(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 a + "/" + b
}
// 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
}