summaryrefslogtreecommitdiff
path: root/cmd/planr/main.go
blob: 93f1cbfbbf08dd6e0e52472f1a2cb210b39dc6b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main

import ( "flag"
	"fmt"
	"io"
	"log"
	"os"
	"runtime/pprof"

	"golang.flu0r1ne.net/planr"
	"golang.flu0r1ne.net/planr/adapters/gtest"
	"golang.flu0r1ne.net/planr/adapters/bash"
	"golang.flu0r1ne.net/planr/cmd/planr/sub"
)

func printUsage(w io.Writer) {
	fmt.Fprintf (w, "usage: %s command args ...                   \n", os.Args[0])
	fmt.Fprintln(w, "  help                                                    ")
	fmt.Fprintln(w, "  version                                                 ")
	fmt.Fprintln(w, "  build                                                   ")
	fmt.Fprintln(w, "  evaluate                                                ")
	fmt.Fprintln(w, "  evaluate <test0> <test1> ...                            ")
	fmt.Fprintln(w, "  evaluate -json                                          ")
	fmt.Fprintln(w, "  clean                                                   ")
	fmt.Fprintln(w, "  config <key>                                            ")
}

func dieUsage() {
	printUsage(os.Stderr)
	os.Exit(1)
}

var src        = flag.String("srcdir",     "", "source directory")
var config     = flag.String("configdir",  "", "config directory")
var build      = flag.String("builddir",   "", "build directory")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func getConfiguredRunner() planr.Runner {
  r := planr.ConfigureRunner()
  r  = planr.RegisterAdapter(r, gtest.NewAdapter())
  r  = planr.RegisterAdapter(r, bash.NewAdapter())

  if wd, err := os.Getwd(); err == nil {
    r = planr.SetConfigDirFromTree(r, wd) 
  }

  if *src != "" {
    r = planr.SetSrcDir(r, *src)    
  }

  if *config != "" {
    r = planr.SetConfigDir(r, *config)
  }

  if *build != "" {
    r = planr.SetBuildDir(r, *build)
  }

  return r.New()
}

func main() {
        flag.Parse()

        if *cpuprofile != "" {
          f, err := os.Create(*cpuprofile)
          
          if err != nil {
            log.Fatal(err)
          }

          pprof.StartCPUProfile(f)
          defer pprof.StopCPUProfile()
        }

        log.SetFlags(log.Llongfile | log.Lmsgprefix)
        log.SetPrefix("planr: ")

	if len(os.Args) < 2 {
	  dieUsage()
	}

        runner := getConfiguredRunner()

        cfg := planr.DecodeConfig(runner.ConfigDir())

	subcommand := flag.Arg(0)
	subargs := flag.Args()[1:]

	switch subcommand {
	  case "version":
		fmt.Printf("%s\n", planr.VERSION)
	  case "build":
		sub.Build(runner, subargs, cfg)
	  case "evaluate", "eval":
		sub.Evaluate(runner, subargs, cfg)
          case "clean":
                sub.Clean(runner, subargs)
          case "config":
                sub.Config(runner, subargs)
	  case "help", "-h", "-help", "--help":
		printUsage(os.Stdout)
	  default:
		fmt.Fprintf(os.Stderr, "unrecognized command %s\n", subcommand)
		dieUsage()
	}

}