From 5fc5b83417e90b0aad1aef2d3fe4c7da89b5bb61 Mon Sep 17 00:00:00 2001 From: "Kyle E. Mitchell" Date: Tue, 17 Jul 2018 13:02:06 -0700 Subject: [PATCH] Add contributing subcommand --- contributing/contributing.go | 57 +++++++++++++++++++++++++++++++++++ main.go | 45 ++++++++++++++-------------- subcommands/contributing.go | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 contributing/contributing.go create mode 100644 subcommands/contributing.go diff --git a/contributing/contributing.go b/contributing/contributing.go new file mode 100644 index 0000000..bdbfce5 --- /dev/null +++ b/contributing/contributing.go @@ -0,0 +1,57 @@ +package contributing + +import "errors" +import "io/ioutil" +import "os" +import "path" +import "regexp" + +// CONTRIBUTINGPattern matches CONTRIBUTING file names. +var CONTRIBUTINGPattern = regexp.MustCompile(`(?i)^contributing(\.(txt|md|markdown|mdown|mkdn|textile|rdoc|org|creole|mediawiki|wiki|rst|asciidoc|adoc|asc|pod))?$`) + +// FindCONTRIBUTING returns the file name of a CONTRIBUTING in the directory. +func FindCONTRIBUTING(directoryPath string) (string, error) { + // Read the entries in the directory. + directory, err := os.Open(directoryPath) + if err != nil { + return "", err + } + defer directory.Close() + entries, err := directory.Readdir(-1) + if err != nil { + return "", err + } + // Compile a list of names that match our pattern. + var matches []string + for _, entry := range entries { + if entry.Mode()&os.ModeType != 0 { + // Not a regular file. + continue + } + name := entry.Name() + if CONTRIBUTINGPattern.MatchString(name) { + matches = append(matches, name) + } + } + if len(matches) == 0 { + return "", errors.New("could not find CONTRIBUTING file") + } + if len(matches) != 1 { + return "", errors.New("found multiple CONTRIBUTING files") + } + return matches[0], nil +} + +// ReadCONTRIBUTING returns the file name and contents of the CONTRIBUTING in a directory. +func ReadCONTRIBUTING(directoryPath string) (string, []byte, error) { + fileName, err := FindCONTRIBUTING(directoryPath) + if err != nil { + return "", nil, err + } + path := path.Join(directoryPath, fileName) + data, err := ioutil.ReadFile(path) + if err != nil { + return "", nil, err + } + return fileName, data, nil +} diff --git a/main.go b/main.go index b349b2c..2b85c63 100644 --- a/main.go +++ b/main.go @@ -10,28 +10,29 @@ import "sort" var Rev string var commands = map[string]*subcommands.Subcommand{ - "backup": subcommands.Backup, - "buy": subcommands.Buy, - "bugs": subcommands.Bugs, - "identify": subcommands.Identify, - "import": subcommands.Import, - "license": subcommands.License, - "latest": subcommands.Latest, - "lock": subcommands.Lock, - "offer": subcommands.Offer, - "quote": subcommands.Quote, - "projects": subcommands.Projects, - "readme": subcommands.README, - "register": subcommands.Register, - "relicense": subcommands.Relicense, - "reprice": subcommands.Reprice, - "reset": subcommands.Reset, - "retract": subcommands.Retract, - "sponsor": subcommands.Sponsor, - "token": subcommands.Token, - "version": subcommands.Version, - "waive": subcommands.Waive, - "whoami": subcommands.WhoAmI, + "backup": subcommands.Backup, + "buy": subcommands.Buy, + "bugs": subcommands.Bugs, + "contributing": subcommands.Contributing, + "identify": subcommands.Identify, + "import": subcommands.Import, + "license": subcommands.License, + "latest": subcommands.Latest, + "lock": subcommands.Lock, + "offer": subcommands.Offer, + "quote": subcommands.Quote, + "projects": subcommands.Projects, + "readme": subcommands.README, + "register": subcommands.Register, + "relicense": subcommands.Relicense, + "reprice": subcommands.Reprice, + "reset": subcommands.Reset, + "retract": subcommands.Retract, + "sponsor": subcommands.Sponsor, + "token": subcommands.Token, + "version": subcommands.Version, + "waive": subcommands.Waive, + "whoami": subcommands.WhoAmI, } func main() { diff --git a/subcommands/contributing.go b/subcommands/contributing.go new file mode 100644 index 0000000..1883f88 --- /dev/null +++ b/subcommands/contributing.go @@ -0,0 +1,58 @@ +package subcommands + +import "flag" +import "github.com/licensezero/cli/contributing" +import "io/ioutil" +import "os" +import "strings" + +const contributingDescription = "Add licensing information to CONTRIBUTING." + +// Contributing appends licensing information to CONTRIBUTING. +var Contributing = &Subcommand{ + Tag: "seller", + Description: contributingDescription, + Handler: func(args []string, paths Paths) { + flagSet := flag.NewFlagSet("contributing", flag.ExitOnError) + silent := silentFlag(flagSet) + flagSet.SetOutput(ioutil.Discard) + flagSet.Usage = func() { + usage := contributingDescription + "\n\n" + + "Usage:\n" + + " licensezero contributing\n\n" + + "Options:\n" + + flagsList(map[string]string{ + "silent": silentLine, + }) + Fail(usage) + } + flagSet.Parse(args) + // Append to CONTRIBUTING. + contributingName, data, err := contributing.ReadCONTRIBUTING(paths.CWD) + var toWrite string + if err != nil { + Fail("Error: " + err.Error()) + } else { + toWrite = string(data) + if strings.HasSuffix(toWrite, "\n") { + toWrite = toWrite + "\n" + } else { + toWrite = toWrite + "\n\n" + } + } + toWrite = toWrite + + "# Licensing\n\n" + + "If you submit a pull request, please be prepared to license " + + "your contributions under the terms of the " + + "[Charity Public License](https://licensezero.com/licenses/charity), " + + "a modern evolution of licenses like MIT and the two-clause BSD license.\n" + err = ioutil.WriteFile(contributingName, []byte(toWrite), 0644) + if err != nil { + Fail("Error writing CONTRIBUTING") + } + if !*silent { + os.Stdout.WriteString("Appended terms to CONTRIBUTING.\n") + } + os.Exit(0) + }, +}