|
| 1 | +// TODO: implement prompt for password |
| 2 | +// |
| 3 | +// See: https://github.com/charmbracelet/bubbletea/blob/main/examples/textinputs/main.go |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stormlightlabs/noteleaf/internal/handlers" |
| 11 | +) |
| 12 | + |
| 13 | +// PublicationCommand implements [CommandGroup] for leaflet publication commands |
| 14 | +type PublicationCommand struct { |
| 15 | + handler *handlers.PublicationHandler |
| 16 | +} |
| 17 | + |
| 18 | +// NewPublicationCommand creates a new [PublicationCommand] with the given handler |
| 19 | +func NewPublicationCommand(handler *handlers.PublicationHandler) *PublicationCommand { |
| 20 | + return &PublicationCommand{handler: handler} |
| 21 | +} |
| 22 | + |
| 23 | +func (c *PublicationCommand) Create() *cobra.Command { |
| 24 | + root := &cobra.Command{ |
| 25 | + Use: "pub", |
| 26 | + Short: "Manage leaflet publication sync", |
| 27 | + Long: `Sync notes with leaflet.pub (AT Protocol publishing platform). |
| 28 | +
|
| 29 | +Authenticate with your BlueSky account to pull drafts and published documents |
| 30 | +from leaflet.pub into your local notes. Track publication status and manage |
| 31 | +your writing workflow across platforms. |
| 32 | +
|
| 33 | +Authentication uses AT Protocol (the same system as BlueSky). You'll need: |
| 34 | +- BlueSky handle (e.g., username.bsky.social) |
| 35 | +- App password (generated at bsky.app/settings/app-passwords) |
| 36 | +
|
| 37 | +Getting Started: |
| 38 | + 1. Authenticate: noteleaf pub auth <handle> |
| 39 | + 2. Pull documents: noteleaf pub pull |
| 40 | + 3. List publications: noteleaf pub list`, |
| 41 | + } |
| 42 | + |
| 43 | + authCmd := &cobra.Command{ |
| 44 | + Use: "auth [handle]", |
| 45 | + Short: "Authenticate with BlueSky/leaflet", |
| 46 | + Long: `Authenticate with AT Protocol (BlueSky) for leaflet access. |
| 47 | +
|
| 48 | +Your handle is typically: username.bsky.social |
| 49 | +
|
| 50 | +For the password, use an app password (not your main password): |
| 51 | +1. Go to bsky.app/settings/app-passwords |
| 52 | +2. Create a new app password named "noteleaf" |
| 53 | +3. Use that password here |
| 54 | +
|
| 55 | +The password will be prompted securely if not provided via flag.`, |
| 56 | + Args: cobra.MaximumNArgs(1), |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + var handle string |
| 59 | + if len(args) > 0 { |
| 60 | + handle = args[0] |
| 61 | + } |
| 62 | + |
| 63 | + password, _ := cmd.Flags().GetString("password") |
| 64 | + |
| 65 | + if handle == "" { |
| 66 | + return fmt.Errorf("handle is required") |
| 67 | + } |
| 68 | + |
| 69 | + if password == "" { |
| 70 | + return fmt.Errorf("password is required (use --password flag)") |
| 71 | + } |
| 72 | + |
| 73 | + defer c.handler.Close() |
| 74 | + return c.handler.Auth(cmd.Context(), handle, password) |
| 75 | + }, |
| 76 | + } |
| 77 | + authCmd.Flags().StringP("password", "p", "", "App password (will be prompted if not provided)") |
| 78 | + root.AddCommand(authCmd) |
| 79 | + |
| 80 | + pullCmd := &cobra.Command{ |
| 81 | + Use: "pull", |
| 82 | + Short: "Pull documents from leaflet", |
| 83 | + Long: `Fetch all drafts and published documents from leaflet.pub. |
| 84 | +
|
| 85 | +This will: |
| 86 | +- Connect to your BlueSky/leaflet account |
| 87 | +- Fetch all documents in your repository |
| 88 | +- Create new notes for documents not yet synced |
| 89 | +- Update existing notes that have changed |
| 90 | +
|
| 91 | +Notes are matched by their leaflet record key (rkey) stored in the database.`, |
| 92 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 93 | + defer c.handler.Close() |
| 94 | + return c.handler.Pull(cmd.Context()) |
| 95 | + }, |
| 96 | + } |
| 97 | + root.AddCommand(pullCmd) |
| 98 | + |
| 99 | + // List command |
| 100 | + listCmd := &cobra.Command{ |
| 101 | + Use: "list [--published|--draft|--all]", |
| 102 | + Short: "List notes synced with leaflet", |
| 103 | + Aliases: []string{"ls"}, |
| 104 | + Long: `Display notes that have been pulled from or pushed to leaflet. |
| 105 | +
|
| 106 | +Shows publication metadata including: |
| 107 | +- Publication status (draft vs published) |
| 108 | +- Published date |
| 109 | +- Leaflet record key (rkey) |
| 110 | +- Content identifier (cid) for change tracking |
| 111 | +
|
| 112 | +Use filters to show specific subsets: |
| 113 | + --published Show only published documents |
| 114 | + --draft Show only drafts |
| 115 | + --all Show all leaflet documents (default)`, |
| 116 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 117 | + published, _ := cmd.Flags().GetBool("published") |
| 118 | + draft, _ := cmd.Flags().GetBool("draft") |
| 119 | + all, _ := cmd.Flags().GetBool("all") |
| 120 | + |
| 121 | + filter := "all" |
| 122 | + if published { |
| 123 | + filter = "published" |
| 124 | + } else if draft { |
| 125 | + filter = "draft" |
| 126 | + } else if all { |
| 127 | + filter = "all" |
| 128 | + } |
| 129 | + |
| 130 | + defer c.handler.Close() |
| 131 | + return c.handler.List(cmd.Context(), filter) |
| 132 | + }, |
| 133 | + } |
| 134 | + listCmd.Flags().Bool("published", false, "Show only published documents") |
| 135 | + listCmd.Flags().Bool("draft", false, "Show only drafts") |
| 136 | + listCmd.Flags().Bool("all", false, "Show all leaflet documents") |
| 137 | + root.AddCommand(listCmd) |
| 138 | + |
| 139 | + statusCmd := &cobra.Command{ |
| 140 | + Use: "status", |
| 141 | + Short: "Show leaflet authentication status", |
| 142 | + Long: "Display current authentication status and session information.", |
| 143 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 144 | + defer c.handler.Close() |
| 145 | + status := c.handler.GetAuthStatus() |
| 146 | + fmt.Println("Leaflet Status:") |
| 147 | + fmt.Printf(" %s\n", status) |
| 148 | + return nil |
| 149 | + }, |
| 150 | + } |
| 151 | + root.AddCommand(statusCmd) |
| 152 | + |
| 153 | + return root |
| 154 | +} |
0 commit comments