refactor a bunch of stuff, docstrings

This commit is contained in:
Tyrel Souza 2019-05-14 22:40:51 -04:00 committed by Tyrel Souza
parent 104aa44ef5
commit e135c1379a
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
5 changed files with 172 additions and 114 deletions

1
go.mod
View File

@ -8,4 +8,5 @@ require (
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
gopkg.in/yaml.v2 v2.2.2
)

48
main.go
View File

@ -13,6 +13,7 @@ import (
scan,s [directory-with-.tidelift.yml]
configure,c [directory-with-.tidelift.yml]
verify [directory-with-.tidelift.yml]
*/
func main() {
app := cli.NewApp()
@ -23,10 +24,18 @@ func main() {
Aliases: []string{"s"},
Usage: "Scan a directory's manifests",
Action: func(c *cli.Context) error {
if len(c.Args()) > 0 {
return scan(c)
}
return cli.NewExitError("no directory provided", 5)
directory := getDirectory(c)
return scan(directory)
},
},
{
Name: "verify",
Usage: "Verify .tidelift.yml configuration",
Action: func(c *cli.Context) error {
directory := getDirectory(c)
return verify(directory)
},
},
{
@ -45,10 +54,35 @@ func main() {
}
}
func scan(c *cli.Context) error {
directory := c.Args().First()
func getDirectory(c *cli.Context) string {
// Return a directory if it's in the arguments
if len(c.Args()) > 0 {
return c.Args().First()
}
return "."
}
// Scan will scan a directory's manifests for all supported manifest files
// then it will upload them to tidelift for scanning
func scan(directory string) error {
// Show error if no .tidelift.yml file
if !verifyTideliftYamlExists(directory) {
return cli.NewExitError("no .tidelift.yml at supplied directory path", 6)
}
fmt.Println(strings.Join(getListOfManifestFilenames(directory), ", "))
// TODO, actually scan, don't just print names
// TODO, actually upload scan, don't just print names
return nil
}
// Verify will verify that a .tidelift.yml file has the minimum information
// in order to upload a manifest to Tidelift
func verify(directory string) error {
// Show error if no .tidelift.yml file
if !verifyTideliftYamlExists(directory) {
return cli.NewExitError("no .tidelift.yml at supplied directory path", 6)
}
return nil
}

View File

@ -8,117 +8,118 @@ import (
"github.com/bmatcuk/doublestar"
)
/*
When adding new manifest types, for new package managers
add a new entry here.
*/
func manifestGlobs() []string {
globs := make([]string, 0)
// Hackage
globs = append(globs, "*.cabal")
// npm
globs = append(globs, "package.json")
globs = append(globs, "package-lock.json")
globs = append(globs, "npm-shrinkwrap.json")
globs = append(globs, "yarn.lock")
// Maven
globs = append(globs, "pom.xml")
globs = append(globs, "ivy.xml")
globs = append(globs, "build.gradle")
// RubyGems
globs = append(globs, "Gemfile")
globs = append(globs, "Gemfile.lock")
globs = append(globs, "gems.rb")
globs = append(globs, "gems.locked")
globs = append(globs, "*.gemspec")
// Packagist
globs = append(globs, "composer.json")
globs = append(globs, "composer.lock")
// PyPi
globs = append(globs, "setup.py")
globs = append(globs, "req*.txt")
globs = append(globs, "req*.pip")
globs = append(globs, "requirements/*.txt")
globs = append(globs, "requirements/*.pip")
globs = append(globs, "Pipfile")
globs = append(globs, "Pipfile.lock")
// Nuget
globs = append(globs, "packages.config")
globs = append(globs, "Project.json")
globs = append(globs, "Project.lock.json")
globs = append(globs, "*.nuspec")
globs = append(globs, "paket.lock")
globs = append(globs, "*.csproj")
// Bower
globs = append(globs, "bower.json")
// CPAN
globs = append(globs, "META.json")
globs = append(globs, "META.yml")
// CocoaPods
globs = append(globs, "Podfile")
globs = append(globs, "Podfile.lock")
globs = append(globs, "*.podspec")
// Clojars
globs = append(globs, "project.clj")
// Meteor
globs = append(globs, "versions.json")
// CRAN
globs = append(globs, "DESCRIPTION")
// Cargo
globs = append(globs, "Cargo.toml")
globs = append(globs, "Cargo.lock")
// Hex
globs = append(globs, "mix.exs")
globs = append(globs, "mix.lock")
// Swift
globs = append(globs, "Package.swift")
// Pub
globs = append(globs, "pubspec.yaml")
globs = append(globs, "pubspec.lock")
// Carthage
globs = append(globs, "Cartfile")
globs = append(globs, "Cartfile.private")
globs = append(globs, "Cartfile.resolved")
// Dub
globs = append(globs, "dub.json")
globs = append(globs, "dub.sdl")
// Julia
globs = append(globs, "REQUIRE")
// Shards
globs = append(globs, "shard.yml")
globs = append(globs, "shard.lock")
// Go
globs = append(globs, "glide.yaml")
globs = append(globs, "glide.lock")
globs = append(globs, "Godeps")
globs = append(globs, "Godeps/Godeps.json")
globs = append(globs, "vendor/manifest")
globs = append(globs, "vendor/vendor.json")
globs = append(globs, "Gopkg.toml")
globs = append(globs, "Gopkg.lock")
// Elm
globs = append(globs, "elm-package.json")
globs = append(globs, "elm_dependencies.json")
globs = append(globs, "elm-stuff/exact-dependencies.json")
// Haxelib
globs = append(globs, "haxelib.json")
// Hackage
globs = append(globs, "*.cabal")
globs = append(globs, "cabal.config")
return globs
}
var (
/*
When adding new manifest types, for new package managers
add a new entry here. If it's a new platform/package manager, please
update the comments with the platform name for organization sense
*/
manifestGlobs = []string{
// Hackage
"*.cabal",
// npm
"package.json",
"package-lock.json",
"npm-shrinkwrap.json",
"yarn.lock",
// Maven
"pom.xml",
"ivy.xml",
"build.gradle",
// RubyGems
"Gemfile",
"Gemfile.lock",
"gems.rb",
"gems.locked",
"*.gemspec",
// Packagist
"composer.json",
"composer.lock",
// PyPi
"setup.py",
"req*.txt",
"req*.pip",
"requirements/*.txt",
"requirements/*.pip",
"Pipfile",
"Pipfile.lock",
// Nuget
"packages.config",
"Project.json",
"Project.lock.json",
"*.nuspec",
"paket.lock",
"*.csproj",
// Bower
"bower.json",
// CPAN
"META.json",
"META.yml",
// CocoaPods
"Podfile",
"Podfile.lock",
"*.podspec",
// Clojars
"project.clj",
// Meteor
"versions.json",
// CRAN
"DESCRIPTION",
// Cargo
"Cargo.toml",
"Cargo.lock",
// Hex
"mix.exs",
"mix.lock",
// Swift
"Package.swift",
// Pub
"pubspec.yaml",
"pubspec.lock",
// Carthage
"Cartfile",
"Cartfile.private",
"Cartfile.resolved",
// Dub
"dub.json",
"dub.sdl",
// Julia
"REQUIRE",
// Shards
"shard.yml",
"shard.lock",
// Go
"glide.yaml",
"glide.lock",
"Godeps",
"Godeps/Godeps.json",
"vendor/manifest",
"vendor/vendor.json",
"Gopkg.toml",
"Gopkg.lock",
// Elm
"elm-package.json",
"elm_dependencies.json",
"elm-stuff/exact-dependencies.json",
// Haxelib
"haxelib.json",
// Hackage
"*.cabal",
"cabal.config",
}
)
// getListOfManifestFilenames: Given a directory, changes directory to that directory,
// then loops through all of the support manifest file patterns, and
// gets all matching instances
//
// returns string[] of filenames relative to the directory passed in.
func getListOfManifestFilenames(directory string) []string {
os.Chdir(directory)
matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() {
pathGlob := fmt.Sprintf("**/%s", glob)
files := getListOfGlobMatches(pathGlob)
for _, glob := range manifestGlobs {
files := getListOfGlobMatches(glob)
if len(files) > 0 {
matchingFiles = append(matchingFiles, files...)
@ -127,8 +128,14 @@ func getListOfManifestFilenames(directory string) []string {
return matchingFiles
}
// getListOfGlobMatches uses doublestar's Glob function to expand and find
// all instances of a manifest file pattern, in your given directory
//
// returns string[] of all matching file patterns
func getListOfGlobMatches(glob string) []string {
files, err := doublestar.Glob(glob)
pathGlob := fmt.Sprintf("**/%s", glob)
files, err := doublestar.Glob(pathGlob)
if err != nil {
log.Fatal(err)
}

2
test/fixtures/.tidelift.yml vendored Normal file
View File

@ -0,0 +1,2 @@
team-name: tidelift
repo-name: fake

14
tidelift_yml.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"os"
)
func verifyTideliftYamlExists(directory string) bool {
os.Chdir(directory)
if _, err := os.Stat(".tidelift.yml"); err == nil {
return true
}
return false
}