forked from longtao.wu/droboticflow
147 lines
4.2 KiB
Go
147 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"droboticflow/internal/testlane"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(os.Args[1:]); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
func run(args []string) error {
|
|
if len(args) == 0 {
|
|
return errors.New("usage: internal-test-seed plan|apply [flags]")
|
|
}
|
|
switch args[0] {
|
|
case "plan":
|
|
return runPlan(args[1:])
|
|
case "apply":
|
|
return runApply(args[1:])
|
|
default:
|
|
return errors.New("usage: internal-test-seed plan|apply [flags]")
|
|
}
|
|
}
|
|
|
|
func runPlan(args []string) error {
|
|
f := flag.NewFlagSet("plan", flag.ContinueOnError)
|
|
manifestPath := f.String("manifest", "", "offline normalized manifest")
|
|
assetsRoot := f.String("assets-root", "", "copied asset import root")
|
|
workspaceRoot := f.String("workspace-root", testlane.RuntimeAssetsRoot, "isolated runtime workspace root")
|
|
batch := f.String("batch", "", "unique internal test batch ID")
|
|
out := f.String("out", "", "private plan file")
|
|
if err := f.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
if f.NArg() != 0 || *manifestPath == "" || *assetsRoot == "" || *batch == "" || *out == "" {
|
|
return errors.New("plan requires --manifest, --assets-root, --batch and --out")
|
|
}
|
|
m, err := loadManifest(*manifestPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p, err := testlane.BuildPlan(*batch, m, *assetsRoot, *workspaceRoot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, err := json.MarshalIndent(p, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data = append(data, '\n')
|
|
file, err := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err = file.Write(data); err != nil {
|
|
file.Close()
|
|
return err
|
|
}
|
|
if err = file.Close(); err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(map[string]any{"batchId": p.BatchID, "database": p.Database, "planSha256": p.SHA256, "projectIds": p.ProjectIDs, "sampleCount": len(p.Samples), "assetCount": len(p.Assets), "userIds": p.UserIDs})
|
|
}
|
|
|
|
func runApply(args []string) error {
|
|
f := flag.NewFlagSet("apply", flag.ContinueOnError)
|
|
planPath := f.String("plan", "", "plan file")
|
|
manifestPath := f.String("manifest", "", "same normalized manifest")
|
|
assetsRoot := f.String("assets-root", "", "copied asset import root")
|
|
accountsPath := f.String("accounts-file", "", "private generated account credentials file")
|
|
mongoURIPath := f.String("mongo-uri-file", "", "private dedicated Mongo URI file")
|
|
confirm := f.String("confirm", "", "exact plan SHA256 from plan output")
|
|
if err := f.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
if f.NArg() != 0 || *planPath == "" || *manifestPath == "" || *assetsRoot == "" || *accountsPath == "" || *mongoURIPath == "" || *confirm == "" {
|
|
return errors.New("apply requires --plan, --manifest, --assets-root, --accounts-file, --mongo-uri-file and --confirm")
|
|
}
|
|
data, err := os.ReadFile(*planPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var p testlane.Plan
|
|
if err := decodeExact(data, &p); err != nil {
|
|
return err
|
|
}
|
|
if *confirm != p.SHA256 {
|
|
return errors.New("--confirm does not match plan SHA256")
|
|
}
|
|
m, err := loadManifest(*manifestPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
accountsData, err := testlane.ReadPrivateFile(*accountsPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var accounts testlane.AccountsFile
|
|
if err := decodeExact(accountsData, &accounts); err != nil {
|
|
return err
|
|
}
|
|
uriData, err := testlane.ReadPrivateFile(*mongoURIPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
uri := strings.TrimSpace(string(uriData))
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
result, err := testlane.Apply(ctx, p, m, *assetsRoot, uri, accounts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(result)
|
|
}
|
|
|
|
func loadManifest(path string) (testlane.Manifest, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return testlane.Manifest{}, err
|
|
}
|
|
return testlane.DecodeManifest(data)
|
|
}
|
|
func decodeExact(data []byte, target any) error {
|
|
decoder := json.NewDecoder(strings.NewReader(string(data)))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(target); err != nil {
|
|
return err
|
|
}
|
|
if decoder.Decode(&struct{}{}) != io.EOF {
|
|
return errors.New("JSON has trailing data")
|
|
}
|
|
return nil
|
|
}
|