This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
155 lines (116 loc) · 3.7 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
// Copyright 2016 Huan Du. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"flag"
"os"
"path/filepath"
"regexp"
"github.com/huandu/goroutine/version"
)
var (
flagGoSrc string
flagOutput string
flagImportPath string
reImportPath = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*(/[a-zA-Z0-9_]+)*$`)
)
type Context struct {
GoSrc string // Path to go src root directory.
GoPackage string // Path to package src in go src directory.
Version version.Version // Version number.
Output string // Path to output directory.
ImportPath string // The prefix of import path for output directory.
}
func init() {
flag.StringVar(&flagGoSrc, "go-src", "", "Path to go src directory.")
flag.StringVar(&flagOutput, "output", "", "Output path. Default is current directory.")
flag.StringVar(&flagImportPath, "import-path", "", "The prefix of import path for output directory.")
}
func validateGoSrc(context *Context) {
if flagGoSrc == "" {
logFatalf("Flag -go-src must be set.")
}
src, err := filepath.Abs(flagGoSrc)
if err != nil {
logFatalf("Fail to get absolute path of go src. [go-src:%v]", flagGoSrc)
}
info, err := os.Stat(src)
if err != nil {
logFatalf("Value of flag -go-src must be a valid directory name. [go-src:%v]", flagGoSrc)
}
if !info.Mode().IsDir() {
logFatalf("Value of flag -go-src must be a directory. [go-src:%v]", flagGoSrc)
}
srcsrc := filepath.Join(src, "src")
info, err = os.Stat(srcsrc)
if err != nil {
logFatalf("Fail to find `src` in go src directory. [go-src:%v]", flagGoSrc)
}
if !info.Mode().IsDir() {
logFatalf("The `src` in go src must be a directory. [go-src:%v]", flagGoSrc)
}
versionPath := filepath.Join(src, "VERSION")
versionFile, err := os.Open(versionPath)
if err != nil {
logFatalf("Fail to read VERSION in go src directory. Are you sure -go-src points to a valid go src directory? [go-src:%v]", flagGoSrc)
}
defer versionFile.Close()
versionBuf := &bytes.Buffer{}
versionBuf.ReadFrom(versionFile)
if versionBuf.Len() == 0 {
logFatalf("VERSION in go src is empty. [go-src:%v]", flagGoSrc)
}
version, err := version.Parse(versionBuf.String())
if err != nil {
logFatalf("VERSION file content is not valid. [go-src:%v] [err:%v] [content:%v]", flagGoSrc, err, versionBuf.String())
}
context.GoSrc = src
context.GoPackage = srcsrc
context.Version = version
}
func validateOutput(context *Context) {
var err error
if flagOutput == "" {
flagOutput, err = os.Getwd()
if err != nil {
logFatalf("Value of flag -output is empty and current directory is not available.")
}
}
output, err := filepath.Abs(flagOutput)
if err != nil {
logFatalf("Fail to get absolute path of output. [output:%v]", flagOutput)
}
info, err := os.Stat(output)
if err != nil {
logFatalf("Value of flag -output must be a valid directory name. [output:%v]", flagOutput)
}
if !info.Mode().IsDir() {
logFatalf("Value of flag -output must be a directory. [output:%v]", flagOutput)
}
context.Output = output
}
func validateImportPath(context *Context) {
if flagImportPath == "" {
logFatalf("Flag -import-path must be set.")
}
if !reImportPath.MatchString(flagImportPath) {
logFatalf("Value of flag -import-path must be a valid import path. [import-path:%v]", flagImportPath)
}
context.ImportPath = flagImportPath
}
func main() {
defer func() {
if err := recover(); err != nil {
logErrorf("%v", err)
os.Exit(1)
}
}()
flag.Parse()
context := &Context{}
validateGoSrc(context)
validateOutput(context)
validateImportPath(context)
NewGenerator(context, &RuntimeHacker{}).Parse()
}