Resolve pipeline settings and collection selections
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
@@ -73,6 +74,20 @@ func processCommand() commandkit.Command {
|
||||
}
|
||||
}))
|
||||
}
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
path ck.Path
|
||||
}{
|
||||
{"glossaries", ck.Keys("glossaries")}, {"protected-terms", ck.Keys("protected_terms")},
|
||||
} {
|
||||
name, path := item.name, item.path
|
||||
flags = append(flags, commandkit.ValueFlag(commandkit.FlagMeta{Name: name, Usage: "JSON array of collection names, or null."}, (*any)(nil), parseCollectionNames,
|
||||
func(in *processInput, value commandkit.Supplied[any]) {
|
||||
if value.Set {
|
||||
in.Overrides = append(in.Overrides, ck.Override{Path: path, Value: value.Value, Flag: "--" + name})
|
||||
}
|
||||
}))
|
||||
}
|
||||
flags = append(flags, commandkit.BoolFlag(commandkit.FlagMeta{Name: "reporting", Usage: "Enable ordinary reports."}, false,
|
||||
func(in *processInput, v commandkit.Supplied[bool]) {
|
||||
if v.Set {
|
||||
@@ -83,3 +98,23 @@ func processCommand() commandkit.Command {
|
||||
Arguments: []commandkit.Argument[processInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "transcript", Usage: "Transcript JSON file."}, true, stringValue, func(in *processInput, v commandkit.Supplied[string]) { in.Transcript = v.Value })},
|
||||
})
|
||||
}
|
||||
|
||||
func parseCollectionNames(raw string) (any, error) {
|
||||
value, err := commandkit.ParseCLIJSON(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
names, ok := value.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected a JSON array of collection names or null")
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, ok := name.(string); !ok {
|
||||
return nil, fmt.Errorf("collection names must be strings")
|
||||
}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
@@ -56,3 +56,30 @@ func TestProcessUsesResolvedReportingMode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessCollectionFlags(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for name, contents := range map[string]string{
|
||||
"config.yml": "version: 3\npipeline:\n stages: []\nreporting:\n enabled: false\nglossary_collections:\n party: {entries: []}\n",
|
||||
"transcript.json": `[{"speaker":"Alice","start":0,"end":1,"text":"hello"}]`,
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
app, err := buildApplication()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
value string
|
||||
status int
|
||||
}{{`["party"]`, 0}, {`[]`, 0}, {`null`, 0}, {`[1]`, 2}, {`"party"`, 2}} {
|
||||
var out, stderr bytes.Buffer
|
||||
args := []string{"process", "--config", filepath.Join(dir, "config.yml"), "--glossaries", tc.value, filepath.Join(dir, "transcript.json")}
|
||||
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
|
||||
if outcome.Status != tc.status {
|
||||
t.Fatalf("value=%q status=%d out=%q err=%q", tc.value, outcome.Status, out.String(), stderr.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,3 +76,33 @@ func TestPrepareProcessInitializesReportsAndToleratesDebugLoss(t *testing.T) {
|
||||
t.Fatalf("output published early: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionFlagJSON(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
raw string
|
||||
valid bool
|
||||
null bool
|
||||
size int
|
||||
}{
|
||||
{`["party","spells"]`, true, false, 2}, {`[]`, true, false, 0}, {`null`, true, true, 0},
|
||||
{`"party"`, false, false, 0}, {`[1]`, false, false, 0}, {`["x"] true`, false, false, 0},
|
||||
} {
|
||||
result, err := parseCollectionNames(tc.raw)
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("%s error=%v", tc.raw, err)
|
||||
}
|
||||
if !tc.valid {
|
||||
continue
|
||||
}
|
||||
if tc.null {
|
||||
if result != nil {
|
||||
t.Fatalf("null=%v", result)
|
||||
}
|
||||
continue
|
||||
}
|
||||
list, ok := result.([]any)
|
||||
if !ok || len(list) != tc.size {
|
||||
t.Fatalf("%s result=%v", tc.raw, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user