This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
217 lines (185 loc) · 5 KB
/
generate.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
//
// generator.go - provides a quick and dirty skeleton for cli based apps.
//
package cli
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
var (
commentBlock = `//
// %s %s
//
// %s
`
importsBlock = `
import (
"fmt"
"strings"
"io/ioutil"
"os"
// Caltech Library Packages
"github.com/caltechlibrary/cli"
)
`
varBlock = `
var (
synopsis = %s
description = %s
examples = %s
bugs = %s
license = %s
// Standard Options
showHelp bool
showLicense bool
showVersion bool
showExamples bool
inputFName string
outputFName string
newLine bool
quiet bool
prettyPrint bool
generateMarkdown bool
generateManPage bool
// Application Options
)
`
mainBlock = `
func main() {
//FIXME: Replace with your base package .Version attribute
app := cli.NewCli("v0.0.0")
//FIXME: if you need the app name then...
//appName := app.AppName()
// Set explicit parameter list for USAGE
//app.SetParams(ARRAY_OF_OPTIONAL_AND_REQUIRE_PARAMETERS...)
// Add Help Docs
app.SectionNo = 1 // The manual page section number
app.AddHelp("synopsis", []byte(synopsis))
app.AddHelp("description", []byte(description))
app.AddHelp("examples", []byte(examples))
app.AddHelp("bugs", []byte(bugs))
// Standard Options
app.BoolVar(&showHelp, "h,help", false, "display help")
app.BoolVar(&showLicense, "l,license", false, "display license")
app.BoolVar(&showVersion, "v,version", false, "display version")
app.BoolVar(&showExamples, "examples", false, "display examples")
app.StringVar(&inputFName, "i,input", "", "input file name")
app.StringVar(&outputFName, "o,output", "", "output file name")
app.BoolVar(&newLine, "nl,newline", false, "if true add a trailing newline")
app.BoolVar(&quiet, "quiet", false, "suppress error messages")
app.BoolVar(&prettyPrint, "p,pretty", false, "pretty print output")
app.BoolVar(&generateMarkdown, "generate-markdown", false, "generate Markdown documentation")
app.BoolVar(&generateManPage, "generate-manpage", false, "output manpage markup")
// Application Options
//FIXME: Add any application specific options
// Application Verbs
//FIXME: If the application is verb based add your verbs here e.g.
//app.VerbsRequired = true
//verbObj := app.NewVerb(STRING_VERB, STRING_DESCRIPTION, FUNC_POINTER)
//verbObj.StringVar(&inputFName, "i,input", "", "input filename")
// ...
// We're ready to process args
app.Parse()
args := app.Args()
// Setup IO
var err error
app.Eout = os.Stderr
app.In, err = cli.Open(inputFName, os.Stdin)
cli.ExitOnError(app.Eout, err, quiet)
defer cli.CloseFile(inputFName, app.In)
app.Out, err = cli.Create(outputFName, os.Stdout)
cli.ExitOnError(app.Eout, err, quiet)
defer cli.CloseFile(outputFName, app.Out)
// Handle options
if generateMarkdown {
app.GenerateMarkdown(app.Out)
os.Exit(0)
}
if generateManPage {
app.GenerateManPage(app.Out)
os.Exit(0)
}
if showHelp || showExamples {
if len(args) > 0 {
fmt.Fprintf(app.Out, app.Help(args...))
} else {
app.Usage(app.Out)
}
os.Exit(0)
}
if showLicense {
fmt.Fprintln(app.Out, app.License())
os.Exit(0)
}
if showVersion {
fmt.Fprintln(app.Out, app.Version())
os.Exit(0)
}
// Application Logic
//FIXME: running code, e.g. os.Exit(app.Run(args))
if newLine {
fmt.Fprintln(app.Out, "")
}
}
`
)
func backQuote(s string) string {
return "`" + s + "\n`"
}
// Generate creates main.go source code
func Generate(appName, synopsis, author, descriptionFName, examplesFName, bugsFName, licenseFName string) []byte {
var (
description []byte
examples []byte
license []byte
bugs []byte
err error
)
if len(descriptionFName) > 0 {
description, err = ioutil.ReadFile(descriptionFName)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: skipping description %q, %s", descriptionFName, err)
}
}
if len(examplesFName) > 0 {
examples, err = ioutil.ReadFile(examplesFName)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: skipping examples %q, %s", examplesFName, err)
}
}
if len(bugsFName) > 0 {
bugs, err = ioutil.ReadFile(bugsFName)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: skipping bugs %q, %s", bugsFName, err)
}
}
if len(licenseFName) > 0 {
license, err = ioutil.ReadFile(licenseFName)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: skipping license %q, %s", licenseFName, err)
}
}
blocks := []string{}
// Setup the initial comment block
blocks = append(blocks, fmt.Sprintf(commentBlock, appName, string(synopsis), author))
// Add the license
blocks = append(blocks, fmt.Sprintf("//%s\n", strings.Replace(string(license), "\n", "\n// ", -1)))
// Add Package name
blocks = append(blocks, "package main")
// Add Standard Imports
blocks = append(blocks, importsBlock)
// Add Global vars
blocks = append(blocks, fmt.Sprintf(varBlock,
backQuote(string(synopsis)),
backQuote(string(description)),
backQuote(string(examples)),
backQuote(string(bugs)),
backQuote(string(license)),
))
// Add Main
blocks = append(blocks, mainBlock)
// Convert to byte array and return
return []byte(strings.Join(blocks, ""))
}