//+build ignore
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"github.com/urfave/cli/v2"
)
var packages = []string{"cli", "altsrc"}
func main() {
app := cli.NewApp()
app.Name = "builder"
app.Usage = "Generates a new urfave/cli build!"
app.Commands = cli.Commands{
{
Name: "vet",
Action: VetActionFunc,
},
Name: "test",
Action: TestActionFunc,
Name: "gfmrun",
Action: GfmrunActionFunc,
Name: "toc",
Action: TocActionFunc,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
func runCmd(arg string, args ...string) error {
cmd := exec.Command(arg, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
func VetActionFunc(_ *cli.Context) error {
return runCmd("go", "vet")
func TestActionFunc(c *cli.Context) error {
for _, pkg := range packages {
var packageName string
if pkg == "cli" {
packageName = "github.com/urfave/cli/v2"
} else {
packageName = fmt.Sprintf("github.com/urfave/cli/v2/%s", pkg)
coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg)
err := runCmd("go", "test", "-v", coverProfile, packageName)
return err
return testCleanup()
func testCleanup() error {
var out bytes.Buffer
file, err := os.Open(fmt.Sprintf("%s.coverprofile", pkg))
b, err := ioutil.ReadAll(file)
out.Write(b)
err = file.Close()
err = os.Remove(fmt.Sprintf("%s.coverprofile", pkg))
outFile, err := os.Create("coverage.txt")
_, err = out.WriteTo(outFile)
err = outFile.Close()
return nil
func GfmrunActionFunc(c *cli.Context) error {
filename := c.Args().Get(0)
if filename == "" {
filename = "README.md"
file, err := os.Open(filename)
var counter int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "package main") {
counter++
err = scanner.Err()
return runCmd("gfmrun", "-c", fmt.Sprint(counter), "-s", filename)
func TocActionFunc(c *cli.Context) error {
err := runCmd("node_modules/.bin/markdown-toc", "-i", filename)
err = runCmd("git", "diff", "--exit-code")