From 53469fe174cc6eae5cdd0c0725380cd230c2b22c Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Mon, 22 May 2017 14:51:12 +0800 Subject: [PATCH] contributor: Add order support Signed-off-by: Ce Gao --- AUTHORS.md | 2 +- cmd/contributor.go | 76 +++++++++++++++++++++++++++++++++++++++++----- config/types.go | 3 ++ 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index e16b2ed..0b319bb 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -2,4 +2,4 @@ Ce Gao -###### Auto generated by [gaocegege/maintainer](https://github.com/gaocegege/maintainer) on 2017-04-27 +###### Auto generated by [gaocegege/maintainer](https://github.com/gaocegege/maintainer) on 2017-05-22 diff --git a/cmd/contributor.go b/cmd/contributor.go index 503a6b8..b660458 100644 --- a/cmd/contributor.go +++ b/cmd/contributor.go @@ -19,6 +19,9 @@ import ( "os/exec" "strings" + "sort" + + "github.com/gaocegege/maintainer/config" "github.com/gaocegege/maintainer/util" "github.com/spf13/cobra" ) @@ -28,8 +31,39 @@ const ( gitLogArgs string = "log" gitFormatArgs string = "--format='%aN <%aE>'" authorFile string = "AUTHORS.md" + + orderTime string = "time" + orderCommit string = "commit" ) +var ( + order *string +) + +// Contributor is the type for contributor. +type Contributor struct { + Name string + Commit int +} + +// ContributorSlice is the type for slice of contributors. +type ContributorSlice []*Contributor + +// Len is part of sort.Interface. +func (d ContributorSlice) Len() int { + return len(d) +} + +// Swap is part of sort.Interface. +func (d ContributorSlice) Swap(i, j int) { + d[i], d[j] = d[j], d[i] +} + +// Less is part of sort.Interface. We use count as the value to sort by +func (d ContributorSlice) Less(i, j int) bool { + return d[i].Commit < d[j].Commit +} + // contributorCmd represents the contributor command var contributorCmd = &cobra.Command{ Use: "contributor", @@ -47,6 +81,9 @@ passion to contribute.`, func init() { RootCmd.AddCommand(contributorCmd) + + order = contributorCmd.PersistentFlags().String(config.Order, orderTime, "The order to compose Authors.md."+ + "(time, commit)") } // contributorRun runs the real logic to generate AUTHORS.md. @@ -67,9 +104,39 @@ func contributorRun() error { contributor = strings.Trim(contributor, "'") if _, ok := dict[contributor]; ok != true { dict[contributor] = 1 + } else { + dict[contributor] = dict[contributor] + 1 } } + return composeOrder(&dict) +} +// authorHeader returns the header to be written into AUTHORS.md. +func authorHeader() string { + return "# Authors\n\n" +} + +func composeOrder(data *map[string]int) error { + contributors := make(ContributorSlice, 0, len(*data)) + for k, v := range *data { + contributors = append(contributors, &Contributor{ + Name: k, + Commit: v, + }) + } + + switch *order { + case orderCommit: + sort.Sort(sort.Reverse(contributors)) + } + return writeToFile(contributors) +} + +func orderByCommit(contributors ContributorSlice) error { + return nil +} + +func writeToFile(contributors ContributorSlice) error { // Output results to AUTHORS.md. f, err := util.OpenFile(authorFile) if err != nil { @@ -78,8 +145,8 @@ func contributorRun() error { if _, err := f.WriteString(authorHeader()); err != nil { return err } - for k := range dict { - if _, err := f.WriteString(k); err != nil { + for _, k := range contributors { + if _, err := f.WriteString(k.Name); err != nil { return err } if _, err := f.WriteString("\n\n"); err != nil { @@ -92,8 +159,3 @@ func contributorRun() error { } return nil } - -// authorHeader returns the header to be written into AUTHORS.md. -func authorHeader() string { - return "# Authors\n\n" -} diff --git a/config/types.go b/config/types.go index 79fa119..1bd7db8 100644 --- a/config/types.go +++ b/config/types.go @@ -17,4 +17,7 @@ package config const ( // Token is the name of the config of token. Token string = "token" + // Order is the name of the config of order, + // which is used in contributor subcommand. + Order string = "order" )