Skip to content

Commit

Permalink
Refactoring commands
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Nov 15, 2021
1 parent 0b6dd11 commit e5a5a7f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
15 changes: 9 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"os"

"github.com/iq2i/aergie/internal/cmd/root"
completionCmd "github.com/iq2i/aergie/internal/cmd/completion"
helpCmd "github.com/iq2i/aergie/internal/cmd/help"
userCmd "github.com/iq2i/aergie/internal/cmd/user"
versionCmd "github.com/iq2i/aergie/internal/cmd/version"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,15 +41,15 @@ func Execute(version string) {
}

rootCmd.PersistentFlags().Bool("help", false, "Show help for command")
rootCmd.SetHelpFunc(root.HelpFunc)
rootCmd.SetHelpFunc(helpCmd.Format)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

rootCmd.PersistentFlags().Bool("version", false, "Show ae version")
rootCmd.SetVersionTemplate(versionFormat(version))
rootCmd.SetVersionTemplate(versionCmd.Format(version))

rootCmd.AddCommand(newVersionCommand(version))
rootCmd.AddCommand(newCompletionCommand())
rootCmd.AddCommand(newUserCommands()...)
rootCmd.AddCommand(versionCmd.NewVersionCommand(version))
rootCmd.AddCommand(completionCmd.NewCompletionCommand())
rootCmd.AddCommand(userCmd.NewUserCommands()...)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/completion.go → internal/cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd
package completion

import (
"os"

"github.com/spf13/cobra"
)

func newCompletionCommand() *cobra.Command {
func NewCompletionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "completion [bash|zsh]",
Short: "Generate completion script",
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/root/help.go → internal/cmd/help/help.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package root
package help

import (
"fmt"
Expand All @@ -8,8 +8,8 @@ import (
"github.com/spf13/cobra"
)

// HelpFunc prints help text
func HelpFunc(cmd *cobra.Command, args []string) {
// Format prints help text
func Format(cmd *cobra.Command, args []string) {
commands := []string{}
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/user.go → internal/cmd/user/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package user

import (
"fmt"
Expand All @@ -12,7 +12,7 @@ import (
"github.com/spf13/cobra"
)

func newUserCommands() []*cobra.Command {
func NewUserCommands() []*cobra.Command {
var cmds = make([]*cobra.Command, 0)

for _, configCmd := range config.AppConfig.Commands {
Expand Down
8 changes: 4 additions & 4 deletions cmd/version.go → internal/cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package version

import (
"fmt"
Expand All @@ -7,21 +7,21 @@ import (
"github.com/spf13/cobra"
)

func newVersionCommand(version string) *cobra.Command {
func NewVersionCommand(version string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Hidden: true,

RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprint(cmd.OutOrStdout(), versionFormat(version))
fmt.Fprint(cmd.OutOrStdout(), Format(version))
return nil
},
}

return cmd
}

func versionFormat(version string) string {
func Format(version string) string {
return fmt.Sprintf("ae version %s\n%s\n", version, changelogURL(version))
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/version_test.go → internal/cmd/version/version_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd
package version

import (
"testing"
)

func TestFormat(t *testing.T) {
expects := "ae version 1.0.0\nhttps://github.com/IQ2i/aergie/releases/tag/v1.0.0\n"
if got := versionFormat("1.0.0"); got != expects {
if got := Format("1.0.0"); got != expects {
t.Errorf("versionFormat() = %q, wants %q", got, expects)
}
}
Expand Down

0 comments on commit e5a5a7f

Please sign in to comment.