-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from tobigiwa/tobytobias#new_feature
New feature (docs command)
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ ff/ff | |
dist/ | ||
*.iml | ||
.idea/ | ||
docs/command_docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright © 2023 Giwa Oluwatobi <[email protected]> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
) | ||
|
||
// docsCmd represents the docs command | ||
var docsCmd = &cobra.Command{ | ||
Use: "docs [dir]", | ||
Short: "Generate markdown documentation for all command", | ||
Args: cobra.MaximumNArgs(1), | ||
Long: `Generate markdown documentation for the entire command tree. | ||
The command takes an optional argument specifying directory to put the | ||
generated documentation, default is "{cwd}/docs/command_docs/"`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var path string | ||
|
||
if len(args) == 0 { | ||
currentWoringDir, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
path = fmt.Sprintf("%s/docs/command_docs", currentWoringDir) | ||
if err := os.MkdirAll(path, 0755); err != nil { | ||
return err | ||
} | ||
} else { | ||
path = args[0] | ||
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) || err != nil { | ||
err = fmt.Errorf("path you supplied for documentation is invalid: %v", err) | ||
return err | ||
} | ||
} | ||
|
||
err := doc.GenMarkdownTree(rootCmd, path) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(docsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters