-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
205 lines (179 loc) · 5.91 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
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
// Goreadme command line tool and Github action
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/golang/gddo/gosrc"
"github.com/posener/goaction"
"github.com/posener/goaction/actionutil"
"github.com/posener/goaction/log"
"github.com/posener/goreadme"
"golang.org/x/oauth2"
)
var (
// Holds configuration for Goreadme invocation.
cfg goreadme.Config
// Write readme output
out io.WriteCloser = os.Stdout
// Github action variables.
//goaction:description Name of readme file.
//goaction:default README.md
path = os.Getenv("readme-file")
//goaction:description Print Goreadme debug output. Set to any non empty value for true.
_ = os.Getenv("debug")
//goaction:description Email for commit message.
//goaction:default posener@gmail.com
email = os.Getenv("email")
//goaction:description Github token for PR comments. Optional.
githubToken = os.Getenv("github-token")
// Aliases for path and githubToken.
//goaction:description An optional alias which can be used instead of 'readme-file'.
path2 = os.Getenv("README_FILE")
//goaction:description An optional alias which can be used instead of 'github-token'.
githubToken2 = os.Getenv("GITHUB_TOKEN")
)
func init() {
flag.StringVar(&cfg.ImportPath, "import-path", "", "Override package import path.")
flag.StringVar(&cfg.Title, "title", "", "Override readme title. Default is package name.")
flag.StringVar(&cfg.GoDocURL, "godoc-url", "https://pkg.go.dev", "Go Doc URL for GoDoc badge.")
flag.BoolVar(&cfg.RecursiveSubPackages, "recursive", false, "Load docs recursively.")
flag.BoolVar(&cfg.RenderTypeContent, "render-type-content", false, "If 'types' is specified, render full type content.")
flag.BoolVar(&cfg.Consts, "constants", false, "Write package constants section, and if 'types' is specified, also write per-type constants section.")
flag.BoolVar(&cfg.Vars, "variables", false, "Write package variables section, and if 'types' is specified, also write per-type variables section.")
flag.BoolVar(&cfg.Functions, "functions", false, "Write functions section.")
flag.BoolVar(&cfg.Types, "types", false, "Write types section.")
flag.BoolVar(&cfg.Factories, "factories", false, "If 'types' is specified, write section for functions returning each type.")
flag.BoolVar(&cfg.Methods, "methods", false, "If 'types' is specified, write section for methods for each type.")
flag.BoolVar(&cfg.SkipExamples, "skip-examples", false, "Skip the examples section.")
flag.BoolVar(&cfg.SkipSubPackages, "skip-sub-packages", false, "Skip the sub packages section.")
flag.BoolVar(&cfg.Badges.TravisCI, "badge-travisci", false, "Show TravisCI badge.")
flag.BoolVar(&cfg.Badges.CodeCov, "badge-codecov", false, "Show CodeCov badge.")
flag.BoolVar(&cfg.Badges.GolangCI, "badge-golangci", false, "Show GolangCI badge.")
flag.BoolVar(&cfg.Badges.GoDoc, "badge-godoc", false, "Show GoDoc badge.")
flag.BoolVar(&cfg.Badges.GoReportCard, "badge-goreportcard", false, "Show GoReportCard badge.")
flag.BoolVar(&cfg.GeneratedNotice, "generated-notice", false, "Add generated file notice (visible only in Markdown code).")
flag.BoolVar(&cfg.Credit, "credit", true, "Add credit line.")
flag.Usage = func() {
fmt.Fprint(
flag.CommandLine.Output(),
`goreadme: Create markdown file from go doc.
Usage:
goreadme [flags] [import path]
import path (optional): Create a readme file for a package from github.
Omitting import path will create a readme for the package in CWD.
Flags:
`)
flag.PrintDefaults()
}
flag.Parse()
if path == "" {
path = path2
}
if githubToken == "" {
githubToken = githubToken2
}
}
func main() {
// Steps to do only in Github Action mode.
if path != "" {
// Setup output file.
var err error
out, err = os.Create(path)
if err != nil {
log.Fatalf("Failed opening file %s: %s", path, err)
}
defer out.Close()
}
if goaction.CI {
// Fix import path if it was not overridden by the user.
if cfg.ImportPath == "" {
cfg.ImportPath = "github.com/" + goaction.Repository
}
}
ctx := context.Background()
client := http.DefaultClient
if githubToken != "" {
client = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken}))
}
gr := goreadme.New(client)
err := gr.WithConfig(cfg).Create(ctx, pkg(flag.Args()), out)
if err != nil {
log.Fatalf("Failed: %s", err)
}
if !goaction.CI {
return
}
// Runs only in Github CI mode.
diff := gitDiff()
log.Printf("Diff:\n\n%s\n", diff)
switch goaction.Event {
case goaction.EventPush:
if diff == "" {
log.Printf("No changes were made. Skipping push.")
break
}
push()
case goaction.EventPullRequest:
pr(diff)
default:
log.Fatalf("Unexpected action mode: %s", goaction.Event)
}
}
func pkg(args []string) string {
if len(args) > 0 {
return args[0]
}
path, err := filepath.Abs("./")
if err != nil {
log.Fatal(err)
}
gosrc.SetLocalDevMode(path)
return "."
}
func gitDiff() string {
// Add files to git, in case it does not exists
d, err := actionutil.GitDiff(path)
if err != nil {
log.Fatal(err)
}
if d == "" {
return ""
}
return fmt.Sprintf("Path: %s\n\n```diff\n%s\n```\n\n", path, d)
}
// Commit and push changes to upstream branch.
func push() {
err := actionutil.GitConfig("goreadme", email)
if err != nil {
log.Fatal(err)
}
err = actionutil.GitCommitPush([]string{path}, "Update readme according to godoc")
if err != nil {
log.Fatal(err)
}
}
// Post a pull request comment with the expected diff.
func pr(diff string) {
if githubToken == "" {
log.Printf("In order to add request comment, set the GITHUB_TOKEN input.")
return
}
body := "[goreadme](https://github.com/posener/goreadme) will not make any changes in this PR"
if diff != "" {
body = fmt.Sprintf(
"[goreadme](https://github.com/posener/goreadme) diff for %s file for this PR:\n\n%s",
path,
diff)
}
ctx := context.Background()
err := actionutil.PRComment(ctx, githubToken, body)
if err != nil {
log.Fatal(err)
}
}