Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add man page #291

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ func useLine() string {
}

func usageFunc(cmd *cobra.Command) error {
fmt.Printf("GPT on the command line. Built for pipelines.\n\n")
fmt.Printf(
"Usage:\n %s\n\n",
useLine(),
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.20
github.com/muesli/mango-cobra v1.2.0
github.com/muesli/roff v0.1.0
github.com/muesli/termenv v0.15.2
github.com/sashabaranov/go-openai v1.24.1
github.com/spf13/cobra v1.7.0
Expand Down Expand Up @@ -51,6 +53,8 @@ require (
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/mango v0.1.0 // indirect
github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,16 @@ github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/mango v0.1.0 h1:DZQK45d2gGbql1arsYA4vfg4d7I9Hfx5rX/GCmzsAvI=
github.com/muesli/mango v0.1.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4=
github.com/muesli/mango-cobra v1.2.0 h1:DQvjzAM0PMZr85Iv9LIMaYISpTOliMEg+uMFtNbYvWg=
github.com/muesli/mango-cobra v1.2.0/go.mod h1:vMJL54QytZAJhCT13LPVDfkvCUJ5/4jNUKF/8NC2UjA=
github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe7Sg=
github.com/muesli/mango-pflag v0.1.0/go.mod h1:YEQomTxaCUp8PrbhFh10UfbhbQrM/xJ4i2PB8VTLLW0=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8=
github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig=
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
Expand Down
32 changes: 32 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/x/editor"
mcobra "github.com/muesli/mango-cobra"
"github.com/muesli/roff"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -75,6 +77,7 @@

rootCmd = &cobra.Command{
Use: "mods",
Short: "GPT on the command line. Built for pipelines.",
SilenceUsage: true,
SilenceErrors: true,
Example: randomExample(),
Expand Down Expand Up @@ -335,6 +338,25 @@
rootCmd.InitDefaultCompletionCmd()
}

if isManCmd(os.Args) {
rootCmd.AddCommand(&cobra.Command{
Use: "man",
Short: "Generates manpages",
SilenceUsage: true,
DisableFlagsInUseLine: true,
Hidden: true,
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
manPage, err := mcobra.NewManPage(1, rootCmd)
if err != nil {
return err

Check failure on line 352 in main.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func github.com/muesli/mango-cobra.NewManPage(section uint, c *github.com/spf13/cobra.Command) (*github.com/muesli/mango.ManPage, error) (wrapcheck)
}
_, err = fmt.Fprint(os.Stdout, manPage.Build(roff.NewDocument()))
return err

Check failure on line 355 in main.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func fmt.Fprint(w io.Writer, a ...any) (n int, err error) (wrapcheck)
},
})
}

if err := rootCmd.Execute(); err != nil {
handleError(err)
_ = db.Close()
Expand Down Expand Up @@ -727,6 +749,16 @@
Value(&config.Model)
}

func isManCmd(args []string) bool {
if len(args) == 2 {

Check failure on line 753 in main.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <condition> detected (mnd)
return args[1] == "man"
}
if len(args) == 3 && args[1] == "man" {
return args[2] == "-h" || args[2] == "--help"
}
return false
}

func isCompletionCmd(args []string) bool {
if len(args) <= 1 {
return false
Expand Down
21 changes: 21 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,24 @@ func TestIsCompletionCmd(t *testing.T) {
})
}
}

func TestIsManCmd(t *testing.T) {
for args, is := range map[string]bool{
"": false,
"something": false,
"something something": false,
"man is no more": false,
"mans": false,
"man foo": false,
"man": true,
"man -h": true,
"man --help": true,
} {
t.Run(args, func(t *testing.T) {
vargs := append([]string{"mods"}, strings.Fields(args)...)
if b := isManCmd(vargs); b != is {
t.Errorf("%v: expected %v, got %v", vargs, is, b)
}
})
}
}
Loading