blob: c66a7886dcad21e5426f094883152d4b6dc0ca94 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/golang/protobuf/jsonpb"
"github.com/maruel/subcommands"
tnProto "go.chromium.org/chromiumos/config/go/api/test/harness/tnull/v1"
rtd "go.chromium.org/chromiumos/config/go/api/test/rtd/v1"
"go.chromium.org/luci/common/errors"
)
type gen struct {
subcommands.CommandRunBase
dest string
}
func GenerateFullRequest() *subcommands.Command {
return &subcommands.Command{
UsageLine: "generate-full-request -output_json /path/to/output/json",
ShortDesc: "create an Invocation requesting every TNull test, as a JSON file.",
CommandRun: func() subcommands.CommandRun {
g := &gen{}
g.Flags.StringVar(&g.dest, "output_json", "", "Path to contain a JSON-encoded rtd.Invocation object")
return g
},
}
}
func (g *gen) Run(a subcommands.Application, args []string, env subcommands.Env) int {
lookup, err := extractTestsFromSpecification()
if err != nil {
fmt.Fprintf(a.GetErr(), "%s\n", err)
return 1
}
err = WriteJSONInvocation(lookup.Lookup, g.dest)
if err != nil {
fmt.Fprintf(a.GetErr(), "%s\n", err)
return 1
}
return 0
}
// WriteJSONInvocation writes a JSON encoded Invocation proto to destFile.
func WriteJSONInvocation(tests map[string]*tnProto.Steps, destFile string) error {
reqs := []*rtd.Request{}
for name := range tests {
req := rtd.Request{
Name: "request_" + filepath.Base(name),
Test: name,
}
reqs = append(reqs, &req)
}
Inv := rtd.Invocation{
Requests: reqs,
}
dir := filepath.Dir(destFile)
// Create the directory if it doesn't exist.
if err := os.MkdirAll(dir, 0777); err != nil {
return errors.Annotate(err, "write JSON pb").Err()
}
w, err := os.Create(destFile)
if err != nil {
return errors.Annotate(err, "write JSON pb").Err()
}
defer w.Close()
marshaler := jsonpb.Marshaler{Indent: " "}
if err := marshaler.Marshal(w, &Inv); err != nil {
return errors.Annotate(err, "write JSON pb").Err()
}
return nil
}