CLI: Flatten config options output when using the "--json" flag #5220

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-09-21 16:52:56 +02:00
parent 25253afcf2
commit f1c57c72d8
14 changed files with 106 additions and 84 deletions

View File

@@ -8,11 +8,14 @@ import (
func TestShowThumbSizes_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowThumbSizesCommand, []string{"thumb-sizes", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v []map[string]string
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v) == 0 {
@@ -28,11 +31,14 @@ func TestShowThumbSizes_JSON(t *testing.T) {
func TestShowSources_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowSourcesCommand, []string{"sources", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v []map[string]string
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v) == 0 {
@@ -48,16 +54,20 @@ func TestShowSources_JSON(t *testing.T) {
func TestShowMetadata_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowMetadataCommand, []string{"metadata", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v struct {
Items []map[string]string `json:"items"`
Docs []map[string]string `json:"docs"`
}
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v.Items) == 0 {
t.Fatalf("expected items")
}
@@ -65,18 +75,22 @@ func TestShowMetadata_JSON(t *testing.T) {
func TestShowConfig_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowConfigCommand, []string{"config", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v struct {
Sections []struct {
Title string `json:"title"`
Items []map[string]string `json:"items"`
} `json:"sections"`
}
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v.Sections) == 0 || len(v.Sections[0].Items) == 0 {
t.Fatalf("expected sections with items")
}
@@ -84,47 +98,49 @@ func TestShowConfig_JSON(t *testing.T) {
func TestShowConfigOptions_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowConfigOptionsCommand, []string{"config-options", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v struct {
Sections []struct {
Title string `json:"title"`
Items []map[string]string `json:"items"`
} `json:"sections"`
}
if err := json.Unmarshal([]byte(out), &v); err != nil {
type options = []map[string]string
var v = options{}
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v.Sections) == 0 || len(v.Sections[0].Items) == 0 {
if len(v) == 0 {
t.Fatalf("expected sections with items")
}
}
func TestShowConfigYaml_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowConfigYamlCommand, []string{"config-yaml", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v struct {
Sections []struct {
Title string `json:"title"`
Items []map[string]string `json:"items"`
} `json:"sections"`
}
if err := json.Unmarshal([]byte(out), &v); err != nil {
type options = []map[string]string
var v = options{}
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v.Sections) == 0 || len(v.Sections[0].Items) == 0 {
if len(v) == 0 {
t.Fatalf("expected sections with items")
}
}
func TestShowFormatConflict_Error(t *testing.T) {
out, err := RunWithTestContext(ShowSourcesCommand, []string{"sources", "--json", "--csv"})
if err == nil {
t.Fatalf("expected error for conflicting flags, got nil; output=%s", out)
}
// Expect an ExitCoder with code 2
if ec, ok := err.(interface{ ExitCode() int }); ok {
if ec.ExitCode() != 2 {
@@ -154,11 +170,14 @@ func min(a, b int) int {
func TestShowFileFormats_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowFileFormatsCommand, []string{"file-formats", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v []map[string]string
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v) == 0 {
@@ -178,11 +197,14 @@ func TestShowFileFormats_JSON(t *testing.T) {
func TestShowVideoSizes_JSON(t *testing.T) {
out, err := RunWithTestContext(ShowVideoSizesCommand, []string{"video-sizes", "--json"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var v []map[string]string
if err := json.Unmarshal([]byte(out), &v); err != nil {
if err = json.Unmarshal([]byte(out), &v); err != nil {
t.Fatalf("invalid json: %v\n%s", err, out)
}
if len(v) == 0 {