Skip to content

Commit

Permalink
feat: Implement documentation command (#5)
Browse files Browse the repository at this point in the history
* feat: Implement documentation command

* fix: linter fixes

* chore: go mod tidy
  • Loading branch information
MaikelVeen authored May 7, 2024
1 parent 14a3b9f commit 9caf7ec
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 2 deletions.
80 changes: 80 additions & 0 deletions cmd/docs.go
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
}
30 changes: 30 additions & 0 deletions cmd/docs_test.go
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))
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ func Execute() {

func init() {
rootCmd.AddCommand(NewParseCommand().Command)
rootCmd.AddCommand(NewDocsCommand().Command)
}
20 changes: 20 additions & 0 deletions docs/commands/glcms.md
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
29 changes: 29 additions & 0 deletions docs/commands/glcms_completion.md
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
48 changes: 48 additions & 0 deletions docs/commands/glcms_completion_bash.md
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
39 changes: 39 additions & 0 deletions docs/commands/glcms_completion_fish.md
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
36 changes: 36 additions & 0 deletions docs/commands/glcms_completion_powershell.md
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
50 changes: 50 additions & 0 deletions docs/commands/glcms_completion_zsh.md
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
27 changes: 27 additions & 0 deletions docs/commands/glcms_parse.md
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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ require (
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.15.0
)
Loading

0 comments on commit 9caf7ec

Please sign in to comment.