generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 10
/
jetbrainsgateway.go
133 lines (115 loc) · 3.12 KB
/
jetbrainsgateway.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
package store
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/spf13/afero"
)
const (
JetbrainsGatewayConfigFileName = "sshConfigs.xml"
)
func getJebrainsConfigDir(home string) (string, error) {
var infix string
infixSuffix := "Jetbrains"
switch runtime.GOOS {
case "windows":
return "", errors.New("windows not supported at this time")
case "linux":
infix = filepath.Join(".config", infixSuffix)
case "darwin":
infix = filepath.Join("Library", "Application Support", infixSuffix)
default:
return "", fmt.Errorf("invalid goos")
}
path := filepath.Join(home, infix)
filePaths, err := getDirsWithPrefix(path, "JetBrainsGateway")
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
return "", errors.New("could not find jetbrains gateway path")
} else if err != nil {
return "", breverrors.WrapAndTrace(err)
}
if len(filePaths) == 0 {
return "", errors.New("could not find jetbrains gateway path")
}
jbgwPath := filePaths[len(filePaths)-1]
return filepath.Join(jbgwPath, "options"), nil
}
func getDirsWithPrefix(parentPath string, prefix string) ([]string, error) {
var files []string
err := filepath.Walk(parentPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return breverrors.WrapAndTrace(err)
}
if info.IsDir() && strings.HasPrefix(info.Name(), prefix) {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
sort.Strings(files)
return files, nil
}
func (f FileStore) GetJetBrainsConfigPath() (string, error) {
home, err := f.UserHomeDir()
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
configDir, err := getJebrainsConfigDir(home)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
sshConfigPath := filepath.Join(configDir, JetbrainsGatewayConfigFileName)
return sshConfigPath, nil
}
func (f FileStore) GetJetBrainsConfig() (string, error) {
path, err := f.GetJetBrainsConfigPath()
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
file, err := f.GetOrCreateFile(path)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
buf := new(strings.Builder)
_, err = io.Copy(buf, file)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return buf.String(), nil
}
func (f FileStore) DoesJetbrainsFilePathExist() (bool, error) {
home, err := f.UserHomeDir()
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
_, err = getJebrainsConfigDir(home)
if err != nil && strings.Contains(err.Error(), "could not find jetbrains gateway path") {
return false, nil
}
return true, nil
}
func (f FileStore) WriteJetBrainsConfig(config string) error {
path, err := f.GetJetBrainsConfigPath()
if err != nil {
return breverrors.WrapAndTrace(err)
}
basePath := filepath.Dir(path)
dir := afero.NewBasePathFs(f.fs, basePath)
err = dir.MkdirAll("", 0o755)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = afero.WriteFile(f.fs, path, []byte(config), 0o644)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}