tideliftcli/main.go

98 lines
2.1 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-15 02:40:51 +00:00
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)
2019-05-14 02:24:02 +00:00
},
},
2019-05-14 03:59:51 +00:00
{
Name: "configure",
Aliases: []string{"c"},
Usage: "Configure the app",
Action: func(c *cli.Context) error {
return cli.NewExitError("No Configuration provided", 4)
},
},
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 "."
}
// 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-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
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-15 02:40:51 +00:00
2019-05-14 03:26:13 +00:00
return nil
}