From 2d52304047a8442294c678adb764bee32de3c4d8 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Sat, 4 May 2024 03:08:04 -0500 Subject: [PATCH] feat(generate): Add manpage generator --- .gitignore | 1 + .goreleaser.yaml | 1 + internal/generate/manpages/main.go | 96 ++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 internal/generate/manpages/main.go diff --git a/.gitignore b/.gitignore index 5190f26..25545e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ changelog-generator /completions/ +/manpages/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 6e8ebc4..8e36235 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -2,6 +2,7 @@ project_name: changelog-generator before: hooks: - go run ./internal/generate/completions --date={{ .CommitDate }} + - go run ./internal/generate/manpages --version={{ .Version }} --date={{ .CommitDate }} builds: - env: - CGO_ENABLED=0 diff --git a/internal/generate/manpages/main.go b/internal/generate/manpages/main.go new file mode 100644 index 0000000..0be064e --- /dev/null +++ b/internal/generate/manpages/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "compress/gzip" + "io" + "io/fs" + "os" + "path/filepath" + "strings" + "time" + + "github.com/gabe565/changelog-generator/cmd" + "github.com/spf13/cobra/doc" + flag "github.com/spf13/pflag" +) + +func main() { + flags := flag.NewFlagSet("", flag.ContinueOnError) + + var version string + flags.StringVar(&version, "version", "beta", "Version") + + var dateParam string + flags.StringVar(&dateParam, "date", time.Now().Format(time.RFC3339), "Build date") + + if err := flags.Parse(os.Args); err != nil { + panic(err) + } + + if err := os.RemoveAll("manpages"); err != nil { + panic(err) + } + + if err := os.MkdirAll("manpages", 0o755); err != nil { + panic(err) + } + + rootCmd := cmd.New("", "") + rootName := rootCmd.Name() + + date, err := time.Parse(time.RFC3339, dateParam) + if err != nil { + panic(err) + } + + header := doc.GenManHeader{ + Title: strings.ToUpper(rootName), + Section: "1", + Date: &date, + Source: rootName + " " + version, + Manual: "User Commands", + } + + if err := doc.GenManTree(rootCmd, &header, "manpages"); err != nil { + panic(err) + } + + if err := filepath.Walk("manpages", func(path string, info fs.FileInfo, err error) error { + if err != nil || info.IsDir() { + return err + } + + in, err := os.Open(path) + if err != nil { + return err + } + + out, err := os.Create(path + ".gz") + if err != nil { + return err + } + gz := gzip.NewWriter(out) + + if _, err := io.Copy(gz, in); err != nil { + return err + } + + if err := in.Close(); err != nil { + return err + } + if err := os.Remove(path); err != nil { + return err + } + + if err := gz.Close(); err != nil { + return err + } + if err := out.Close(); err != nil { + return err + } + + return nil + }); err != nil { + panic(err) + } +}