forked from ironsmile/nedomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (78 loc) · 1.99 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
// This small program is used for generating module glue files from templates.
// It scans the modules main directory and outputs the template in the output file
// using as values all directories found in the modules' main directory.
package main
import (
"flag"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
var (
templateFile string
outputFile string
)
func init() {
flag.StringVar(&templateFile, "template", "", "The input template file location")
flag.StringVar(&outputFile, "output", "",
"The location of the file which will be generated from the template")
}
func main() {
flag.Parse()
if templateFile == "" {
log.Fatalln("The -template argument is required. See -h.")
}
if outputFile == "" {
log.Fatalln("The -ouput argument is required. See -h.")
}
tpl, err := template.ParseFiles(templateFile)
if err != nil {
log.Fatalln("Error parsing template file.", err)
}
outFile, err := os.Create(outputFile)
if err != nil {
log.Fatalln("Error creating output file.", err)
}
workingDirectory, err := os.Getwd()
if err != nil {
log.Fatalln("Could not get the current directory.", err)
}
var directories []string
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error in entry %s: %s\n", path, err)
return err
}
if !info.IsDir() {
return nil
}
path = strings.TrimPrefix(path, workingDirectory)
if path == "" {
return nil
}
path = strings.TrimPrefix(path, "/")
if path == "" {
return nil
}
if strings.Contains(path, "/") {
return nil
}
directories = append(directories, path)
return nil
}
err = filepath.Walk(workingDirectory, walkFunc)
if err != nil {
log.Fatalln("Error scaning the module directory", err)
}
err = tpl.Execute(outFile, directories)
if err != nil {
log.Fatalln("Error executing the template.", err)
}
cmd := exec.Command("go", "fmt", outputFile)
if err := cmd.Run(); err != nil {
log.Fatalln("Error executing go fmt.", err)
}
}