Skip to content

Commit

Permalink
feat(generate): Add manpage generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 4, 2024
1 parent 1ff90ef commit 2d52304
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
changelog-generator
/completions/
/manpages/
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 96 additions & 0 deletions internal/generate/manpages/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 2d52304

Please sign in to comment.