-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistros.go
220 lines (178 loc) · 4.81 KB
/
distros.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
/*
* Copyright (c) 2018 lærling
* For details see ./LICENSE
*/
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
)
func listDistros(distroDirName string) error {
// open distro directory
distroDir, err := os.Open(distroDirName)
if err != nil {
return errors.New("Cannot open directory " +
distroDirName +
". Please make sure that it exists and the permissions are set correctly.")
}
// get distros from distro directory
var distros sortableStringSlice
distros, err = distroDir.Readdirnames(0)
if err != nil {
return err
}
// sort filenames alphabetically (case-insensitively)
sort.Sort(distros)
// list distros
for _, distroName := range distros {
// check that it's really a distro and doesn't start with a dot
if directoryExists(distroDirName+PATHSEP+distroName) && !strings.HasPrefix(distroName, ".") {
fmt.Println(distroName)
}
}
return nil
}
func startDistro(homeDir, distroDir string) {
// set distro as $HOME
originalDistroDir, err := os.Readlink(distroDir)
// if readlink fails, assume distroDir is the original dir and continue
if err != nil {
originalDistroDir = distroDir
}
// check symlinks are present in distro
ensureSymlinksPresent(homeDir, distroDir)
// make Emacs command
emacsExe := "emacs"
if WINDOWS {
emacsExe += ".exe"
}
emacsCmd := exec.Command(emacsExe)
emacsCmd.Env = append(os.Environ(), "HOME="+originalDistroDir)
// launch Emacs asynchronously
err = emacsCmd.Start()
if err != nil {
panic(err)
}
}
func createDistro(homeDir, distroDirName, distroName string) error {
distroDir := distroDirName + PATHSEP + distroName
if err := downloadDistro(distroDirName, distroName); err == nil {
startDistro(homeDir, distroDir)
return nil
}
// distro not found. Ask user whether to create a new distro
fmt.Print("Distribution " + distroName + " does not exist. Create it now? (y/N) ")
var input [1]byte
_, err := os.Stdin.Read(input[:])
if err != nil {
return errors.New("Error reading answer")
}
// check answer
if input[0] != 'y' && input[0] != 'Y' {
return nil
}
// create new distro
if err := ensureDirectoryExists(distroDir); err != nil {
return errors.New("Cannot mkdir " + distroName)
}
// prepare distro
makeInitFile(distroDir)
return nil
}
func downloadDistro(distroDirName, distroUrlOrName string) error {
distroUrl, distroName, err := makeRepoUrl(distroUrlOrName)
if err != nil {
return err
}
// assume https
if !strings.Contains(distroUrl, "://") {
distroUrl = "https://" + distroUrl
}
// generate git command
gitExe := "git"
if WINDOWS {
gitExe += ".exe"
}
// the directory to clone the distro into
destinationDir := distroDirName + PATHSEP + distroName
// make git command
destinationEmacsDir := destinationDir + PATHSEP + ".emacs.d"
gitCmd := exec.Command(gitExe, "clone", "--depth", "1", distroUrl, destinationEmacsDir)
// show git running
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
// run git
err = gitCmd.Run()
if err != nil {
return errors.New("Git error: " + err.Error())
}
return nil
}
func ensureSymlinksPresent(homeDir, distroDir string) {
// symlinks to create
symlinks := []string{
".cache",
".cargo",
".config",
".gitconfig",
".gnupg",
".local",
".mozilla",
".rustup",
}
// create symlinks
for _, linkName := range symlinks {
source := homeDir + PATHSEP + linkName
target := distroDir + PATHSEP + linkName
// skip if source does not exist
_, err := os.Stat(source)
if err != nil {
continue
}
// make symlink
if err := os.Symlink(source, target); err == nil {
fmt.Println("Created symlink " + target + " -> " + source)
}
}
}
func makeInitFile(distroDir string) {
// get environment variables
envHttpProxy := os.Getenv("http_proxy")
envHttpsProxy := os.Getenv("https_proxy")
// make string to write to init.el
initElString := ""
// append http_proxy
if envHttpProxy != "" {
envHttpProxySplitted := strings.Split(envHttpProxy, "://")
initElString += "\n (\"" +
envHttpProxySplitted[0] + "\" . \"" +
envHttpProxySplitted[1] + "\")"
}
// append https_proxy
if envHttpsProxy != "" {
envHttpsProxySplitted := strings.Split(envHttpsProxy, "://")
initElString += "\n (\"" +
envHttpsProxySplitted[0] + "\" . \"" +
envHttpsProxySplitted[1] + "\")"
}
// return if nothing to write
if initElString == "" {
return
}
// make .emacs.d directory
emacsDir := distroDir + PATHSEP + ".emacs.d"
ensureDirectoryExists(emacsDir)
// write init.el
stringToWrite := []byte("(set 'url-proxy-services '(" + initElString + "))\n\n")
initFileName := emacsDir + PATHSEP + "init.el"
if err := ioutil.WriteFile(initFileName, stringToWrite, 0644); err != nil {
fmt.Fprintln(os.Stderr, "Cannot write to init.el."+
" This could make the distribution unusable."+
" Please check the init.el file manually")
}
}