From 5d43e71c20442cb365ac6beaaf904b7935c4b1b5 Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Fri, 17 May 2019 09:39:48 +0900 Subject: [PATCH 1/3] ref #56 change cli library to ogier/pflag. --- Gopkg.lock | 9 +++++++ Gopkg.toml | 4 ++++ add/add_cmd.go | 12 ++++------ add/add_test.go | 10 ++++---- clone/clone_cmd.go | 15 +++++------- clone/clone_test.go | 6 ++--- export/export_cmd.go | 2 +- export/import_cmd.go | 5 ++-- export/import_test.go | 4 ++-- fetch/fetch_all_cmd.go | 6 ++--- fetch/fetch_cmd.go | 7 +++--- group/group_cmd.go | 45 +++++++++++++---------------------- group/group_test.go | 12 +++++----- list/list_cmd.go | 20 ++++++---------- move/move_cmd.go | 5 ++-- move/move_test.go | 2 +- path/path_cmd.go | 8 +++---- remove/remove_cmd.go | 11 ++++----- repository/repository_cmd.go | 23 +++++++----------- repository/repository_test.go | 8 +++---- status/status_cmd.go | 16 +++++-------- 21 files changed, 102 insertions(+), 128 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index b03b1b5..1d5b910 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -120,6 +120,14 @@ revision = "af06845cf3004701891bf4fdb884bfe4920b3727" version = "v1.1.0" +[[projects]] + digest = "1:8fd3a15613c7e70cceff3aa03dd57560dba87c4868864e397d5eb2f14addd3f5" + name = "github.com/ogier/pflag" + packages = ["."] + pruneopts = "UT" + revision = "32a05c62658bd1d7c7e75cbc8195de5d585fde0f" + version = "v0.0.1" + [[projects]] digest = "1:b2ee62e09bec113cf086d2ce0769efcc7bf79481aba8373fd8f7884e94df3462" name = "github.com/pelletier/go-buffruneio" @@ -295,6 +303,7 @@ "github.com/gookit/color", "github.com/mitchellh/cli", "github.com/mitchellh/go-homedir", + "github.com/ogier/pflag", "gopkg.in/src-d/go-git.v4", "gopkg.in/src-d/go-git.v4/plumbing", ] diff --git a/Gopkg.toml b/Gopkg.toml index 9fc2dfd..931f422 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -48,3 +48,7 @@ [prune] go-tests = true unused-packages = true + +[[constraint]] + name = "github.com/ogier/pflag" + version = "0.0.1" diff --git a/add/add_cmd.go b/add/add_cmd.go index d0ced2a..1898a4c 100644 --- a/add/add_cmd.go +++ b/add/add_cmd.go @@ -1,10 +1,10 @@ package add import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -28,8 +28,8 @@ Help function shows the help message. func (add *Command) Help() string { return `rrh add [OPTIONS] OPTIONS - -g, --group add repository to RRH database. - -r, --repository-id specified repository id of the given repository path. + -g, --group= add repository to RRH database. + -r, --repository-id= specified repository id of the given repository path. Specifying this option fails with multiple arguments. ARGUMENTS REPOSITORY_PATHS the local path list of the git repositories.` @@ -92,10 +92,8 @@ func (add *Command) buildFlagSet(config *common.Config) (*flag.FlagSet, *options var defaultGroup = config.GetValue(common.RrhDefaultGroupName) flags := flag.NewFlagSet("add", flag.ContinueOnError) flags.Usage = func() { fmt.Println(add.Help()) } - flags.StringVar(&opt.group, "g", defaultGroup, "target group") - flags.StringVar(&opt.group, "group", defaultGroup, "target group") - flags.StringVar(&opt.repoID, "r", "", "specifying repository id") - flags.StringVar(&opt.repoID, "repository-id", "", "specifying repository id") + flags.StringVarP(&opt.group, "group", "g", defaultGroup, "target group") + flags.StringVarP(&opt.repoID, "repository-id", "r", "", "specifying repository id") return flags, &opt } diff --git a/add/add_test.go b/add/add_test.go index 37e1451..84c28cb 100644 --- a/add/add_test.go +++ b/add/add_test.go @@ -24,8 +24,8 @@ func TestHelpAndSynopsis(t *testing.T) { } if command.Help() != `rrh add [OPTIONS] OPTIONS - -g, --group add repository to RRH database. - -r, --repository-id specified repository id of the given repository path. + -g, --group= add repository to RRH database. + -r, --repository-id= specified repository id of the given repository path. Specifying this option fails with multiple arguments. ARGUMENTS REPOSITORY_PATHS the local path list of the git repositories.` { @@ -54,7 +54,7 @@ func TestAdd(t *testing.T) { rCheckers []repositoryChecker relCheckers []relationChecker }{ - {[]string{"--group", "group2", "../testdata/helloworld"}, 0, + {[]string{"--group=group2", "../testdata/helloworld"}, 0, []groupChecker{{"group2", true}}, []repositoryChecker{{"helloworld", true}}, []relationChecker{{"group2", "helloworld", true}}, @@ -74,12 +74,12 @@ func TestAdd(t *testing.T) { []repositoryChecker{{"helloworld", true}}, []relationChecker{{"no-group", "helloworld", true}}, }, - {[]string{"--repository-id", "hw", "../testdata/other/helloworld"}, 0, + {[]string{"--repository-id=hw", "../testdata/other/helloworld"}, 0, []groupChecker{}, []repositoryChecker{{"hw", true}}, []relationChecker{{"no-group", "hw", true}}, }, - {[]string{"--repository-id", "fails", "../testdata/other/helloworld", "../testdata/fibonacci"}, 0, + {[]string{"--repository-id=fails", "../testdata/other/helloworld", "../testdata/fibonacci"}, 0, []groupChecker{}, []repositoryChecker{}, []relationChecker{}, diff --git a/clone/clone_cmd.go b/clone/clone_cmd.go index f5d6505..23daf68 100644 --- a/clone/clone_cmd.go +++ b/clone/clone_cmd.go @@ -1,10 +1,10 @@ package clone import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -34,8 +34,8 @@ Help function shows the help message. func (clone *Command) Help() string { return `rrh clone [OPTIONS] OPTIONS - -g, --group print managed repositories categorized in the group. - -d, --dest specify the destination. + -g, --group= print managed repositories categorized in the group. + -d, --dest= specify the destination. -v, --verbose verbose mode. ARGUMENTS REMOTE_REPOS repository urls` @@ -109,12 +109,9 @@ func (clone *Command) buildFlagSets(config *common.Config) (*flag.FlagSet, *opti var options = options{defaultGroup, ".", false} flags := flag.NewFlagSet("clone", flag.ContinueOnError) flags.Usage = func() { fmt.Println(clone.Help()) } - flags.StringVar(&options.group, "g", defaultGroup, "belonging group") - flags.StringVar(&options.group, "group", defaultGroup, "belonging group") - flags.StringVar(&options.dest, "d", destination, "destination") - flags.StringVar(&options.dest, "dest", destination, "destination") - flags.BoolVar(&options.verbose, "v", false, "verbose mode") - flags.BoolVar(&options.verbose, "verbose", false, "verbose mode") + flags.StringVarP(&options.group, "group", "g", defaultGroup, "belonging group") + flags.StringVarP(&options.dest, "dest", "d", destination, "destination") + flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose mode") return flags, &options } diff --git a/clone/clone_test.go b/clone/clone_test.go index 249b7a7..36fe928 100644 --- a/clone/clone_test.go +++ b/clone/clone_test.go @@ -94,7 +94,7 @@ func TestCommand_Run(t *testing.T) { func TestCommand_SpecifyingId(t *testing.T) { var dbFile = common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() { var clone, _ = CommandFactory() - clone.Run([]string{"-d", "../testdata/newid", "../testdata/helloworld"}) + clone.Run([]string{"--dest=../testdata/newid", "../testdata/helloworld"}) defer cleanup([]string{"../testdata/newid"}) var config = common.OpenConfig() @@ -143,8 +143,8 @@ func TestCloneNotGitRepository(t *testing.T) { func TestHelpAndSynopsis(t *testing.T) { var helpMessage = `rrh clone [OPTIONS] OPTIONS - -g, --group print managed repositories categorized in the group. - -d, --dest specify the destination. + -g, --group= print managed repositories categorized in the group. + -d, --dest= specify the destination. -v, --verbose verbose mode. ARGUMENTS REMOTE_REPOS repository urls` diff --git a/export/export_cmd.go b/export/export_cmd.go index b927d8f..18a65e1 100644 --- a/export/export_cmd.go +++ b/export/export_cmd.go @@ -3,7 +3,6 @@ package export import ( "bytes" "encoding/json" - "flag" "fmt" "os" "path/filepath" @@ -11,6 +10,7 @@ import ( "github.com/mitchellh/cli" "github.com/mitchellh/go-homedir" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) diff --git a/export/import_cmd.go b/export/import_cmd.go index f393480..4aae52e 100644 --- a/export/import_cmd.go +++ b/export/import_cmd.go @@ -1,10 +1,10 @@ package export import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -82,8 +82,7 @@ func buildFlagSet(command *ImportCommand) (*flag.FlagSet, *importOptions) { flags.Usage = func() { fmt.Println(command.Help()) } flags.BoolVar(&options.overwrite, "overwrite", false, "overwrite mode") flags.BoolVar(&options.autoClone, "auto-clone", false, "auto clone mode") - flags.BoolVar(&options.verbose, "v", false, "verbose mode") - flags.BoolVar(&options.verbose, "verbose", false, "verbose mode") + flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose mode") return flags, &options } diff --git a/export/import_test.go b/export/import_test.go index 942031d..b982d00 100644 --- a/export/import_test.go +++ b/export/import_test.go @@ -89,14 +89,14 @@ func TestParsingFailOfArgs(t *testing.T) { }{ {[]string{}, "too few arguments"}, {[]string{"a", "b", "c"}, "too many arguments: [a b c]"}, - {[]string{"-unknown-option"}, `rrh import [OPTIONS] + {[]string{"--unknown-option"}, `rrh import [OPTIONS] OPTIONS --auto-clone clone the repository, if paths do not exist. --overwrite replace the local RRH database to the given database. -v, --verbose verbose mode. ARGUMENTS DATABASE_JSON the exported RRH database. -flag provided but not defined: -unknown-option`}, +unknown flag: --unknown-option`}, } for _, testcase := range testcases { diff --git a/fetch/fetch_all_cmd.go b/fetch/fetch_all_cmd.go index 068c111..aa28855 100644 --- a/fetch/fetch_all_cmd.go +++ b/fetch/fetch_all_cmd.go @@ -1,10 +1,10 @@ package fetch import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -26,7 +26,7 @@ Help returns the help message. func (fetchAll *AllCommand) Help() string { return `rrh fetch-all [OPTIONS] OPTIONS - -r, --remote specify the remote name. Default is "origin."` + -r, --remote= specify the remote name. Default is "origin."` } func (fetchAll *AllCommand) validateArguments(args []string) (*options, error) { @@ -94,7 +94,7 @@ func (fetchAll *AllCommand) parse(args []string) (*options, error) { var options = options{"origin", []string{}} flags := flag.NewFlagSet("fetch-all", flag.ExitOnError) flags.Usage = func() { fmt.Println(fetchAll.Help()) } - flags.StringVar(&options.remote, "r", "origin", "remote name") + flags.StringVarP(&options.remote, "remote", "r", "origin", "remote name") if err := flags.Parse(args); err != nil { return nil, err diff --git a/fetch/fetch_cmd.go b/fetch/fetch_cmd.go index 26ad97e..9a8aac4 100644 --- a/fetch/fetch_cmd.go +++ b/fetch/fetch_cmd.go @@ -1,10 +1,10 @@ package fetch import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -28,7 +28,7 @@ Help returns the help message of the command. func (fetch *Command) Help() string { return `rrh fetch [OPTIONS] [GROUPS...] OPTIONS - -r, --remote specify the remote name. Default is "origin." + -r, --remot= specify the remote name. Default is "origin." ARGUMENTS GROUPS run "git fetch" command on each repository on the group. if no value is specified, run on the default group.` @@ -92,8 +92,7 @@ func (fetch *Command) parse(args []string) (*options, error) { var options = options{"origin", []string{}} flags := flag.NewFlagSet("fetch", flag.ExitOnError) flags.Usage = func() { fmt.Println(fetch.Help()) } - flags.StringVar(&options.remote, "r", "origin", "remote name") - flags.StringVar(&options.remote, "remote", "origin", "remote name") + flags.StringVarP(&options.remote, "remote", "r", "origin", "remote name") // flags.StringVar(&options.key, "k", "", "private key path") // flags.StringVar(&options.userName, "u", "", "user name") // flags.StringVar(&options.password, "p", "", "password") diff --git a/group/group_cmd.go b/group/group_cmd.go index 4b56bfc..a51c36e 100644 --- a/group/group_cmd.go +++ b/group/group_cmd.go @@ -1,10 +1,10 @@ package group import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -50,8 +50,8 @@ func removeCommandFactory() (cli.Command, error) { func (gac *addCommand) Help() string { return `rrh group add [OPTIONS] OPTIONS - -d, --desc gives the description of the group. - -o, --omit-list gives the omit list flag of the group. + -d, --desc= gives the description of the group. + -o, --omit-list= gives the omit list flag of the group. ARGUMENTS GROUPS gives group names.` } @@ -83,9 +83,9 @@ ARGUMENTS func (guc *updateCommand) Help() string { return `rrh group update [OPTIONS] OPTIONS - -n, --name change group name to NAME. - -d, --desc change description to DESC. - -o, --omit-list change omit-list of the group. FLAG must be "true" or "false". + -n, --name= change group name to NAME. + -d, --desc= change description to DESC. + -o, --omit-list= change omit-list of the group. FLAG must be "true" or "false". ARGUMENTS GROUP update target group names.` } @@ -138,10 +138,8 @@ func (gac *addCommand) buildFlagSet() (*flag.FlagSet, *addOptions) { var opt = addOptions{} flags := flag.NewFlagSet("add", flag.ContinueOnError) flags.Usage = func() { fmt.Println(gac.Help()) } - flags.StringVar(&opt.desc, "d", "", "description") - flags.StringVar(&opt.desc, "desc", "", "description") - flags.StringVar(&opt.omit, "o", "", "omit list flag") - flags.StringVar(&opt.omit, "omit-list", "", "omit list flag") + flags.StringVarP(&opt.desc, "desc", "d", "", "description") + flags.StringVarP(&opt.omit, "omit-list", "o", "", "omit list flag") return flags, &opt } @@ -191,12 +189,9 @@ func (glc *listCommand) buildFlagSet() (*flag.FlagSet, *listOptions) { var opt = listOptions{} flags := flag.NewFlagSet("list", flag.ContinueOnError) flags.Usage = func() { fmt.Println(glc.Help()) } - flags.BoolVar(&opt.desc, "d", false, "show description") - flags.BoolVar(&opt.desc, "desc", false, "show description") - flags.BoolVar(&opt.repositories, "r", false, "show repositories") - flags.BoolVar(&opt.repositories, "repository", false, "show repositories") - flags.BoolVar(&opt.nameOnly, "o", false, "show only group names") - flags.BoolVar(&opt.nameOnly, "only-groupname", false, "show only group names") + flags.BoolVarP(&opt.desc, "desc", "d", false, "show description") + flags.BoolVarP(&opt.repositories, "repository", "r", false, "show repositories") + flags.BoolVarP(&opt.nameOnly, "only-groupname", "o", false, "show only group names") return flags, &opt } @@ -312,12 +307,9 @@ func (grc *removeCommand) buildFlagSet() (*flag.FlagSet, *removeOptions) { var opt = removeOptions{} flags := flag.NewFlagSet("rm", flag.ContinueOnError) flags.Usage = func() { fmt.Println(grc.Help()) } - flags.BoolVar(&opt.inquiry, "i", false, "inquiry mode") - flags.BoolVar(&opt.verbose, "v", false, "verbose mode") - flags.BoolVar(&opt.force, "f", false, "force remove") - flags.BoolVar(&opt.inquiry, "inquiry", false, "inquiry mode") - flags.BoolVar(&opt.verbose, "verbose", false, "verbose mode") - flags.BoolVar(&opt.force, "force", false, "force remove") + flags.BoolVarP(&opt.inquiry, "inquiry", "i", false, "inquiry mode") + flags.BoolVarP(&opt.verbose, "verbose", "v", false, "verbose mode") + flags.BoolVarP(&opt.force, "force", "f", false, "force remove") return flags, &opt } @@ -392,12 +384,9 @@ func (guc *updateCommand) buildFlagSet() (*flag.FlagSet, *updateOptions) { var opt = updateOptions{} flags := flag.NewFlagSet("update", flag.ContinueOnError) flags.Usage = func() { fmt.Println(guc.Help()) } - flags.StringVar(&opt.newName, "n", "", "specify new group name") - flags.StringVar(&opt.newName, "name", "", "specify new group name") - flags.StringVar(&opt.desc, "d", "", "specify the description") - flags.StringVar(&opt.desc, "desc", "", "specify the description") - flags.StringVar(&opt.omitList, "omit-list", "", "set the omit list flag.") - flags.StringVar(&opt.omitList, "o", "", "set the omit list flag.") + flags.StringVarP(&opt.newName, "name", "n", "", "specify new group name") + flags.StringVarP(&opt.desc, "desc", "d", "", "specify the description") + flags.StringVarP(&opt.omitList, "omit-list", "o", "", "set the omit list flag.") return flags, &opt } diff --git a/group/group_test.go b/group/group_test.go index 0fd5ef9..6cbc478 100644 --- a/group/group_test.go +++ b/group/group_test.go @@ -178,7 +178,7 @@ func TestUpdateGroup(t *testing.T) { gexists []groupChecker relations []relation }{ - {[]string{"update", "-d", "newdesc2", "--name", "newgroup2", "-o", "true", "group2"}, 0, + {[]string{"update", "-d", "newdesc2", "--name=newgroup2", "-o", "true", "group2"}, 0, []groupChecker{{"newgroup2", true, "newdesc2", true}, {"group2", false, "", false}}, []relation{}}, {[]string{"update", "-n", "newgroup3", "group3"}, 0, @@ -296,8 +296,8 @@ func TestHelp(t *testing.T) { var gacHelp = `rrh group add [OPTIONS] OPTIONS - -d, --desc gives the description of the group. - -o, --omit-list gives the omit list flag of the group. + -d, --desc= gives the description of the group. + -o, --omit-list= gives the omit list flag of the group. ARGUMENTS GROUPS gives group names.` @@ -317,9 +317,9 @@ ARGUMENTS var gucHelp = `rrh group update [OPTIONS] OPTIONS - -n, --name change group name to NAME. - -d, --desc change description to DESC. - -o, --omit-list change omit-list of the group. FLAG must be "true" or "false". + -n, --name= change group name to NAME. + -d, --desc= change description to DESC. + -o, --omit-list= change omit-list of the group. FLAG must be "true" or "false". ARGUMENTS GROUP update target group names.` diff --git a/list/list_cmd.go b/list/list_cmd.go index 202336e..4e4b042 100644 --- a/list/list_cmd.go +++ b/list/list_cmd.go @@ -1,10 +1,10 @@ package list import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -230,18 +230,12 @@ func (list *Command) buildFlagSet() (*flag.FlagSet, *options) { var options = options{args: []string{}} flags := flag.NewFlagSet("list", flag.ContinueOnError) flags.Usage = func() { fmt.Println(list.Help()) } - flags.BoolVar(&options.all, "A", false, "show all entries") - flags.BoolVar(&options.all, "all-entries", false, "show all entries") - flags.BoolVar(&options.description, "d", false, "description flag") - flags.BoolVar(&options.description, "desc", false, "description flag") - flags.BoolVar(&options.localPath, "p", false, "local path flag") - flags.BoolVar(&options.localPath, "path", false, "local path flag") - flags.BoolVar(&options.remoteURL, "r", false, "remote url flag") - flags.BoolVar(&options.remoteURL, "remote", false, "remote url flag") - flags.BoolVar(&options.noOmit, "a", false, "no omit repositories") - flags.BoolVar(&options.noOmit, "all", false, "no omit repositories") - flags.BoolVar(&options.csv, "c", false, "print as csv format") - flags.BoolVar(&options.csv, "csv", false, "print as csv format") + flags.BoolVarP(&options.all, "all-entries", "A", false, "show all entries") + flags.BoolVarP(&options.description, "desc", "d", false, "description flag") + flags.BoolVarP(&options.localPath, "path", "p", false, "local path flag") + flags.BoolVarP(&options.remoteURL, "remote", "r", false, "remote url flag") + flags.BoolVarP(&options.noOmit, "all", "a", false, "no omit repositories") + flags.BoolVarP(&options.csv, "csv", "c", false, "print as csv format") flags.BoolVar(&options.repoNameOnly, "only-repositoryname", false, "show only repository names") flags.BoolVar(&options.groupRepoName, "group-repository-form", false, "show group and repository pair form") return flags, &options diff --git a/move/move_cmd.go b/move/move_cmd.go index 698f324..308d954 100644 --- a/move/move_cmd.go +++ b/move/move_cmd.go @@ -1,11 +1,11 @@ package move import ( - "flag" "fmt" "strings" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -261,8 +261,7 @@ func buildFlagSet(mv *Command) (*flag.FlagSet, *options) { var options = options{false, false, []string{}, ""} flags := flag.NewFlagSet("mv", flag.ContinueOnError) flags.Usage = func() { fmt.Println(mv.Help()) } - flags.BoolVar(&options.verbose, "v", false, "verbose mode") - flags.BoolVar(&options.verbose, "verbose", false, "verbose mode") + flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose mode") return flags, &options } diff --git a/move/move_test.go b/move/move_test.go index 28c6974..df3c4e7 100644 --- a/move/move_test.go +++ b/move/move_test.go @@ -59,7 +59,7 @@ func TestMoveCommand(t *testing.T) { {"group to group", []string{"group1", "group4"}, []relation{ {"group4", "repo1", true}, {"group1", "repo1", false}}}, - {"groups to group", []string{"-verbose", "group1", "group3", "group4"}, []relation{ + {"groups to group", []string{"--verbose", "group1", "group3", "group4"}, []relation{ {"group4", "repo1", true}, {"group4", "repo2", true}, {"group3", "repo2", false}, diff --git a/path/path_cmd.go b/path/path_cmd.go index c5ab784..e0bd4a7 100644 --- a/path/path_cmd.go +++ b/path/path_cmd.go @@ -1,11 +1,11 @@ package path import ( - "flag" "fmt" "strings" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -101,10 +101,8 @@ func (path *Command) buildFlagSet() (*flag.FlagSet, *options) { var options = options{partialMatch: false, args: []string{}} flags := flag.NewFlagSet("path", flag.ContinueOnError) flags.Usage = func() { fmt.Println(path.Help()) } - flags.BoolVar(&options.partialMatch, "m", false, "partial match mode") - flags.BoolVar(&options.partialMatch, "partial-match", false, "partial match mode") - flags.BoolVar(&options.showRepoID, "r", false, "show path only") - flags.BoolVar(&options.showRepoID, "show-repository-id", false, "show path only") + flags.BoolVarP(&options.partialMatch, "partial-match", "m", false, "partial match mode") + flags.BoolVarP(&options.showRepoID, "show-repository-id", "r", false, "show path only") return flags, &options } diff --git a/remove/remove_cmd.go b/remove/remove_cmd.go index 156730c..5ca5249 100644 --- a/remove/remove_cmd.go +++ b/remove/remove_cmd.go @@ -1,10 +1,10 @@ package remove import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -75,12 +75,9 @@ func (rm *Command) buildFlagSet() (*flag.FlagSet, *options) { var options = options{false, false, false, []string{}} flags := flag.NewFlagSet("rm", flag.ContinueOnError) flags.Usage = func() { fmt.Println(rm.Help()) } - flags.BoolVar(&options.inquiry, "i", false, "inquiry flag") - flags.BoolVar(&options.verbose, "v", false, "verbose flag") - flags.BoolVar(&options.recursive, "r", false, "recursive flag") - flags.BoolVar(&options.inquiry, "inquiry", false, "inquiry flag") - flags.BoolVar(&options.verbose, "verbose", false, "verbose flag") - flags.BoolVar(&options.recursive, "recursive", false, "recursive flag") + flags.BoolVarP(&options.inquiry, "inquiry", "i", false, "inquiry flag") + flags.BoolVarP(&options.verbose, "verbose", "v", false, "verbose flag") + flags.BoolVarP(&options.recursive, "recursive", "r", false, "recursive flag") return flags, &options } diff --git a/repository/repository_cmd.go b/repository/repository_cmd.go index 53aee7a..23e036b 100644 --- a/repository/repository_cmd.go +++ b/repository/repository_cmd.go @@ -1,10 +1,10 @@ package repository import ( - "flag" "fmt" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" ) @@ -77,10 +77,8 @@ func (info *infoCommand) buildFlagSet() (*flag.FlagSet, *infoOptions) { var options = infoOptions{} flags := flag.NewFlagSet("info", flag.ContinueOnError) flags.Usage = func() { fmt.Println(info.Help()) } - flags.BoolVar(&options.csv, "c", false, "prints in the csv format.") - flags.BoolVar(&options.csv, "csv", false, "prints in the csv format.") - flags.BoolVar(&options.color, "G", false, "enables colorized output.") - flags.BoolVar(&options.color, "color", false, "enables colorized output.") + flags.BoolVarP(&options.csv, "csv", "c", false, "prints in the csv format.") + flags.BoolVarP(&options.color, "color", "G", false, "enables colorized output.") flags.BoolVar(&options.noColor, "no-color", false, "no colorized output.") return flags, &options } @@ -220,12 +218,9 @@ func (update *updateCommand) buildFlagSet() (*flag.FlagSet, *updateOptions) { var options = updateOptions{} flags := flag.NewFlagSet("update", flag.ContinueOnError) flags.Usage = func() { fmt.Println(update.Help()) } - flags.StringVar(&options.newID, "i", "", "specifies new repository id") - flags.StringVar(&options.description, "d", "", "specifies description") - flags.StringVar(&options.newPath, "p", "", "specifies new path") - flags.StringVar(&options.newID, "id", "", "specifies new repository id") - flags.StringVar(&options.description, "desc", "", "specifies description") - flags.StringVar(&options.newPath, "path", "", "specifies new path") + flags.StringVarP(&options.newID, "id", "i", "", "specifies new repository id") + flags.StringVarP(&options.description, "desc", "d", "", "specifies description") + flags.StringVarP(&options.newPath, "path", "p", "", "specifies new path") return flags, &options } @@ -321,9 +316,9 @@ Note: func (update *updateCommand) Help() string { return `rrh repository update [OPTIONS] OPTIONS - -i, --id specifies new repository id. - -d, --desc specifies new description. - -p, --path specifies new path. + -i, --id= specifies new repository id. + -d, --desc= specifies new description. + -p, --path= specifies new path. ARGUMENTS REPOSITORY specifies the repository id.` } diff --git a/repository/repository_test.go b/repository/repository_test.go index 3a4f118..824fd13 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -123,7 +123,7 @@ func TestUpdateRepository(t *testing.T) { newRepoID string wontRepo *common.Repository }{ - {[]string{"--id", "newRepo1", "--path", "newPath1", "--desc", "desc1", "repo1"}, 0, "newRepo1", &common.Repository{ID: "newRepo1", Description: "desc1", Path: "newPath1"}}, + {[]string{"--id=newRepo1", "--path=newPath1", "--desc=desc1", "repo1"}, 0, "newRepo1", &common.Repository{ID: "newRepo1", Description: "desc1", Path: "newPath1"}}, {[]string{"-d", "desc2", "repo2"}, 0, "repo2", &common.Repository{ID: "repo2", Description: "desc2", Path: "path2"}}, {[]string{"repo4"}, 3, "repo4", nil}, // unknown repository {[]string{"--invalid-option"}, 1, "never used", nil}, // invalid option @@ -185,9 +185,9 @@ Note: var updateCommandHelp = `rrh repository update [OPTIONS] OPTIONS - -i, --id specifies new repository id. - -d, --desc specifies new description. - -p, --path specifies new path. + -i, --id= specifies new repository id. + -d, --desc= specifies new description. + -p, --path= specifies new path. ARGUMENTS REPOSITORY specifies the repository id.` diff --git a/status/status_cmd.go b/status/status_cmd.go index f424296..bba40df 100644 --- a/status/status_cmd.go +++ b/status/status_cmd.go @@ -1,11 +1,11 @@ package status import ( - "flag" "fmt" "time" "github.com/mitchellh/cli" + flag "github.com/ogier/pflag" "github.com/tamada/rrh/common" "gopkg.in/src-d/go-git.v4/plumbing" ) @@ -69,7 +69,7 @@ OPTIONS -b, --branches show the status of the local branches. -r, --remote show the status of the remote branches. -c, --csv print result in csv format. - -f, --time-format specifies time format. Available value is + -f, --time-format= specifies time format. Available value is 'relative' ad 'absolute' ARGUMENTS REPOSITORIES target repositories. If no repository was specified @@ -163,14 +163,10 @@ func (status *Command) buildFlagSet() (*flag.FlagSet, *options) { var options = options{false, false, false, notSpecified} flags := flag.NewFlagSet("status", flag.ExitOnError) flags.Usage = func() { fmt.Println(status.Help()) } - flags.BoolVar(&options.csv, "c", false, "csv format") - flags.BoolVar(&options.csv, "csv", false, "csv format") - flags.BoolVar(&options.remote, "r", false, "remote branch status") - flags.BoolVar(&options.remote, "remote", false, "remote branch status") - flags.BoolVar(&options.branch, "b", false, "local branch status") - flags.BoolVar(&options.branch, "branches", false, "local branch status") - flags.StringVar(&options.format, "time-format", notSpecified, "specifies time format") - flags.StringVar(&options.format, "f", notSpecified, "specifies time format") + flags.BoolVarP(&options.csv, "csv", "c", false, "csv format") + flags.BoolVarP(&options.remote, "remote", "r", false, "remote branch status") + flags.BoolVarP(&options.branch, "branches", "b", false, "local branch status") + flags.StringVarP(&options.format, "time-format", "f", notSpecified, "specifies time format") return flags, &options } From c0dd8f7d8e7f4dcdb242f95222e75fcfe62f5cfa Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Fri, 17 May 2019 19:11:18 +0900 Subject: [PATCH 2/3] add a unit test of FindRemotes function --- .gitmodules | 3 --- add/add.go | 4 ++-- add/add_test.go | 19 +++++++++++++++++++ testdata/dummygit/README.md | 1 + 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 testdata/dummygit/README.md diff --git a/.gitmodules b/.gitmodules index 6541462..5667a40 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,9 +7,6 @@ [submodule "testdata/helloworld2"] path = testdata/other/helloworld url = https://htamada@bitbucket.org/htamada/helloworld2.git -[submodule "docs/themes/hugo-theme-massively"] - path = docs/themes/hugo-theme-massively - url = https://github.com/curtistimson/hugo-theme-massively.git [submodule "docs/themes/cayman-hugo-theme"] path = docs/themes/cayman-hugo-theme url = https://github.com/zwbetz-gh/cayman-hugo-theme.git diff --git a/add/add.go b/add/add.go index 559ad9c..26a1d91 100644 --- a/add/add.go +++ b/add/add.go @@ -75,11 +75,11 @@ func (add *Command) AddRepositoriesToGroup(db *common.Database, opt *options) [] FindRemotes function returns the remote of the given git repository. */ func FindRemotes(path string) ([]common.Remote, error) { - r, err := git.PlainOpen(path) + var repo, err = git.PlainOpen(path) if err != nil { return nil, err } - remotes, err := r.Remotes() + remotes, err := repo.Remotes() if err != nil { return nil, err } diff --git a/add/add_test.go b/add/add_test.go index 84c28cb..14b793b 100644 --- a/add/add_test.go +++ b/add/add_test.go @@ -164,3 +164,22 @@ func TestAddFailed(t *testing.T) { } } } + +func TestFindRemotes(t *testing.T) { + var testdata = []struct { + path string + errorFlag bool + count int + }{ + {"../testdata/dummygit", true, 0}, + } + for _, td := range testdata { + var remotes, err = FindRemotes(td.path) + if (err == nil) == td.errorFlag { + t.Errorf("%s: error flag did not match, wont: %v, got: %v, %v", td.path, td.errorFlag, !td.errorFlag, err) + } + if err != nil && td.count != len(remotes) { + t.Errorf("%s: remote count did not match, wont: %d, got: %d", td.path, td.count, len(remotes)) + } + } +} diff --git a/testdata/dummygit/README.md b/testdata/dummygit/README.md new file mode 100644 index 0000000..f808089 --- /dev/null +++ b/testdata/dummygit/README.md @@ -0,0 +1 @@ +This is dummy! From 3c5de29c95007f9345ff3248caa0e7907a0bdf4a Mon Sep 17 00:00:00 2001 From: Haruaki Tamada Date: Mon, 20 May 2019 11:13:13 +0900 Subject: [PATCH 3/3] update unit tests for updating coverage. --- common/config_cmd.go | 8 +- common/config_test.go | 170 +++++++++++++++++++++++++----------------- common/testutils.go | 1 + 3 files changed, 104 insertions(+), 75 deletions(-) diff --git a/common/config_cmd.go b/common/config_cmd.go index 5efc03d..9076b94 100644 --- a/common/config_cmd.go +++ b/common/config_cmd.go @@ -2,7 +2,6 @@ package common import ( "fmt" - "log" "github.com/mitchellh/cli" ) @@ -87,10 +86,7 @@ func (config *Command) Run(args []string) int { new(listCommand).Run([]string{}) return 0 } - var exitStatus, err = c.Run() - if err != nil { - log.Println(err) - } + var exitStatus, _ = c.Run() return exitStatus } @@ -106,7 +102,7 @@ func (csc *setCommand) Run(args []string) int { var err = config.Update(args[0], args[1]) if err != nil { fmt.Println(err.Error()) - return 1 + return 2 } config.StoreConfig() return 0 diff --git a/common/config_test.go b/common/config_test.go index 1cde5fa..0650a67 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -67,34 +67,35 @@ func TestSynopsises(t *testing.T) { } func TestConfigUnset(t *testing.T) { - os.Setenv(RrhConfigPath, "../testdata/config.json") - os.Setenv(RrhHome, "../testdata/") - var baseConfig = OpenConfig() - + os.Setenv(RrhOnError, Fail) var testcases = []struct { - label string + args []string status int wontValue string wontFrom ReadFrom }{ - {RrhAutoCreateGroup, 0, "false", Default}, - {"unknown", 0, "", NotFound}, + {[]string{RrhAutoCreateGroup}, 0, "false", Default}, + {[]string{"unknown"}, 5, "", NotFound}, + {[]string{RrhAutoCreateGroup, "tooManyArgs"}, 1, "", ""}, } for _, tc := range testcases { - var cuc, _ = unsetCommandFactory() - var statusCode = cuc.Run([]string{tc.label}) - if statusCode != tc.status { - t.Errorf("%v: status code did not match, wont: %d, got: %d", tc, tc.status, statusCode) - } - if statusCode == 0 { - var config = OpenConfig() - var value, from = config.GetString(tc.label) - if value != tc.wontValue || from != tc.wontFrom { - t.Errorf("%v: did not match: wont: (%s, %s), got: (%s, %s)", tc, tc.wontValue, tc.wontFrom, value, from) + var dbfile = Rollback("../testdata/tmp.json", "../testdata/config.json", func() { + var cuc, _ = unsetCommandFactory() + var statusCode = cuc.Run(tc.args) + if statusCode != tc.status { + t.Errorf("%v: status code did not match, wont: %d, got: %d", tc, tc.status, statusCode) } - } - baseConfig.StoreConfig() + if statusCode == 0 { + var config = OpenConfig() + var value, from = config.GetString(tc.args[0]) + if value != tc.wontValue || from != tc.wontFrom { + t.Errorf("%v: did not match: wont: (%s, %s), got: (%s, %s)", tc, tc.wontValue, tc.wontFrom, value, from) + } + } + }) + defer os.Remove(dbfile) } + os.Unsetenv(RrhOnError) } func ExampleCommand() { @@ -193,10 +194,6 @@ func TestLoadConfigFile(t *testing.T) { } func TestUpdateTrueFalseValue(t *testing.T) { - os.Setenv(RrhConfigPath, "../testdata/config.json") - var original = OpenConfig() - - var config = OpenConfig() var testdata = []struct { key string value string @@ -214,21 +211,20 @@ func TestUpdateTrueFalseValue(t *testing.T) { } for _, data := range testdata { - if err := config.Update(data.key, data.value); (err == nil) == data.wantError { - t.Errorf("%s: set to \"%s\", error: %s", data.key, data.value, err.Error()) - } - if val := config.GetValue(data.key); !data.wantError && val != data.wantValue { - t.Errorf("%s: want: %s, got: %s", data.key, data.wantValue, val) - } + var dbfile = Rollback("../testdata/tmp.json", "../testdata/config.json", func() { + var config = OpenConfig() + if err := config.Update(data.key, data.value); (err == nil) == data.wantError { + t.Errorf("%s: set to \"%s\", error: %s", data.key, data.value, err.Error()) + } + if val := config.GetValue(data.key); !data.wantError && val != data.wantValue { + t.Errorf("%s: want: %s, got: %s", data.key, data.wantValue, val) + } + }) + defer os.Remove(dbfile) } - original.StoreConfig() } func TestUpdateOnError(t *testing.T) { - os.Setenv(RrhConfigPath, "../testdata/config.json") - var original = OpenConfig() - - var config = OpenConfig() var testdata = []struct { key string success bool @@ -241,47 +237,46 @@ func TestUpdateOnError(t *testing.T) { } for _, data := range testdata { - if err := config.Update(RrhOnError, data.key); (err == nil) != data.success { - t.Errorf("%s: set to \"%s\", success: %v", RrhOnError, data.key, data.success) - } + var dbfile = Rollback("../testdata/tmp.json", "../testdata/config.json", func() { + var config = OpenConfig() + if err := config.Update(RrhOnError, data.key); (err == nil) != data.success { + t.Errorf("%s: set to \"%s\", success: %v", RrhOnError, data.key, data.success) + } + }) + defer os.Remove(dbfile) } - - original.StoreConfig() } func TestUpdateValue(t *testing.T) { - os.Setenv(RrhConfigPath, "../testdata/config.json") - var original = OpenConfig() - - var config = OpenConfig() - if err := config.Update(RrhConfigPath, "hogehoge"); err == nil { - t.Error("RrhConfigPath cannot update") - } - if err := config.Update(RrhHome, "hoge1"); err != nil { - t.Error(err.Error()) - } - if err := config.Update(RrhDatabasePath, "hoge2"); err != nil { - t.Error(err.Error()) - } - if err := config.Update(RrhDefaultGroupName, "hoge3"); err != nil { - t.Error(err.Error()) - } - if err := config.Update(RrhTimeFormat, "not-relative-string"); err != nil { - t.Error(err.Error()) - } - assert(t, config.GetValue(RrhHome), "hoge1") - assert(t, config.GetValue(RrhDatabasePath), "hoge2") - assert(t, config.GetValue(RrhDefaultGroupName), "hoge3") - assert(t, config.GetValue(RrhTimeFormat), "not-relative-string") - - if err := config.Update("unknown", "hoge4"); err == nil { - t.Error("unknown property was unknown") + var testdata = []struct { + label string + value string + shouldError bool + wontValue string + }{ + {RrhConfigPath, "hogehoge", true, ""}, + {RrhHome, "hoge1", false, "hoge1"}, + {RrhDatabasePath, "hoge2", false, "hoge2"}, + {RrhDefaultGroupName, "hoge3", false, "hoge3"}, + {RrhTimeFormat, "not-relative-string", false, "not-relative-string"}, + {"unknown", "hoge4", true, ""}, } - if config.IsSet(RrhTimeFormat) { - t.Error("IsSet is only bool value.") + for _, td := range testdata { + var dbfile = Rollback("../testdata/tmp.json", "../testdata/config.json", func() { + var config = NewConfig() + var err = config.Update(td.label, td.value) + if (err == nil) == td.shouldError { + t.Errorf("error of Update(%s, %s) did not match, wont: %v, got: %v", td.label, td.value, td.shouldError, !td.shouldError) + } + if err == nil { + var value = config.GetValue(td.label) + if value != td.wontValue { + t.Errorf("Value after Update(%s, %s) did not match, wont: %v, got: %v", td.label, td.value, td.wontValue, value) + } + } + }) + defer os.Remove(dbfile) } - - original.StoreConfig() } func TestOpenConfig(t *testing.T) { @@ -350,8 +345,45 @@ func TestPrintErrors(t *testing.T) { } } +func TestConfigSet(t *testing.T) { + var testdata = []struct { + args []string + statusCode int + value string + location ReadFrom + }{ + {[]string{"RRH_DEFAULT_GROUP_NAME", "newgroup"}, 0, "newgroup", ConfigFile}, + {[]string{"RRH_DEFAULT_GROUP_NAME"}, 1, "", ""}, + {[]string{"RRH_AUTO_DELETE_GROUP", "yes"}, 2, "", ""}, + {[]string{RrhConfigPath, "../testdata/broken.json"}, 2, "", ""}, + } + for _, td := range testdata { + var dbfile = Rollback("../testdata/tmp.json", "../testdata/config.json", func() { + var set, _ = setCommandFactory() + var status = set.Run(td.args) + if status != td.statusCode { + t.Errorf("%v: status code did not match, wont: %d, got: %d", td.args, td.statusCode, status) + } + if status == 0 { + var config = OpenConfig() + var value, from = config.GetString(td.args[0]) + if value != td.value { + t.Errorf("%v: set value did not match, wont: %s, got: %s", td.args, td.value, value) + } + if from != td.location { + t.Errorf("%v: read from did not match, wont: %s, got: %s", td.args, td.location, from) + } + } + }) + defer os.Remove(dbfile) + } +} + func TestFormatVariableAndValue(t *testing.T) { os.Setenv(RrhConfigPath, "../testdata/config.json") var config = OpenConfig() assert(t, config.formatVariableAndValue(RrhDefaultGroupName), "RRH_DEFAULT_GROUP_NAME: no-group (default)") + if config.IsSet(RrhOnError) { + t.Errorf("IsSet accepts only bool variable") + } } diff --git a/common/testutils.go b/common/testutils.go index 1f7d15e..5d99086 100644 --- a/common/testutils.go +++ b/common/testutils.go @@ -39,6 +39,7 @@ func Rollback(dbpath, configPath string, f func()) string { var config = OpenConfig() var db, _ = Open(config) defer db.StoreAndClose() + defer config.StoreConfig() f() })