This repository was archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (82 loc) · 1.83 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"text/template"
)
const (
templateFileName = ".gb-sublime-project.template"
defaultTemplate = `{
"folders":
[
{
"path": "{{ .ProjectDir }}"
}
],
"settings": {
"GoSublime": {
"env": {
"GOPATH": "{{ .ProjectDir }}:{{ .ProjectDir }}/vendor"
}
}
}
}`
)
var (
writeDefaultTemplate = flag.Bool("write-template", false, "writes default template to ~/"+templateFileName)
)
func main() {
flag.Parse()
wd, err := os.Getwd()
if err != nil {
panic(err)
}
u, err := user.Current()
if err != nil {
panic(err)
}
fullTemplatePath := path.Join(u.HomeDir, templateFileName)
if *writeDefaultTemplate {
out, err := os.Create(fullTemplatePath)
if err != nil {
panic(fmt.Errorf("Error opening template file for writing: %s", err.Error()))
}
out.WriteString(defaultTemplate)
out.Close()
fmt.Printf("writing template to %s...\n", fullTemplatePath)
}
f, err := os.Open(fullTemplatePath)
var templateStr string
switch {
case os.IsNotExist(err):
fmt.Println("template not found in home directory; using default...")
templateStr = defaultTemplate
case err != nil:
panic(fmt.Sprintf("Error opening template file: %s", err.Error()))
default:
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
templateStr = string(b)
}
tmpl := template.Must(template.New("project").Parse(templateStr))
projectName := filepath.Base(wd)
projectFileName := projectName + ".sublime-project"
fmt.Printf("writing %s to %s...\n", projectFileName, wd)
out, err := os.Create(projectFileName)
if err != nil {
panic(err)
}
defer out.Close()
err = tmpl.Execute(out, struct{ ProjectDir string }{wd})
if err != nil {
panic(err)
}
}