diff --git a/lucifer/main.go b/lucifer/main.go index e78fc72..336d9fa 100644 --- a/lucifer/main.go +++ b/lucifer/main.go @@ -38,6 +38,7 @@ func usage() { } const baseUri = "http://127.0.0.1:11666" +const timeout = time.Duration(5) * time.Second type Filename string @@ -45,75 +46,132 @@ type LuciferInvalidateRequest struct { Files []Filename `json:"files"` } -func makeInvalidateRequest(fnames []Filename) error { - body := LuciferInvalidateRequest{ +type LuciferRunRequest struct { + Bail bool `json:"bail"` + Files []Filename `json:"files"` + Grep string `json:"grep"` +} + +func makeRequest(method string, uri string, body *bytes.Buffer) (*http.Response, error) { + req, err := http.NewRequest(method, uri, body) + if err != nil { + return nil, err + } + req.Header.Add("User-Agent", userAgent) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Accept", "application/json, q=0.8; application/problem+json, q=0.6; */*, q=0.3") + client := http.Client{ + Timeout: timeout, + } + return client.Do(req) +} + +func makeRunRequest(fnames []Filename, bail bool) (string, error) { + body := LuciferRunRequest{ + Bail: bail, Files: fnames, } var buf bytes.Buffer enc := json.NewEncoder(&buf) enc.Encode(body) - req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/cache/invalidate", baseUri), &buf) + resp, err := makeRequest("POST", fmt.Sprintf("%s/v1/test_runs", baseUri), &buf) if err != nil { - return err + return "", err } - req.Header.Add("User-Agent", userAgent) - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Accept", "application/json, q=0.8; application/problem+json, q=0.6; */*, q=0.3") - client := http.Client{ - Timeout: time.Duration(5) * time.Second, + defer resp.Body.Close() + rbody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + if resp.StatusCode >= 300 { + // XXX + return "", errors.New(string(rbody)) + } + return string(rbody), nil +} + +func makeInvalidateRequest(fnames []Filename) (string, error) { + body := LuciferInvalidateRequest{ + Files: fnames, } - resp, err := client.Do(req) + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + enc.Encode(body) + resp, err := makeRequest("POST", fmt.Sprintf("%s/v1/cache/invalidate", baseUri), &buf) if err != nil { - return err + return "", err } defer resp.Body.Close() rbody, err := ioutil.ReadAll(resp.Body) if err != nil { - return err + return "", err } if resp.StatusCode >= 300 { // XXX - return errors.New(string(rbody)) + return "", errors.New(string(rbody)) } - fmt.Println(string(rbody)) - return nil + return string(rbody), nil } -func main() { +func handleError(err error, verbose bool) { + if verbose { + log.Fatal(err) + } else { + os.Exit(0) + } +} + +func init() { flag.Usage = usage +} + +func doInvalidate(flags *flag.FlagSet, sync bool, verbose bool) { + err := flags.Parse(os.Args[2:]) + if err != nil { + handleError(err, verbose) + } + args := flags.Args() + var fnames []Filename + for i := 0; i < len(args); i++ { + fnames = append(fnames, Filename(args[i])) + } + body, err := makeInvalidateRequest(fnames) + if err != nil { + handleError(err, verbose) + } + fmt.Println(body) + fmt.Println(sync) +} + +func doRun(flags *flag.FlagSet, bail bool, verbose bool) { + err := flags.Parse(os.Args[2:]) + if err != nil { + handleError(err, verbose) + } + args := flags.Args() + var fnames []Filename + for i := 0; i < len(args); i++ { + fnames = append(fnames, Filename(args[i])) + } + body, err := makeRunRequest(fnames, bail) + if err != nil { + handleError(err, verbose) + } + fmt.Println(body) +} + +func main() { invalidateflags := flag.NewFlagSet("invalidate", flag.ExitOnError) sync := invalidateflags.Bool("sync", true, "Make request synchronously") verbose := invalidateflags.Bool("verbose", false, "Verbose output") runflags := flag.NewFlagSet("run", flag.ExitOnError) + bail := runflags.Bool("bail", false, "Bail after a single test failure") + runverbose := runflags.Bool("verbose", false, "Verbose response output") switch os.Args[1] { case "invalidate": - err := invalidateflags.Parse(os.Args[2:]) - if err != nil { - if *verbose { - log.Fatal(err) - } else { - os.Exit(0) - } - } - fmt.Println(*sync) - args := invalidateflags.Args() - var fnames []Filename - for i := 0; i < len(args); i++ { - fnames = append(fnames, Filename(args[i])) - } - err = makeInvalidateRequest(fnames) - if err != nil { - if *verbose { - log.Fatal(err) - } else { - os.Exit(0) - } - } + doInvalidate(invalidateflags, *sync, *verbose) case "run": - err := runflags.Parse(os.Args[2:]) - if err != nil { - log.Fatal(err) - } + doRun(runflags, *bail, *runverbose) default: usage() }