mirror of
https://github.com/aykhans/bsky-feedgen.git
synced 2025-07-17 13:24:01 +00:00
🦋
This commit is contained in:
18
cmd/manager/Dockerfile
Normal file
18
cmd/manager/Dockerfile
Normal file
@@ -0,0 +1,18 @@
|
||||
FROM golang:1.24-alpine AS builder
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
COPY ../../pkg ./pkg
|
||||
COPY ../../cmd/manager ./cmd/manager
|
||||
|
||||
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o manager ./cmd/manager/main.go
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:latest
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /src/manager .
|
||||
|
||||
ENTRYPOINT ["/app/manager"]
|
||||
CMD ["help"]
|
47
cmd/manager/README.md
Normal file
47
cmd/manager/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# Feed Manager CLI Tool
|
||||
|
||||
## Overview
|
||||
|
||||
The Feed Manager is a command-line interface (CLI) tool that allows users to create, update, and delete feed generator records on the Bluesky network.
|
||||
|
||||
**Pre-Built Docker Image**: `git.aykhans.me/bsky/feedgen-manager:latest`
|
||||
|
||||
## Commands
|
||||
|
||||
### Create a Feed Generator
|
||||
|
||||
```bash
|
||||
task run-manager create
|
||||
# or
|
||||
make run-manager create
|
||||
```
|
||||
|
||||
This will prompt for:
|
||||
|
||||
- Your Bluesky handle
|
||||
- Your Bluesky password
|
||||
- Feed generator hostname
|
||||
- Record short name (for the URL)
|
||||
- Display name
|
||||
- Description (optional)
|
||||
- Avatar image path (optional)
|
||||
|
||||
### Update a Feed Generator
|
||||
|
||||
```bash
|
||||
task run-manager update
|
||||
# or
|
||||
make run-manager update
|
||||
```
|
||||
|
||||
Allows updating the properties of an existing feed generator record.
|
||||
|
||||
### Delete a Feed Generator
|
||||
|
||||
```bash
|
||||
task run-manager delete
|
||||
# or
|
||||
make run-manager delete
|
||||
```
|
||||
|
||||
Permenantly removes a feed generator record from the Bluesky network.
|
268
cmd/manager/main.go
Normal file
268
cmd/manager/main.go
Normal file
@@ -0,0 +1,268 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/aykhans/bsky-feedgen/pkg/manage"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go listenForTermination(func() {
|
||||
cancel()
|
||||
fmt.Println()
|
||||
os.Exit(130)
|
||||
})
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "feedgen-manager",
|
||||
Short: "BlueSkye Feed Generator client CLI",
|
||||
Long: "A command-line tool for managing feed generators on Bluesky",
|
||||
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
|
||||
}
|
||||
|
||||
var createCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a feed generator record",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
handle := prompt("Enter your Bluesky handle", "")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: handle is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
password := promptPassword("Enter your Bluesky password (preferably an App Password)")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: password is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
service := prompt("Optionally, enter a custom PDS service to sign in with", manage.DefaultPDSHost)
|
||||
|
||||
feedgenHostname := prompt("Enter the feed generator hostname (e.g. 'feeds.bsky.example.com')", "")
|
||||
if feedgenHostname == "" {
|
||||
fmt.Println("\nError: hostname is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
recordKey := prompt("Enter a short name for the record. This will be shown in the feed's URL and should be unique.", "")
|
||||
if recordKey == "" {
|
||||
fmt.Println("\nError: short name is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
displayName := prompt("Enter a display name for your feed", "")
|
||||
if displayName == "" {
|
||||
fmt.Println("\nError: display name is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
description := prompt("Optionally, enter a brief description of your feed", "")
|
||||
avatar := prompt("Optionally, enter a local path to an avatar that will be used for the feed", "")
|
||||
|
||||
client, err := manage.NewClientWithAuth(ctx, manage.NewClient(&service), handle, password)
|
||||
if err != nil {
|
||||
fmt.Printf("\nAuthentication failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = manage.CreateFeedGenerator(
|
||||
ctx,
|
||||
client,
|
||||
displayName,
|
||||
toPtr(description),
|
||||
toPtr(avatar),
|
||||
"did:web:"+feedgenHostname,
|
||||
recordKey,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("\nFailed to create feed generator record: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("\nFeed generator created successfully! 🎉")
|
||||
},
|
||||
}
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Update a feed generator record",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
handle := prompt("Enter your Bluesky handle", "")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: handle is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
password := promptPassword("Enter your Bluesky password (preferably an App Password)")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: password is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
service := prompt("Optionally, enter a custom PDS service to sign in with", manage.DefaultPDSHost)
|
||||
feedgenHostname := prompt("Optionally, enter the feed generator hostname (e.g. 'feeds.bsky.example.com')", "")
|
||||
|
||||
recordKey := prompt("Enter short name of the record", "")
|
||||
if recordKey == "" {
|
||||
fmt.Println("\nError: short name is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
displayName := prompt("Optionally, enter a display name for your feed", "")
|
||||
description := prompt("Optionally, enter a brief description of your feed", "")
|
||||
avatar := prompt("Optionally, enter a local path to an avatar that will be used for the feed", "")
|
||||
|
||||
client, err := manage.NewClientWithAuth(ctx, manage.NewClient(&service), handle, password)
|
||||
if err != nil {
|
||||
fmt.Printf("\nAuthentication failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var did *string
|
||||
if feedgenHostname != "" {
|
||||
did = toPtr("did:web:" + feedgenHostname)
|
||||
}
|
||||
|
||||
err = manage.UpdateFeedGenerator(
|
||||
ctx,
|
||||
client,
|
||||
toPtr(displayName),
|
||||
toPtr(description),
|
||||
toPtr(avatar),
|
||||
did,
|
||||
recordKey,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("\nFailed to update feed generator record: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("\nFeed generator updated successfully! 🎉")
|
||||
},
|
||||
}
|
||||
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete a feed generator record",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
handle := prompt("Enter your Bluesky handle", "")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: handle is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
password := promptPassword("Enter your Bluesky password (preferably an App Password)")
|
||||
if handle == "" {
|
||||
fmt.Println("\nError: password is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
service := prompt("Optionally, enter a custom PDS service to sign in with", manage.DefaultPDSHost)
|
||||
|
||||
recordKey := prompt("Enter short name of the record", "")
|
||||
if recordKey == "" {
|
||||
fmt.Println("\nError: short name is required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
confirm := promptConfirm("Are you sure you want to delete this record? Any likes that your feed has will be lost", false)
|
||||
if !confirm {
|
||||
fmt.Println("\nAborting...")
|
||||
return
|
||||
}
|
||||
|
||||
client, err := manage.NewClientWithAuth(ctx, manage.NewClient(&service), handle, password)
|
||||
if err != nil {
|
||||
fmt.Printf("\nAuthentication failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = manage.DeleteFeedGenerator(ctx, client, recordKey)
|
||||
if err != nil {
|
||||
fmt.Printf("\nFailed to delete feed generator record: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("\nFeed generator deleted successfully! 🎉")
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(createCmd)
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// <-------------- Utils -------------->
|
||||
|
||||
func listenForTermination(do func()) {
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
do()
|
||||
}
|
||||
|
||||
func toPtr[T comparable](value T) *T {
|
||||
var zero T
|
||||
if value == zero {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &value
|
||||
}
|
||||
|
||||
func prompt(label string, defaultValue string) string {
|
||||
if defaultValue != "" {
|
||||
fmt.Printf("%s [%s]: ", label, defaultValue)
|
||||
} else {
|
||||
fmt.Printf("%s: ", label)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
input, _ := reader.ReadString('\n')
|
||||
input = strings.TrimSpace(input)
|
||||
|
||||
if input == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func promptPassword(label string) string {
|
||||
fmt.Printf("%s: ", label)
|
||||
password, _ := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Println()
|
||||
return string(password)
|
||||
}
|
||||
|
||||
func promptConfirm(label string, defaultValue bool) bool {
|
||||
defaultStr := "y/N"
|
||||
if defaultValue {
|
||||
defaultStr = "Y/n"
|
||||
}
|
||||
|
||||
fmt.Printf("%s [%s]: ", label, defaultStr)
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
input, _ := reader.ReadString('\n')
|
||||
input = strings.ToLower(strings.TrimSpace(input))
|
||||
|
||||
if input == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return input == "y" || input == "yes"
|
||||
}
|
Reference in New Issue
Block a user