Fix --json flag shadowing issue causing test failures
Fixed TestHashIDs_IdenticalContentDedup test failure by removing duplicate --json flag definitions that were shadowing the global persistent flag. Root cause: Commands had both a persistent --json flag (main.go) and local --json flags (in individual command files). The local flags shadowed the persistent flag, preventing jsonOutput variable from being set correctly. Changes: - Removed 31 duplicate --json flag definitions from 15 command files - All commands now use the single persistent --json flag from main.go - Commands now correctly output JSON when --json flag is specified Test results: - TestHashIDs_IdenticalContentDedup: Now passes (was failing) - TestHashIDs_MultiCloneConverge: Passes without JSON parsing warnings - All other tests: Pass with no regressions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
// Package main implements the bd CLI label management commands.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
@@ -8,25 +7,21 @@ import (
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/beads/internal/rpc"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/steveyegge/beads/internal/utils"
|
||||
)
|
||||
|
||||
var labelCmd = &cobra.Command{
|
||||
Use: "label",
|
||||
Short: "Manage issue labels",
|
||||
}
|
||||
|
||||
// Helper function to process label operations for multiple issues
|
||||
func processBatchLabelOperation(issueIDs []string, label string, operation string, jsonOut bool,
|
||||
daemonFunc func(string, string) error, storeFunc func(context.Context, string, string, string) error) {
|
||||
ctx := context.Background()
|
||||
results := []map[string]interface{}{}
|
||||
|
||||
for _, issueID := range issueIDs {
|
||||
var err error
|
||||
if daemonClient != nil {
|
||||
@@ -34,12 +29,10 @@ func processBatchLabelOperation(issueIDs []string, label string, operation strin
|
||||
} else {
|
||||
err = storeFunc(ctx, issueID, label, actor)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error %s label %s %s: %v\n", operation, operation, issueID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if jsonOut {
|
||||
results = append(results, map[string]interface{}{
|
||||
"status": operation,
|
||||
@@ -57,22 +50,18 @@ func processBatchLabelOperation(issueIDs []string, label string, operation strin
|
||||
fmt.Printf("%s %s label '%s' %s %s\n", green("✓"), verb, label, prep, issueID)
|
||||
}
|
||||
}
|
||||
|
||||
if len(issueIDs) > 0 && daemonClient == nil {
|
||||
markDirtyAndScheduleFlush()
|
||||
}
|
||||
|
||||
if jsonOut && len(results) > 0 {
|
||||
outputJSON(results)
|
||||
}
|
||||
}
|
||||
|
||||
func parseLabelArgs(args []string) (issueIDs []string, label string) {
|
||||
label = args[len(args)-1]
|
||||
issueIDs = args[:len(args)-1]
|
||||
return
|
||||
}
|
||||
|
||||
//nolint:dupl // labelAddCmd and labelRemoveCmd are similar but serve different operations
|
||||
var labelAddCmd = &cobra.Command{
|
||||
Use: "add [issue-id...] [label]",
|
||||
@@ -81,14 +70,12 @@ var labelAddCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Use global jsonOutput set by PersistentPreRun
|
||||
issueIDs, label := parseLabelArgs(args)
|
||||
|
||||
// Resolve partial IDs
|
||||
ctx := context.Background()
|
||||
resolvedIDs := make([]string, 0, len(issueIDs))
|
||||
for _, id := range issueIDs {
|
||||
var fullID string
|
||||
var err error
|
||||
|
||||
if daemonClient != nil {
|
||||
resolveArgs := &rpc.ResolveIDArgs{ID: id}
|
||||
resp, err := daemonClient.ResolveID(resolveArgs)
|
||||
@@ -107,7 +94,6 @@ var labelAddCmd = &cobra.Command{
|
||||
resolvedIDs = append(resolvedIDs, fullID)
|
||||
}
|
||||
issueIDs = resolvedIDs
|
||||
|
||||
processBatchLabelOperation(issueIDs, label, "added", jsonOutput,
|
||||
func(issueID, lbl string) error {
|
||||
_, err := daemonClient.AddLabel(&rpc.LabelAddArgs{ID: issueID, Label: lbl})
|
||||
@@ -118,7 +104,6 @@ var labelAddCmd = &cobra.Command{
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
//nolint:dupl // labelRemoveCmd and labelAddCmd are similar but serve different operations
|
||||
var labelRemoveCmd = &cobra.Command{
|
||||
Use: "remove [issue-id...] [label]",
|
||||
@@ -127,14 +112,12 @@ var labelRemoveCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Use global jsonOutput set by PersistentPreRun
|
||||
issueIDs, label := parseLabelArgs(args)
|
||||
|
||||
// Resolve partial IDs
|
||||
ctx := context.Background()
|
||||
resolvedIDs := make([]string, 0, len(issueIDs))
|
||||
for _, id := range issueIDs {
|
||||
var fullID string
|
||||
var err error
|
||||
|
||||
if daemonClient != nil {
|
||||
resolveArgs := &rpc.ResolveIDArgs{ID: id}
|
||||
resp, err := daemonClient.ResolveID(resolveArgs)
|
||||
@@ -153,7 +136,6 @@ var labelRemoveCmd = &cobra.Command{
|
||||
resolvedIDs = append(resolvedIDs, fullID)
|
||||
}
|
||||
issueIDs = resolvedIDs
|
||||
|
||||
processBatchLabelOperation(issueIDs, label, "removed", jsonOutput,
|
||||
func(issueID, lbl string) error {
|
||||
_, err := daemonClient.RemoveLabel(&rpc.LabelRemoveArgs{ID: issueID, Label: lbl})
|
||||
@@ -164,7 +146,6 @@ var labelRemoveCmd = &cobra.Command{
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
var labelListCmd = &cobra.Command{
|
||||
Use: "list [issue-id]",
|
||||
Short: "List labels for an issue",
|
||||
@@ -172,7 +153,6 @@ var labelListCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Use global jsonOutput set by PersistentPreRun
|
||||
ctx := context.Background()
|
||||
|
||||
// Resolve partial ID first
|
||||
var issueID string
|
||||
if daemonClient != nil {
|
||||
@@ -191,9 +171,7 @@ var labelListCmd = &cobra.Command{
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
var labels []string
|
||||
|
||||
// Use daemon if available
|
||||
if daemonClient != nil {
|
||||
resp, err := daemonClient.Show(&rpc.ShowArgs{ID: issueID})
|
||||
@@ -201,7 +179,6 @@ var labelListCmd = &cobra.Command{
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var issue types.Issue
|
||||
if err := json.Unmarshal(resp.Data, &issue); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error parsing response: %v\n", err)
|
||||
@@ -217,7 +194,6 @@ var labelListCmd = &cobra.Command{
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
// Always output array, even if empty
|
||||
if labels == nil {
|
||||
@@ -226,12 +202,10 @@ var labelListCmd = &cobra.Command{
|
||||
outputJSON(labels)
|
||||
return
|
||||
}
|
||||
|
||||
if len(labels) == 0 {
|
||||
fmt.Printf("\n%s has no labels\n", issueID)
|
||||
return
|
||||
}
|
||||
|
||||
cyan := color.New(color.FgCyan).SprintFunc()
|
||||
fmt.Printf("\n%s Labels for %s:\n", cyan("🏷"), issueID)
|
||||
for _, label := range labels {
|
||||
@@ -240,17 +214,14 @@ var labelListCmd = &cobra.Command{
|
||||
fmt.Println()
|
||||
},
|
||||
}
|
||||
|
||||
var labelListAllCmd = &cobra.Command{
|
||||
Use: "list-all",
|
||||
Short: "List all unique labels in the database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Use global jsonOutput set by PersistentPreRun
|
||||
ctx := context.Background()
|
||||
|
||||
var issues []*types.Issue
|
||||
var err error
|
||||
|
||||
// Use daemon if available
|
||||
if daemonClient != nil {
|
||||
resp, err := daemonClient.List(&rpc.ListArgs{})
|
||||
@@ -258,7 +229,6 @@ var labelListAllCmd = &cobra.Command{
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Data, &issues); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error parsing response: %v\n", err)
|
||||
os.Exit(1)
|
||||
@@ -271,7 +241,6 @@ var labelListAllCmd = &cobra.Command{
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Collect unique labels with counts
|
||||
labelCounts := make(map[string]int)
|
||||
for _, issue := range issues {
|
||||
@@ -292,7 +261,6 @@ var labelListAllCmd = &cobra.Command{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(labelCounts) == 0 {
|
||||
if jsonOutput {
|
||||
outputJSON([]string{})
|
||||
@@ -301,14 +269,12 @@ var labelListAllCmd = &cobra.Command{
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Sort labels alphabetically
|
||||
labels := make([]string, 0, len(labelCounts))
|
||||
for label := range labelCounts {
|
||||
labels = append(labels, label)
|
||||
}
|
||||
sort.Strings(labels)
|
||||
|
||||
if jsonOutput {
|
||||
// Output as array of {label, count} objects
|
||||
type labelInfo struct {
|
||||
@@ -325,10 +291,8 @@ var labelListAllCmd = &cobra.Command{
|
||||
outputJSON(result)
|
||||
return
|
||||
}
|
||||
|
||||
cyan := color.New(color.FgCyan).SprintFunc()
|
||||
fmt.Printf("\n%s All labels (%d unique):\n", cyan("🏷"), len(labels))
|
||||
|
||||
// Find longest label for alignment
|
||||
maxLen := 0
|
||||
for _, label := range labels {
|
||||
@@ -336,7 +300,6 @@ var labelListAllCmd = &cobra.Command{
|
||||
maxLen = len(label)
|
||||
}
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
padding := strings.Repeat(" ", maxLen-len(label))
|
||||
fmt.Printf(" %s%s (%d issues)\n", label, padding, labelCounts[label])
|
||||
@@ -344,13 +307,7 @@ var labelListAllCmd = &cobra.Command{
|
||||
fmt.Println()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
labelAddCmd.Flags().Bool("json", false, "Output JSON format")
|
||||
labelRemoveCmd.Flags().Bool("json", false, "Output JSON format")
|
||||
labelListCmd.Flags().Bool("json", false, "Output JSON format")
|
||||
labelListAllCmd.Flags().Bool("json", false, "Output JSON format")
|
||||
|
||||
labelCmd.AddCommand(labelAddCmd)
|
||||
labelCmd.AddCommand(labelRemoveCmd)
|
||||
labelCmd.AddCommand(labelListCmd)
|
||||
|
||||
Reference in New Issue
Block a user