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/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.3.0 github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.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] scan,s [directory-with-.tidelift.yml]
configure,c [directory-with-.tidelift.yml] configure,c [directory-with-.tidelift.yml]
verify [directory-with-.tidelift.yml]
*/ */
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
@ -23,10 +24,18 @@ func main() {
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "Scan a directory's manifests", Usage: "Scan a directory's manifests",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if len(c.Args()) > 0 { directory := getDirectory(c)
return scan(c)
} return scan(directory)
return cli.NewExitError("no directory provided", 5) },
},
{
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 { func getDirectory(c *cli.Context) string {
directory := c.Args().First() // 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), ", ")) 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 return nil
} }

View File

@ -8,117 +8,118 @@ import (
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
) )
/* var (
When adding new manifest types, for new package managers /*
add a new entry here. 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
func manifestGlobs() []string { */
manifestGlobs = []string{
globs := make([]string, 0) // Hackage
"*.cabal",
// Hackage // npm
globs = append(globs, "*.cabal") "package.json",
// npm "package-lock.json",
globs = append(globs, "package.json") "npm-shrinkwrap.json",
globs = append(globs, "package-lock.json") "yarn.lock",
globs = append(globs, "npm-shrinkwrap.json") // Maven
globs = append(globs, "yarn.lock") "pom.xml",
// Maven "ivy.xml",
globs = append(globs, "pom.xml") "build.gradle",
globs = append(globs, "ivy.xml") // RubyGems
globs = append(globs, "build.gradle") "Gemfile",
// RubyGems "Gemfile.lock",
globs = append(globs, "Gemfile") "gems.rb",
globs = append(globs, "Gemfile.lock") "gems.locked",
globs = append(globs, "gems.rb") "*.gemspec",
globs = append(globs, "gems.locked") // Packagist
globs = append(globs, "*.gemspec") "composer.json",
// Packagist "composer.lock",
globs = append(globs, "composer.json") // PyPi
globs = append(globs, "composer.lock") "setup.py",
// PyPi "req*.txt",
globs = append(globs, "setup.py") "req*.pip",
globs = append(globs, "req*.txt") "requirements/*.txt",
globs = append(globs, "req*.pip") "requirements/*.pip",
globs = append(globs, "requirements/*.txt") "Pipfile",
globs = append(globs, "requirements/*.pip") "Pipfile.lock",
globs = append(globs, "Pipfile") // Nuget
globs = append(globs, "Pipfile.lock") "packages.config",
// Nuget "Project.json",
globs = append(globs, "packages.config") "Project.lock.json",
globs = append(globs, "Project.json") "*.nuspec",
globs = append(globs, "Project.lock.json") "paket.lock",
globs = append(globs, "*.nuspec") "*.csproj",
globs = append(globs, "paket.lock") // Bower
globs = append(globs, "*.csproj") "bower.json",
// Bower // CPAN
globs = append(globs, "bower.json") "META.json",
// CPAN "META.yml",
globs = append(globs, "META.json") // CocoaPods
globs = append(globs, "META.yml") "Podfile",
// CocoaPods "Podfile.lock",
globs = append(globs, "Podfile") "*.podspec",
globs = append(globs, "Podfile.lock") // Clojars
globs = append(globs, "*.podspec") "project.clj",
// Clojars // Meteor
globs = append(globs, "project.clj") "versions.json",
// Meteor // CRAN
globs = append(globs, "versions.json") "DESCRIPTION",
// CRAN // Cargo
globs = append(globs, "DESCRIPTION") "Cargo.toml",
// Cargo "Cargo.lock",
globs = append(globs, "Cargo.toml") // Hex
globs = append(globs, "Cargo.lock") "mix.exs",
// Hex "mix.lock",
globs = append(globs, "mix.exs") // Swift
globs = append(globs, "mix.lock") "Package.swift",
// Swift // Pub
globs = append(globs, "Package.swift") "pubspec.yaml",
// Pub "pubspec.lock",
globs = append(globs, "pubspec.yaml") // Carthage
globs = append(globs, "pubspec.lock") "Cartfile",
// Carthage "Cartfile.private",
globs = append(globs, "Cartfile") "Cartfile.resolved",
globs = append(globs, "Cartfile.private") // Dub
globs = append(globs, "Cartfile.resolved") "dub.json",
// Dub "dub.sdl",
globs = append(globs, "dub.json") // Julia
globs = append(globs, "dub.sdl") "REQUIRE",
// Julia // Shards
globs = append(globs, "REQUIRE") "shard.yml",
// Shards "shard.lock",
globs = append(globs, "shard.yml") // Go
globs = append(globs, "shard.lock") "glide.yaml",
// Go "glide.lock",
globs = append(globs, "glide.yaml") "Godeps",
globs = append(globs, "glide.lock") "Godeps/Godeps.json",
globs = append(globs, "Godeps") "vendor/manifest",
globs = append(globs, "Godeps/Godeps.json") "vendor/vendor.json",
globs = append(globs, "vendor/manifest") "Gopkg.toml",
globs = append(globs, "vendor/vendor.json") "Gopkg.lock",
globs = append(globs, "Gopkg.toml") // Elm
globs = append(globs, "Gopkg.lock") "elm-package.json",
// Elm "elm_dependencies.json",
globs = append(globs, "elm-package.json") "elm-stuff/exact-dependencies.json",
globs = append(globs, "elm_dependencies.json") // Haxelib
globs = append(globs, "elm-stuff/exact-dependencies.json") "haxelib.json",
// Haxelib // Hackage
globs = append(globs, "haxelib.json") "*.cabal",
// Hackage "cabal.config",
globs = append(globs, "*.cabal") }
globs = append(globs, "cabal.config") )
return globs
}
// 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 { func getListOfManifestFilenames(directory string) []string {
os.Chdir(directory) os.Chdir(directory)
matchingFiles := make([]string, 0) matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() { for _, glob := range manifestGlobs {
pathGlob := fmt.Sprintf("**/%s", glob) files := getListOfGlobMatches(glob)
files := getListOfGlobMatches(pathGlob)
if len(files) > 0 { if len(files) > 0 {
matchingFiles = append(matchingFiles, files...) matchingFiles = append(matchingFiles, files...)
@ -127,8 +128,14 @@ func getListOfManifestFilenames(directory string) []string {
return matchingFiles 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 { func getListOfGlobMatches(glob string) []string {
files, err := doublestar.Glob(glob) pathGlob := fmt.Sprintf("**/%s", glob)
files, err := doublestar.Glob(pathGlob)
if err != nil { if err != nil {
log.Fatal(err) 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
}