package cmd import ( "fmt" "os" "sort" "github.com/spf13/cobra" "github.com/yeho/doks/internal/registry" ) var listTagFilter string var listCmd = &cobra.Command{ Use: "list", Short: "List all registered documents", Long: `Display all documents registered in doks, optionally filtered by tag.`, Run: func(cmd *cobra.Command, args []string) { reg, err := registry.Load(cfg.RegistryPath()) if err != nil { fmt.Fprintf(os.Stderr, "Error loading registry: %v\n", err) os.Exit(1) } entries := reg.List() if len(entries) == 0 { fmt.Println("No documents registered") return } // Sort by key sort.Slice(entries, func(i, j int) bool { return entries[i].Key < entries[j].Key }) // Filter by tag if specified if listTagFilter != "" { filtered := make([]*registry.Entry, 0) for _, entry := range entries { if hasTag(entry.Tags, listTagFilter) { filtered = append(filtered, entry) } } entries = filtered } if len(entries) == 0 { fmt.Printf("No documents found with tag '%s'\n", listTagFilter) return } // Print entries for _, entry := range entries { symlink := "" if entry.HasSymlink { symlink = " [symlinked]" } tags := "" if len(entry.Tags) > 0 { tags = fmt.Sprintf(" [%s]", joinStrings(entry.Tags, ", ")) } fmt.Printf(" %s%s%s\n", entry.Key, tags, symlink) if entry.Description != "" { fmt.Printf(" %s\n", entry.Description) } } }, } func init() { rootCmd.AddCommand(listCmd) listCmd.Flags().StringVarP(&listTagFilter, "tag", "t", "", "Filter by tag") } func hasTag(tags []string, target string) bool { for _, tag := range tags { if tag == target { return true } } return false } func joinStrings(strs []string, sep string) string { if len(strs) == 0 { return "" } result := strs[0] for i := 1; i < len(strs); i++ { result += sep + strs[i] } return result }