-
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.
- Loading branch information
1 parent
3f6aab7
commit b5fa04c
Showing
7 changed files
with
245 additions
and
31 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 |
---|---|---|
@@ -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 | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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 @@ | ||
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) | ||
} |
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,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") | ||
}, | ||
} | ||
} |
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,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)) | ||
} |
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 |
---|---|---|
@@ -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) | ||
} | ||
} |