Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contributing subcommand #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions contributing/contributing.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 23 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
58 changes: 58 additions & 0 deletions subcommands/contributing.go
Original file line number Diff line number Diff line change
@@ -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)
},
}