Skip to content

Commit

Permalink
add: more commands examples
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolosLykos committed Sep 6, 2022
1 parent 3f6aab7 commit b5fa04c
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 31 deletions.
60 changes: 50 additions & 10 deletions cmd/date.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
package cmd

import (
"errors"
"fmt"
"time"

"github.com/spf13/cobra"
)

var DateCmd = &cobra.Command{
Use: "date",
Short: "Print the current date.",
Run: func(cmd *cobra.Command, args []string) {
format, _ := cmd.Flags().GetString("format")
fmt.Fprint(cmd.OutOrStdout(), time.Now().Format(format))
},
var availableFormats = []string{
"02 01 06",
"02 01 06 15:04:05",
"02 Jan 06",
"02 Jan 06 15:04:05",
"Mon Jan 06",
"Mon Jan 06 15:04:05",
"02 01 2006",
"02 01 2006 15:04:05 MST",
}

func init() {
RootCmd.AddCommand(DateCmd)
var ErrBadFormat = errors.New("wrong date format")

DateCmd.Flags().StringP("format", "f", "02 Jan 06", "specify a custom date format")
func NewDateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "date",
Short: "Print the current date.",
RunE: run,
}

cmd.Flags().StringP("format", "f", "02 Jan 06", "specify a custom date format")

return cmd
}

func run(cmd *cobra.Command, _ []string) error {
format, err := cmd.Flags().GetString("format")
if err != nil {
return err
}

if !checkFormat(format) {
return ErrBadFormat
}

fmt.Fprintf(cmd.OutOrStdout(), "%s\n", time.Now().Format(format))

return nil
}

func checkFormat(format string) bool {
found := false

for _, f := range availableFormats {
if f == format {
found = true

break
}
}

return found
}
93 changes: 83 additions & 10 deletions cmd/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd_test

import (
"bytes"
"io/ioutil"
"testing"
"time"

Expand All @@ -10,17 +11,89 @@ import (
"github.com/KarolosLykos/cli-template/cmd"
)

func TestDateCmd(t *testing.T) {
root := cmd.RootCmd
func TestDateCommand(t *testing.T) {
testCases := []struct {
name string
args []string
expected string
err bool
}{
{
name: "default",
args: nil,
expected: time.Now().Format("02 Jan 06") + "\n",
err: false,
},
{
name: "02 01 06",
args: []string{"-f", "02 Jan 06"},
expected: time.Now().Format("02 Jan 06") + "\n",
err: false,
},
{
name: "02 01 06 15:04:05",
args: []string{"-f", "02 01 06 15:04:05"},
expected: time.Now().Format("02 01 06 15:04:05") + "\n",
err: false,
},
{
name: "02 Jan 06",
args: []string{"-f", "02 Jan 06"},
expected: time.Now().Format("02 Jan 06") + "\n",
err: false,
},
{
name: "02 Jan 06 15:04:05",
args: []string{"-f", "02 Jan 06 15:04:05"},
expected: time.Now().Format("02 Jan 06 15:04:05") + "\n",
err: false,
},
{
name: "Mon Jan 06",
args: []string{"-f", "Mon Jan 06"},
expected: time.Now().Format("Mon Jan 06") + "\n",
err: false,
},
{
name: "Mon Jan 06 15:04:05",
args: []string{"-f", "Mon Jan 06 15:04:05"},
expected: time.Now().Format("Mon Jan 06 15:04:05") + "\n",
err: false,
},
{
name: "02 01 2006",
args: []string{"-f", "02 01 2006"},
expected: time.Now().Format("02 01 2006") + "\n",
err: false,
},
{
name: "02 01 2006 15:04:05 MST",
args: []string{"-f", "02 01 2006 15:04:05 MST"},
expected: time.Now().Format("02 01 2006 15:04:05 MST") + "\n",
err: false,
},
{
name: "Wrong format",
args: []string{"-f", "Something wrong"},
expected: "",
err: true,
},
}

buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetErr(buf)
root.SetArgs([]string{"date"})
for _, tc := range testCases {
command := cmd.NewDateCmd()
b := bytes.NewBufferString("")

if err := root.Execute(); err != nil {
panic(err)
}
command.SetArgs(tc.args)
command.SetOut(b)

assert.Equal(t, time.Now().Format("02 Jan 06"), buf.String())
err := command.Execute()
out, _ := ioutil.ReadAll(b)

if tc.err {
assert.Error(t, err, tc.name)
} else {
assert.Equal(t, tc.expected, string(out), tc.name)
}
}
}
36 changes: 26 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// RootCmd command to print the date.
var RootCmd = &cobra.Command{
Use: "cli-template",
Short: "This cli template shows nothing",
Long: `This is a template CLI application, which can be used as a boilerplate for awesome CLI tools written in Go.`,
Example: `cli-template`,
Version: "v0.0.1", // <--VERSION-->
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cli-template",
Short: "This cli template shows nothing",
Long: `This is a template CLI application, which can be used as a boilerplate for awesome CLI tools written in Go.`,
Example: `cli-template`,
Version: "v0.0.1", // <--VERSION-->
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(cmd.UsageString())

return nil
},
}

cmd.AddCommand(NewDateCmd()) // version subcommand
cmd.AddCommand(NewVersionCmd()) // version subcommand

return cmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// Execute invokes the command.
func Execute() error {
return RootCmd.Execute()
if err := NewRootCmd().Execute(); err != nil {
return fmt.Errorf("error executing root command: %w", err)
}

return nil
}
29 changes: 29 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd_test

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/KarolosLykos/cli-template/cmd"
)

func TestRootCommandOutput(t *testing.T) {
command := cmd.NewRootCmd()
b := bytes.NewBufferString("")

command.SetArgs([]string{"-h"})
command.SetOut(b)

cmdErr := command.Execute()
require.NoError(t, cmdErr)

out, err := ioutil.ReadAll(b)
require.NoError(t, err)

assert.Equal(t, "This is a template CLI application, which can be used as a boilerplate for awesome CLI tools written in Go.\n\n"+command.UsageString(), string(out))
assert.Nil(t, cmdErr)
}
19 changes: 19 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func NewVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Displays version",
Args: cobra.NoArgs,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", "v0.0.1")
},
}
}
31 changes: 31 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd_test

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/KarolosLykos/cli-template/cmd"
)

func TestVersionCommand(t *testing.T) {
version := "v0.0.1"
command := cmd.NewRootCmd()

b := bytes.NewBufferString("")

command.SetArgs([]string{"version"})
command.SetOut(b)

_, err := command.ExecuteC()
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
require.NoError(t, err)

assert.Equal(t, fmt.Sprintln(version), string(out))
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package main

import (
"fmt"
"os"

"github.com/KarolosLykos/cli-template/cmd"
)

func main() {
_ = cmd.Execute()
if err := cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}

0 comments on commit b5fa04c

Please sign in to comment.