Skip to content

Commit 013c0d4

Browse files
feat: generate a manual page automatically with cobra/doc (#2352)
Add a command line switch "--man-page" to print an automatically generated manual page to stdout. Co-authored-by: ChristopherHX <[email protected]>
1 parent f75a2d8 commit 013c0d4

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

cmd/root.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
"fmt"
78
"os"
@@ -19,6 +20,7 @@ import (
1920
gitignore "github.com/sabhiram/go-gitignore"
2021
log "github.com/sirupsen/logrus"
2122
"github.com/spf13/cobra"
23+
"github.com/spf13/cobra/doc"
2224
"gopkg.in/yaml.v3"
2325

2426
"github.com/nektos/act/pkg/artifactcache"
@@ -47,6 +49,7 @@ func Execute(ctx context.Context, version string) {
4749
rootCmd.Flags().BoolP("graph", "g", false, "draw workflows")
4850
rootCmd.Flags().StringP("job", "j", "", "run a specific job ID")
4951
rootCmd.Flags().BoolP("bug-report", "", false, "Display system information for bug report")
52+
rootCmd.Flags().BoolP("man-page", "", false, "Print a generated manual page to stdout")
5053

5154
rootCmd.Flags().StringVar(&input.remoteName, "remote-name", "origin", "git remote name that will be used to retrieve url of git repo")
5255
rootCmd.Flags().StringArrayVarP(&input.secrets, "secret", "s", []string{}, "secret to make available to actions with optional value (e.g. -s mysecret=foo or -s mysecret)")
@@ -227,6 +230,18 @@ func bugReport(ctx context.Context, version string) error {
227230
return nil
228231
}
229232

233+
func generateManPage(cmd *cobra.Command) error {
234+
header := &doc.GenManHeader{
235+
Title: "act",
236+
Section: "1",
237+
Source: fmt.Sprintf("act %s", cmd.Version),
238+
}
239+
buf := new(bytes.Buffer)
240+
cobra.CheckErr(doc.GenMan(cmd, header, buf))
241+
fmt.Print(buf.String())
242+
return nil
243+
}
244+
230245
func readArgsFile(file string, split bool) []string {
231246
args := make([]string, 0)
232247
f, err := os.Open(file)
@@ -341,6 +356,10 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
341356
if ok, _ := cmd.Flags().GetBool("bug-report"); ok {
342357
return bugReport(ctx, cmd.Version)
343358
}
359+
if ok, _ := cmd.Flags().GetBool("man-page"); ok {
360+
return generateManPage(cmd)
361+
}
362+
344363
if ret, err := container.GetSocketAndHost(input.containerDaemonSocket); err != nil {
345364
log.Warnf("Couldn't get a valid docker connection: %+v", err)
346365
} else {

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ require (
4949
github.com/cloudflare/circl v1.3.7 // indirect
5050
github.com/containerd/containerd v1.7.13 // indirect
5151
github.com/containerd/log v0.1.0 // indirect
52+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
5253
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
5354
github.com/davecgh/go-spew v1.1.1 // indirect
5455
github.com/distribution/reference v0.5.0 // indirect
@@ -80,6 +81,7 @@ require (
8081
github.com/pmezard/go-difflib v1.0.0 // indirect
8182
github.com/rivo/uniseg v0.4.7 // indirect
8283
github.com/robfig/cron/v3 v3.0.1 // indirect
84+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
8385
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
8486
github.com/skeema/knownhosts v1.2.2 // indirect
8587
github.com/stretchr/objx v0.5.2 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ github.com/containerd/containerd v1.7.13 h1:wPYKIeGMN8vaggSKuV1X0wZulpMz4CrgEsZd
3535
github.com/containerd/containerd v1.7.13/go.mod h1:zT3up6yTRfEUa6+GsITYIJNgSVL9NQ4x4h1RPzk0Wu4=
3636
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
3737
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
38+
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
3839
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3940
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4041
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
@@ -168,6 +169,7 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
168169
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
169170
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
170171
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
172+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
171173
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
172174
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
173175
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=

0 commit comments

Comments
 (0)