Skip to content

Commit

Permalink
Merge pull request #68 from muandane/fix/commitcmd
Browse files Browse the repository at this point in the history
Fix: commit printing tabs
  • Loading branch information
muandane authored Aug 27, 2024
2 parents 3cd94d7 + 327bba5 commit f340614
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 421 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.0.6]: https://github.com///compare/v0.0.5..v0.0.6
[0.0.4-rc1]: https://github.com///compare/v0.0.3..v0.0.4-rc1
[0.0.3]: https://github.com///compare/v0.0.2-rc1..v0.0.3

<!-- test commit -->
<!-- generated by git-cliff -->
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ Simply run `goji` in your terminal to start the interactive commit process:
If you don't want the interactive screen you can use the flags to construct a message:

```sh
goji --type feat --scope home --message "Add home page"
goji --type feat --scope home --message "Add home page" --git-flag="--porcelain" --git-flag="--branch" --signoff --no-verify --add

-a, --add Automatically stage files that have been modified and deleted
--amend Change last commit
-n, --no-verify bypass pre-commit and commit-msg hooks
```

## Check command
Expand Down
1 change: 0 additions & 1 deletion cliff.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# git-cliff ~ configuration file
# https://git-cliff.org/docs/configuration

[changelog]
# changelog header
header = """
Expand Down
4 changes: 2 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"regexp"
"strings"

"github.com/charmbracelet/log"
"github.com/muandane/goji/pkg/config"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -22,7 +22,7 @@ var checkCmd = &cobra.Command{
fromFile, _ := cmd.Flags().GetBool("from-file")
config, err := config.ViperConfig()
if err != nil {
log.Fatalf("Error loading config file.")
log.Fatal().Msg("Error loading config file.")
}
if fromFile {
// Read commit message from file
Expand Down
172 changes: 68 additions & 104 deletions cmd/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,99 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/charmbracelet/glamour"
"github.com/mattn/go-isatty"
mcobra "github.com/muesli/mango-cobra"
"github.com/muesli/roff"
"github.com/spf13/cobra"
)

const specialChar = "%"
func init() {
rootCmd.AddCommand(manCmd)
}

var manDescription = `
Goji CLI Documentation
var manCmd = &cobra.Command{
Use: "man",
Short: "Display manual for Goji CLI",
Run: func(cmd *cobra.Command, args []string) {
manPage := `
GOJI(1) General Commands Manual GOJI(1)
# NAME
goji - A CLI tool for generating conventional commits with emojis.
NAME
goji - A CLI tool for generating conventional commits with emojis.
# SYNOPSIS
**goji** [**--help**|**-h**]
SYNOPSIS
goji [--help|-h]
**goji init** [**--global**|**--repo**]
goji init [--global|--repo]
**goji** [**--type** <type>] [**--message** <message>] [**--scope** <scope>]
goji [--type <type>] [--message <message>] [--scope <scope>]
[--no-verify|-n] [--amend] [--add|-a] [--git-flag <flag>]
# DESCRIPTION
Goji is a command-line interface designed to facilitate conventional commit messages with the addition of emojis. It can be used in interactive mode or with flags to quickly generate commit messages.
DESCRIPTION
Goji is a command-line interface designed to facilitate conventional commit messages with the addition of emojis.
It can be used in interactive mode or with flags to quickly generate commit messages.
# OPTIONS
**--help**, **-h**
* Display the help message and exit.
OPTIONS
--help, -h
Display the help message and exit.
**init**
* Initialize a Goji configuration file. Use **--global** to apply settings globally or **--repo** to apply to the current repository.
init
Initialize a Goji configuration file. Use --global to apply settings globally or --repo to apply to the current repository.
**--type** <type>
* Specify the type of commit (e.g., feat, fix, docs).
--type <type>, -t <type>
Specify the type of commit (e.g., feat, fix, docs).
**--message** <message>
* The commit message to be used.
--message <message>, -m <message>
The commit message to be used.
**--scope** <scope>
* Optional scope for the commit message.
--scope <scope>, -s <scope>
Optional scope for the commit message.
# EXAMPLES
**goji init --global**
* Initialize a global Goji configuration file in the user's home path.
--no-verify, -n
Bypass pre-commit and commit-msg hooks.
**goji init --repo**
* Add a Goji configuration file to the current repository.
--version, -v
Display version information.
**goji**
* Enter interactive mode to generate a commit message.
--amend
Amend the last commit.
**goji --type feat --message "Add login feature" --scope auth**
* Generate a commit message for adding a feature without entering interactive mode.
--add, -a
Automatically stage files that have been modified and deleted.
# ENVIRONMENT
**GOJI_CONFIG**
* The path to the Goji configuration file. If not set, defaults to the home directory or the current repository's root.
--git-flag <flag>
Additional Git flags (can be used multiple times).
`
EXAMPLES
goji init --global
Initialize a global Goji configuration file in the user's home path.
var manBugs = "See GitHub Issues: <https://github.com/muandane/goji/issues>"
var manAuthor = "Zine el abidine Moualhi <zineelabidinemoualhi@gmail.com>"
goji init --repo
Add a Goji configuration file to the current repository.
// manCmd generates the man pages.
var manCmd = &cobra.Command{
Use: "manual",
Aliases: []string{"man"},
Short: "Generate man pages",
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
if isatty.IsTerminal(os.Stdout.Fd()) {
renderer, err := glamour.NewTermRenderer(
glamour.WithStandardStyle("dark"),
)
if err != nil {
return err
}
// Render the manual in the terminal using glamour.
out, err := renderer.Render(markdownManual())
if err != nil {
return err
}
fmt.Print(out)
} else {
// Generate the man page using mango-cobra.
manPage, err := mcobra.NewManPage(1, rootCmd)
if err != nil {
return err
}

manPage = manPage.
WithLongDescription(sanitizeSpecial(manDescription)).
WithSection("Bugs", sanitizeSpecial(manBugs)).
WithSection("Author", sanitizeSpecial(manAuthor)).
WithSection("Copyright", "Copyright (C) 2024 Moualhi Zine El Abidine")

fmt.Println(manPage.Build(roff.NewDocument()))
return nil
}
return nil
},
}
goji
Enter interactive mode to generate a commit message.
// Generate markdown for the manual.
func markdownManual() string {
return fmt.Sprint(
"# MANUAL\n", sanitizeMarkdown(manDescription),
"# BUGS\n", manBugs,
"\n# AUTHOR\n", manAuthor,
)
}
goji --type feat --message "Add login feature" --scope auth
Generate a commit message for adding a feature without entering interactive mode.
func sanitizeMarkdown(input string) string {
escaped := strings.NewReplacer(
"<", "&lt;",
">", "&gt;",
"`", "&#96;", // backtick
).Replace(input)
return strings.ReplaceAll(escaped, specialChar, "`")
}
goji --type fix --message "Fix bug" --no-verify
Create a commit with the "fix" type and bypass pre-commit hooks.
func sanitizeSpecial(s string) string {
return strings.ReplaceAll(s, specialChar, "")
}
goji --type docs --message "Update README" --amend
Amend the last commit with a new message.
func init() {
rootCmd.AddCommand(manCmd)
goji --type feat --message "Add feature" --add
Stage changes and create a commit.
goji --type chore --message "Update dependencies" --git-flag "--signoff"
Add a sign-off trailer to the commit.
AUTHOR
Written by Zine el abidine Moualhi.
COPYRIGHT
Copyright (C) 2024 Moualhi Zine El Abidine. This is free software; see the source for copying conditions.
`

fmt.Println(manPage)
},
}
60 changes: 60 additions & 0 deletions cmd/man_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"bytes"
"io"
"os"
"strings"
"testing"

"github.com/spf13/cobra"
)

// captureOutput captures and returns the output of a function that prints to stdout.
func captureOutput(f func()) string {
// Save the current stdout
oldStdout := os.Stdout

// Create a pipe to capture stdout
r, w, _ := os.Pipe()
os.Stdout = w

// Execute the function
f()

// Close the writer and restore stdout
_ = w.Close()
os.Stdout = oldStdout

// Read the captured output
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
_ = r.Close()

return buf.String()
}

// TestManCmd tests the `manCmd` to ensure it outputs the manual correctly.
func TestManCmd(t *testing.T) {
// Capturing the output of the manCmd.
output := captureOutput(func() {
manCmd.Run(&cobra.Command{}, []string{})
})

// Check that the output contains key sections.
sections := []string{
"NAME",
"SYNOPSIS",
"DESCRIPTION",
"OPTIONS",
"EXAMPLES",
"AUTHOR",
"COPYRIGHT",
}

for _, section := range sections {
if !strings.Contains(output, section) {
t.Errorf("Expected section %s in man page output, but it was not found.", section)
}
}
}
Loading

0 comments on commit f340614

Please sign in to comment.