-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement documentation command (#5)
* feat: Implement documentation command * fix: linter fixes * chore: go mod tidy
- Loading branch information
1 parent
14a3b9f
commit 9caf7ec
Showing
12 changed files
with
370 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
DocsCommandsFolder = "./docs/commands" | ||
) | ||
|
||
type DocsCommand struct { | ||
Command *cobra.Command | ||
} | ||
|
||
// NewDocsCommand creates a new cobra.Command for `docs` which generates | ||
// documentation for the application. | ||
func NewDocsCommand() *DocsCommand { | ||
dc := &DocsCommand{} | ||
dc.Command = &cobra.Command{ | ||
Use: "docs", | ||
Short: "Generate documentation", | ||
RunE: dc.Execute, | ||
Hidden: true, // Development commands are hidden. | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
return dc | ||
} | ||
|
||
func (c *DocsCommand) Execute(_ *cobra.Command, _ []string) error { | ||
if _, err := os.Stat(DocsCommandsFolder); os.IsNotExist(err) { | ||
err = os.MkdirAll(DocsCommandsFolder, 0755) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
return doc.GenMarkdownTreeCustom(rootCmd, DocsCommandsFolder, DocFilePrepender, linkHandler) | ||
} | ||
|
||
// TODO: Consider moving this to package. | ||
func DocFilePrepender(filename string) string { | ||
type FrontMatter struct { | ||
Title string `yaml:"title"` | ||
CreateTimestamp int64 `yaml:"create_timestamp"` | ||
} | ||
|
||
name := filepath.Base(filename) | ||
name = strings.TrimSuffix(name, filepath.Ext(name)) | ||
|
||
title := strings.ReplaceAll(name, "_", " ") | ||
title = strings.ReplaceAll(title, "-", " ") | ||
title = cases.Title(language.English).String(title) | ||
|
||
frontMatter := FrontMatter{ | ||
Title: title, | ||
CreateTimestamp: time.Now().Unix(), | ||
} | ||
|
||
yamlFrontMatter, err := yaml.Marshal(&frontMatter) | ||
if err != nil { | ||
log.Fatalf("error: %v", err) | ||
} | ||
|
||
return "---\n" + string(yamlFrontMatter) + "---\n" | ||
} | ||
|
||
func linkHandler(_ string) string { | ||
return "" // TODO: Implement linkHandler | ||
} |
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,30 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/glass-cms/glasscms/cmd" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFilePrepender(t *testing.T) { | ||
filename := "test_file.md" | ||
expectedTitle := "Test File" | ||
|
||
result := cmd.DocFilePrepender(filename) | ||
|
||
// Check if the result starts with the YAML front matter | ||
require.True(t, strings.HasPrefix(result, "---\n")) | ||
|
||
// Check if the title is correct | ||
require.Contains(t, result, "title: "+expectedTitle) | ||
|
||
// Check if the create timestamp is a valid Unix timestamp | ||
require.Contains(t, result, "create_timestamp: ") | ||
timestampStr := strings.Split(strings.Split(result, "create_timestamp: ")[1], "\n")[0] | ||
timestamp, err := strconv.ParseInt(timestampStr, 10, 64) | ||
require.NoError(t, err) | ||
require.Greater(t, timestamp, int64(0)) | ||
} |
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,20 @@ | ||
--- | ||
title: Glcms | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms | ||
|
||
glcms is a CMS for mangaging content based on markdown files | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for glcms | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms completion]() - Generate the autocompletion script for the specified shell | ||
* [glcms parse]() - Parses source files | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,29 @@ | ||
--- | ||
title: Glcms Completion | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms completion | ||
|
||
Generate the autocompletion script for the specified shell | ||
|
||
### Synopsis | ||
|
||
Generate the autocompletion script for glcms for the specified shell. | ||
See each sub-command's help for details on how to use the generated script. | ||
|
||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for completion | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms]() - glcms is a CMS for mangaging content based on markdown files | ||
* [glcms completion bash]() - Generate the autocompletion script for bash | ||
* [glcms completion fish]() - Generate the autocompletion script for fish | ||
* [glcms completion powershell]() - Generate the autocompletion script for powershell | ||
* [glcms completion zsh]() - Generate the autocompletion script for zsh | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,48 @@ | ||
--- | ||
title: Glcms Completion Bash | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms completion bash | ||
|
||
Generate the autocompletion script for bash | ||
|
||
### Synopsis | ||
|
||
Generate the autocompletion script for the bash shell. | ||
|
||
This script depends on the 'bash-completion' package. | ||
If it is not installed already, you can install it via your OS's package manager. | ||
|
||
To load completions in your current shell session: | ||
|
||
source <(glcms completion bash) | ||
|
||
To load completions for every new session, execute once: | ||
|
||
#### Linux: | ||
|
||
glcms completion bash > /etc/bash_completion.d/glcms | ||
|
||
#### macOS: | ||
|
||
glcms completion bash > $(brew --prefix)/etc/bash_completion.d/glcms | ||
|
||
You will need to start a new shell for this setup to take effect. | ||
|
||
|
||
``` | ||
glcms completion bash | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for bash | ||
--no-descriptions disable completion descriptions | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms completion]() - Generate the autocompletion script for the specified shell | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,39 @@ | ||
--- | ||
title: Glcms Completion Fish | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms completion fish | ||
|
||
Generate the autocompletion script for fish | ||
|
||
### Synopsis | ||
|
||
Generate the autocompletion script for the fish shell. | ||
|
||
To load completions in your current shell session: | ||
|
||
glcms completion fish | source | ||
|
||
To load completions for every new session, execute once: | ||
|
||
glcms completion fish > ~/.config/fish/completions/glcms.fish | ||
|
||
You will need to start a new shell for this setup to take effect. | ||
|
||
|
||
``` | ||
glcms completion fish [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for fish | ||
--no-descriptions disable completion descriptions | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms completion]() - Generate the autocompletion script for the specified shell | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,36 @@ | ||
--- | ||
title: Glcms Completion Powershell | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms completion powershell | ||
|
||
Generate the autocompletion script for powershell | ||
|
||
### Synopsis | ||
|
||
Generate the autocompletion script for powershell. | ||
|
||
To load completions in your current shell session: | ||
|
||
glcms completion powershell | Out-String | Invoke-Expression | ||
|
||
To load completions for every new session, add the output of the above command | ||
to your powershell profile. | ||
|
||
|
||
``` | ||
glcms completion powershell [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for powershell | ||
--no-descriptions disable completion descriptions | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms completion]() - Generate the autocompletion script for the specified shell | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,50 @@ | ||
--- | ||
title: Glcms Completion Zsh | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms completion zsh | ||
|
||
Generate the autocompletion script for zsh | ||
|
||
### Synopsis | ||
|
||
Generate the autocompletion script for the zsh shell. | ||
|
||
If shell completion is not already enabled in your environment you will need | ||
to enable it. You can execute the following once: | ||
|
||
echo "autoload -U compinit; compinit" >> ~/.zshrc | ||
|
||
To load completions in your current shell session: | ||
|
||
source <(glcms completion zsh) | ||
|
||
To load completions for every new session, execute once: | ||
|
||
#### Linux: | ||
|
||
glcms completion zsh > "${fpath[1]}/_glcms" | ||
|
||
#### macOS: | ||
|
||
glcms completion zsh > $(brew --prefix)/share/zsh/site-functions/_glcms | ||
|
||
You will need to start a new shell for this setup to take effect. | ||
|
||
|
||
``` | ||
glcms completion zsh [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for zsh | ||
--no-descriptions disable completion descriptions | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms completion]() - Generate the autocompletion script for the specified shell | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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,27 @@ | ||
--- | ||
title: Glcms Parse | ||
create_timestamp: 1715116017 | ||
--- | ||
## glcms parse | ||
|
||
Parses source files | ||
|
||
### Synopsis | ||
|
||
Parses source files and pumps them to the desired destination | ||
|
||
``` | ||
glcms parse <source> [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for parse | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [glcms]() - glcms is a CMS for mangaging content based on markdown files | ||
|
||
###### Auto generated by spf13/cobra on 7-May-2024 |
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
Oops, something went wrong.