-
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.
- Loading branch information
1 parent
d9a2892
commit f792e75
Showing
8 changed files
with
106 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build docs | ||
|
||
package cli | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
) | ||
|
||
var isDocsBuild = true | ||
|
||
func generateDocs(cmd *cobra.Command, filename string, title string) { | ||
cmd.DisableAutoGenTag = true | ||
|
||
// create docs directory | ||
_ = os.Mkdir("./docs", 0755) | ||
|
||
err := doc.GenMarkdownTreeCustom(cmd, "./docs", | ||
func(_ string) string { | ||
return `--- | ||
title: ` + title + ` | ||
--- | ||
` | ||
}, | ||
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) | ||
} | ||
|
||
// 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 strings.HasPrefix(c.Use, "help") { | ||
continue | ||
} | ||
generateDocs(c, filename, title+" "+c.Use) | ||
} | ||
} | ||
} |
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,11 @@ | ||
//go:build !docs | ||
|
||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var isDocsBuild = false | ||
|
||
func generateDocs(_ *cobra.Command, _ string, _ 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 |
---|---|---|
|
@@ -17,11 +17,19 @@ Support: [email protected] | |
` | ||
) | ||
|
||
var mainCommandText string | ||
|
||
func createMainCommand() *cobra.Command { | ||
if isDocsBuild { | ||
mainCommandText = "This is the main command for Dispatch CLI. Add a subcommand to make it useful." | ||
} else { | ||
mainCommandText = DispatchCmdLong | ||
} | ||
cmd := &cobra.Command{ | ||
Version: version(), | ||
Use: "dispatch", | ||
Long: DispatchCmdLong, | ||
Long: mainCommandText, | ||
Short: "Main command for Dispatch CLI", | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return loadEnvFromFile(DotEnvFilePath) | ||
}, | ||
|
@@ -42,6 +50,16 @@ func createMainCommand() *cobra.Command { | |
Title: "Dispatch Commands:", | ||
}) | ||
|
||
// create a vector of commands | ||
commands := []*cobra.Command{ | ||
Check failure on line 54 in cli/main.go GitHub Actions / lint
Check failure on line 54 in cli/main.go GitHub Actions / lint
|
||
loginCommand(), | ||
initCommand(), | ||
switchCommand(DispatchConfigPath), | ||
verificationCommand(), | ||
runCommand(), | ||
versionCommand(), | ||
} | ||
|
||
// Passing the global variables to the commands make testing in parallel possible. | ||
cmd.AddCommand(loginCommand()) | ||
cmd.AddCommand(initCommand()) | ||
|
@@ -50,6 +68,14 @@ func createMainCommand() *cobra.Command { | |
cmd.AddCommand(runCommand()) | ||
cmd.AddCommand(versionCommand()) | ||
|
||
// Generate markdown documentation | ||
generateDocs(loginCommand(), "dispatch_login.md", "dispatch login") | ||
generateDocs(initCommand(), "dispatch_init.md", "dispatch init") | ||
generateDocs(switchCommand(DispatchConfigPath), "dispatch_switch.md", "dispatch switch") | ||
generateDocs(verificationCommand(), "dispatch_verification.md", "dispatch verification") | ||
generateDocs(runCommand(), "dispatch_run.md", "dispatch run") | ||
generateDocs(versionCommand(), "dispatch_version.md", "dispatch version") | ||
|
||
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