tideliftcli/main.go

108 lines
2.4 KiB
Go
Raw Normal View History

2019-05-14 02:24:02 +00:00
package main
import (
"fmt"
"log"
"os"
2019-05-14 02:52:22 +00:00
"strings"
2019-05-14 02:24:02 +00:00
"github.com/urfave/cli"
)
2019-05-15 02:09:14 +00:00
/*
scan,s [directory-with-.tidelift.yml]
configure,c [directory-with-.tidelift.yml]
2019-05-15 02:40:51 +00:00
verify [directory-with-.tidelift.yml]
2019-05-15 02:09:14 +00:00
*/
2019-05-14 02:24:02 +00:00
func main() {
app := cli.NewApp()
app.Commands = []cli.Command{
{
2019-05-14 03:30:09 +00:00
Name: "scan",
2019-05-14 03:59:51 +00:00
Aliases: []string{"s"},
2019-05-14 03:28:30 +00:00
Usage: "Scan a directory's manifests",
2019-05-14 02:24:02 +00:00
Action: func(c *cli.Context) error {
2019-05-16 02:27:36 +00:00
everyCommand()
2019-05-15 02:40:51 +00:00
directory := getDirectory(c)
return scan(directory)
},
},
{
Name: "verify",
2019-05-16 02:34:22 +00:00
Usage: "Verify .tidelift.yml configuration and your ability to upload scans",
2019-05-15 02:40:51 +00:00
Action: func(c *cli.Context) error {
2019-05-16 02:27:36 +00:00
everyCommand()
2019-05-15 02:40:51 +00:00
directory := getDirectory(c)
return verify(directory)
2019-05-14 02:24:02 +00:00
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
2019-05-15 02:40:51 +00:00
func getDirectory(c *cli.Context) string {
// Return a directory if it's in the arguments
if len(c.Args()) > 0 {
2019-05-15 03:54:04 +00:00
path := c.Args().First()
path = strings.TrimRight(path, "/")
return path
2019-05-15 02:40:51 +00:00
}
return "."
}
2019-05-16 02:27:36 +00:00
2019-05-16 02:34:22 +00:00
/// everyCommand is things to run for every command
2019-05-16 02:27:36 +00:00
func everyCommand() error {
2019-05-16 02:34:22 +00:00
if os.Getenv("TIDELIFT_API_KEY") == "" {
return cli.NewExitError("please set TIDELIFT_API_KEY environment variable", 8)
2019-05-16 02:27:36 +00:00
}
return nil
}
2019-05-15 02:40:51 +00:00
// 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 {
2019-05-14 03:26:13 +00:00
2019-05-15 02:40:51 +00:00
// Show error if no .tidelift.yml file
if !verifyTideliftYamlExists(directory) {
return cli.NewExitError("no .tidelift.yml at supplied directory path", 6)
}
2019-05-14 21:26:22 +00:00
fmt.Println(strings.Join(getListOfManifestFilenames(directory), ", "))
2019-05-16 03:54:57 +00:00
upload(directory)
2019-05-15 02:40:51 +00:00
// 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
2019-05-16 02:27:36 +00:00
2019-05-15 02:40:51 +00:00
if !verifyTideliftYamlExists(directory) {
return cli.NewExitError("no .tidelift.yml at supplied directory path", 6)
}
2019-05-15 17:12:36 +00:00
missingKey := passesMinimumRequirements(readTideliftYamlFile(directory))
2019-05-15 03:19:10 +00:00
if missingKey != "" {
errorMsg := fmt.Sprintf("Missing key '%s:' in .tidelift.yml", missingKey)
return cli.NewExitError(errorMsg, 7)
2019-05-16 02:27:36 +00:00
}
2019-05-15 03:19:10 +00:00
2019-05-16 02:34:22 +00:00
fmt.Println("Your team-name, repo-name and TIDELIFT_API_KEY are all set! Feel free to use `scan`")
2019-05-14 03:26:13 +00:00
return nil
}
2019-05-16 03:54:57 +00:00
// Check error and panic if so
func check(e error) {
if e != nil {
panic(e)
}
}