package cmd import ( "fmt" "os" "strings" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var tagCmd = &cobra.Command{ Use: "tag ", Short: "Add or remove tags on a document", Args: cobra.ExactArgs(1), RunE: runTag, } func init() { tagCmd.Flags().String("add", "", "tags to add (comma-separated)") tagCmd.Flags().String("remove", "", "tags to remove (comma-separated)") rootCmd.AddCommand(tagCmd) } func runTag(cmd *cobra.Command, args []string) error { addStr, _ := cmd.Flags().GetString("add") removeStr, _ := cmd.Flags().GetString("remove") body := map[string]interface{}{} if addStr != "" { body["add"] = splitTags(addStr) } if removeStr != "" { body["remove"] = splitTags(removeStr) } if len(body) == 0 { return fmt.Errorf("specify --add and/or --remove") } client := api.NewClient() resp, err := client.Put("/api/v1/documents/"+args[0]+"/tags", body) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if err := api.CheckError(resp); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if output.IsJSON() { var raw interface{} if err := api.DecodeJSON(resp, &raw); err != nil { return fmt.Errorf("failed to decode response: %w", err) } output.PrintJSON(raw) } else { fmt.Printf("Tags updated for document %s\n", args[0]) } return nil } func splitTags(s string) []string { var tags []string for _, t := range strings.Split(s, ",") { t = strings.TrimSpace(t) if t != "" { tags = append(tags, t) } } return tags }