tideliftcli/main.go

73 lines
1.3 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
2019-05-14 02:52:22 +00:00
"github.com/bmatcuk/doublestar"
2019-05-14 02:24:02 +00:00
"github.com/urfave/cli"
)
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-14 03:37:32 +00:00
if len(c.Args()) > 0 {
return scan(c)
}
return cli.NewExitError("no directory provided", 5)
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-14 03:28:30 +00:00
func scan(c *cli.Context) error {
2019-05-14 02:52:22 +00:00
directory := c.Args().First()
2019-05-14 03:26:13 +00:00
fmt.Println(strings.Join(getAllManifests(directory), ", "))
2019-05-14 03:28:30 +00:00
// TODO, actually scan, don't just print names
2019-05-14 03:26:13 +00:00
return nil
}
func getAllManifests(directory string) []string {
2019-05-14 02:52:22 +00:00
matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() {
2019-05-14 03:26:13 +00:00
pathGlob := fmt.Sprintf("%s/**/%s", directory, glob)
files := getManifestGlobMatches(pathGlob)
2019-05-14 02:52:22 +00:00
if len(files) > 0 {
matchingFiles = append(matchingFiles, files...)
}
}
2019-05-14 03:26:13 +00:00
return matchingFiles
2019-05-14 02:52:22 +00:00
}
2019-05-14 03:26:13 +00:00
func getManifestGlobMatches(glob string) []string {
2019-05-14 02:52:22 +00:00
files, err := doublestar.Glob(glob)
if err != nil {
log.Fatal(err)
}
return files
}