-
Notifications
You must be signed in to change notification settings - Fork 1
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 #84 from dispatchrun/gen_docs
Create CLI docs
- Loading branch information
Showing
9 changed files
with
134 additions
and
14 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 |
---|---|---|
|
@@ -34,3 +34,5 @@ goreleaser/ | |
.netrc | ||
|
||
.dispatch | ||
|
||
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
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
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,70 @@ | ||
//go:build docs | ||
|
||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
) | ||
|
||
const DispatchCmdLong = "This is the main command for Dispatch CLI. Add a subcommand to make it useful." | ||
|
||
const RunExampleText = "```\ndispatch run [options] -- <command>\n```" | ||
|
||
func generateDocs(cmd *cobra.Command, title string) { | ||
cmd.DisableAutoGenTag = true | ||
|
||
// create docs directory | ||
_ = os.Mkdir("./docs", 0755) | ||
|
||
out := new(bytes.Buffer) | ||
|
||
err := doc.GenMarkdownCustom(cmd, out, func(name string) string { | ||
// err := doc.GenMarkdownCustom(cmd, out, func(name string) string { | ||
base := strings.TrimSuffix(name, path.Ext(name)) | ||
return "/cli/" + strings.ToLower(base) + "/" | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Define the text to be replaced and the replacement text | ||
oldText := []byte("## " + title) | ||
newText := []byte("---\ntitle: " + title + "\n---") | ||
|
||
// Perform the replacement on the buffer's content | ||
updatedContent := bytes.Replace(out.Bytes(), oldText, newText, 1) | ||
|
||
// Reset the buffer and write the updated content back to it | ||
out.Reset() | ||
out.Write(updatedContent) | ||
|
||
// write markdown to file | ||
file, err := os.Create("./docs/" + strings.ReplaceAll(title, " ", "_") + ".md") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = file.Write(out.Bytes()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer file.Close() | ||
|
||
// if command has subcommands, generate markdown for each subcommand | ||
if cmd.HasSubCommands() { | ||
for _, c := range cmd.Commands() { | ||
// if c.Use starts with "help", skip it | ||
if c.Name() == "help" { | ||
continue | ||
} | ||
generateDocs(c, title+" "+c.Name()) | ||
} | ||
} | ||
} |
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,20 @@ | ||
//go:build !docs | ||
|
||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
const DispatchCmdLong = `Welcome to Dispatch! | ||
To get started, use the login command to authenticate with Dispatch or create an account. | ||
Documentation: https://docs.dispatch.run | ||
Discord: https://dispatch.run/discord | ||
Support: [email protected] | ||
` | ||
|
||
const RunExampleText = " dispatch run [options] -- <command>" | ||
|
||
func generateDocs(_ *cobra.Command, _ string) { | ||
// do nothing if the build tag "docs" is not set | ||
} |
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 |
---|---|---|
|
@@ -6,22 +6,12 @@ import ( | |
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
DispatchCmdLong = `Welcome to Dispatch! | ||
To get started, use the login command to authenticate with Dispatch or create an account. | ||
Documentation: https://docs.dispatch.run | ||
Discord: https://dispatch.run/discord | ||
Support: [email protected] | ||
` | ||
) | ||
|
||
func createMainCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Version: version(), | ||
Use: "dispatch", | ||
Long: DispatchCmdLong, | ||
Short: "Main command for Dispatch CLI", | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return loadEnvFromFile(DotEnvFilePath) | ||
}, | ||
|
@@ -50,6 +40,9 @@ func createMainCommand() *cobra.Command { | |
cmd.AddCommand(runCommand()) | ||
cmd.AddCommand(versionCommand()) | ||
|
||
// Generate markdown documentation | ||
generateDocs(cmd, "dispatch") | ||
|
||
return cmd | ||
} | ||
|
||
|
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
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
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