-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
go-cli.txtar
191 lines (153 loc) · 4.39 KB
/
go-cli.txtar
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
author: Author full name? John Doe
proj_full: Project full name? Project X
proj_short: Project short name? {{xstringstosnakecase .proj_full}}
repo: Repo URL? github.com/{{ xstringstosnakecase .author }}/{{.proj_short}}
pkg: Application package name? app
gversion: Go version? 1.17
description: Project description?
-- finalize.sh --
#!/bin/bash
set -eu -o pipefail
# Get the directory that this script file is in
THIS_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "$THIS_DIR/{{.proj_short}}"
go mod tidy
go mod download
go fmt .
go test ./...
git init
git add .
git commit -m 'Init commit'
cd "$THIS_DIR"
rm ./finalize.sh
-- {{.proj_short}}/.github/workflows/go.yml --
name: Go
on: [ push, pull_request ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^{{.gversion}}
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: go mod download
- name: Test
run: go test -v ./...
-- {{.proj_short}}/.gitignore --
-- {{.proj_short}}/LICENSE --
MIT License
Copyright (c) {{ timenow.Format "2006" }} {{ .author }}
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-- {{.proj_short}}/README.md --
# {{ .proj_full }} [![GoDoc](https://godoc.org/{{.repo}}?status.svg)](https://godoc.org/{{.repo}}) [![Go Report Card](https://goreportcard.com/badge/{{.repo}})](https://goreportcard.com/report/{{.repo}})
{{ .description }}
## Installation
First install [Go](http://golang.org).
If you just want to install the binary to your current directory and don't care about the source code, run
```bash
GOBIN="$(pwd)" go install {{.repo}}@latest
```
## Screenshots
```
```
-- {{.proj_short}}/{{.pkg}}/{{.pkg}}.go --
package {{.pkg}}
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/carlmjohnson/flagx"
"github.com/carlmjohnson/flagx/lazyio"
"github.com/carlmjohnson/versioninfo"
)
const AppName = "{{.proj_full}}"
func CLI(args []string) error {
var app appEnv
err := app.ParseArgs(args)
if err != nil {
return err
}
if err = app.Exec(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
return err
}
func (app *appEnv) ParseArgs(args []string) error {
fl := flag.NewFlagSet(AppName, flag.ContinueOnError)
src := lazyio.FileOrURL(lazyio.StdIO, nil)
app.src = src
fl.Var(src, "src", "source file or URL")
app.Logger = log.New(io.Discard, AppName+" ", log.LstdFlags)
flagx.BoolFunc(fl, "verbose", "log debug output", func() error {
app.Logger.SetOutput(os.Stderr)
return nil
})
fl.Usage = func() {
fmt.Fprintf(fl.Output(), `{{.proj_short}} - %s
{{.description}}
Usage:
{{.proj_short}} [options]
Options:
`, versioninfo.Version)
fl.PrintDefaults()
}
if err := fl.Parse(args); err != nil {
return err
}
if err := flagx.ParseEnv(fl, AppName); err != nil {
return err
}
return nil
}
type appEnv struct {
src io.ReadCloser
*log.Logger
}
func (app *appEnv) Exec() (err error) {
app.Println("starting")
defer func() { app.Println("done") }()
n, err := io.Copy(os.Stdout, app.src)
defer func() {
e2 := app.src.Close()
if err == nil {
err = e2
}
}()
app.Printf("copied %d bytes\n", n)
return err
}
-- {{.proj_short}}/go.mod --
module {{.repo}}
go {{.gversion}}
-- {{.proj_short}}/main.go --
package main
import (
"os"
"github.com/carlmjohnson/exitcode"
"{{.repo}}/{{.pkg}}"
)
func main() {
exitcode.Exit({{.pkg}}.CLI(os.Args[1:]))
}