-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (130 loc) · 3.73 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
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
package main
import (
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
flag "github.com/spf13/pflag"
)
// This program executes the fixed actions for starting a "program of teh day"
// * create a directory named prog_of_the_day/yyyymmdd
// * create the standard files to get going (for go: main.go, go.mod, go.sum, vscode debug launch fiule)
// * Open new folder in Visual Studio Code
// Golang code for program that is created
const mainTemplate = `package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
}
`
// VS Code launch file
const launchJSON = `{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
`
const maxVers = 1000 // Maximum number of verion directories to create
type options struct {
verbose bool
version bool
dir string
}
var opt options
func init() {
flag.BoolVarP(&opt.verbose, "verbose", "v", false, "Show what's being done")
flag.BoolVarP(&opt.version, "version", "V", false, "Print version and exit")
flag.StringVarP(&opt.dir, "dir", "d", "~/dailyprog", "Base directory name where new program is created.")
}
func main() {
flag.Parse()
homeDirNative, err := os.UserHomeDir()
if err != nil {
log.Fatalln("Can't get name of home directory:", err)
}
homeDir := filepath.ToSlash(homeDirNative)
dateStr := time.Now().Format("20060102")
dailyDir := strings.Replace(opt.dir, "~", homeDir, 1)
if len(flag.Args()) > 0 {
for _, a := range flag.Args() {
dailyprogDir := filepath.Join(dailyDir, dateStr+"-"+a)
createDailyProg(dailyprogDir, a)
}
} else {
dailyprogDir := filepath.Join(dailyDir, "dailyprog-"+dateStr)
createDailyProg(dailyprogDir, "dailyprog-"+dateStr)
}
}
func createDailyProg(dailyprogDir string, progName string) error {
// Check if dir existst, and continue to append a version string until we have a non-existing dir
vers := 0
versStr := ""
for _, err := os.Stat(dailyprogDir + versStr); !os.IsNotExist(err); _, err = os.Stat(dailyprogDir + versStr) {
if vers > maxVers {
log.Fatalln("All directories " + dailyprogDir + " to " + dailyprogDir + versStr + " seem to exist, quitting.")
}
vers++
versStr = "-" + strconv.Itoa(vers)
}
dailyprogDir = dailyprogDir + versStr
vsCodeDir := filepath.Join(dailyprogDir, ".vscode")
err := os.MkdirAll(vsCodeDir, os.ModePerm) // Also creates dailyprogDir
if err != nil {
log.Fatalln("Can't create directory:", err)
}
dailyprogDirMain := filepath.Join(dailyprogDir, "main.go")
f, err := os.Create(dailyprogDirMain)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.WriteString(mainTemplate)
if err != nil {
log.Fatal(err)
}
vsCodeLaunch := filepath.Join(vsCodeDir, "launch.json")
f2, err := os.Create(vsCodeLaunch)
if err != nil {
log.Fatal(err)
}
defer f2.Close()
_, err = f2.WriteString(launchJSON)
if err != nil {
log.Fatal(err)
}
os.Chdir(dailyprogDir)
goModFn := filepath.Join(dailyprogDir, "go.mod")
_ = os.Remove(goModFn)
modName := "dummy/" + progName
// Execute go mod init dummy/dailyprog-yyyymmdd
_, err = exec.Command("go", "mod", "init", modName).Output()
if err != nil {
log.Fatal(err)
}
// Execute go mod tidy
_, err = exec.Command("go", "mod", "tidy").Output()
if err != nil {
log.Fatal(err)
}
// Open VS code
_, err = exec.Command("code", "--disable-workspace-trust", "-n", dailyprogDir, dailyprogDirMain).Output()
if err != nil {
log.Fatal(err)
}
return nil
}