Move to Federated Gitea
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
claudeapi "coremarketing/internal/api/claude"
|
||||
linkedinapi "coremarketing/internal/api/linkedin"
|
||||
"coremarketing/internal/api/twitter"
|
||||
"coremarketing/internal/api/youtube"
|
||||
"coremarketing/internal/auth"
|
||||
"coremarketing/internal/config"
|
||||
"coremarketing/internal/fetcher"
|
||||
"coremarketing/internal/state"
|
||||
"coremarketing/internal/store"
|
||||
"coremarketing/internal/ui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfgPath := filepath.Join(os.Getenv("HOME"), ".local", "state", "coremarketing", "config.yaml")
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "auth" {
|
||||
rest := os.Args[2:]
|
||||
if len(rest) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: coremarketing auth <twitter|linkedin> [config.yaml]\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
platform := rest[0]
|
||||
rest = rest[1:]
|
||||
if len(rest) > 0 {
|
||||
cfgPath = rest[0]
|
||||
}
|
||||
switch platform {
|
||||
case "twitter":
|
||||
runAuth(cfgPath)
|
||||
case "linkedin":
|
||||
runLinkedInAuth(cfgPath)
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown platform %q. Use twitter or linkedin.\n", platform)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
cfgPath = os.Args[1]
|
||||
}
|
||||
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Copy config.yaml and fill in your credentials.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// If Twitter is configured but has no access token, run auth before the TUI.
|
||||
if cfg.Credentials.Twitter.ClientID != "" && !cfg.HasTwitterPost() {
|
||||
fmt.Println("No Twitter access token found. Starting authorization...")
|
||||
runAuth(cfgPath)
|
||||
cfg, err = config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve data file paths relative to the config file so they're always
|
||||
// written to the same place regardless of where the binary is invoked from.
|
||||
cfgDir := filepath.Dir(cfgPath)
|
||||
|
||||
statePath := cfg.Settings.StateFile
|
||||
if !filepath.IsAbs(statePath) {
|
||||
statePath = filepath.Join(cfgDir, statePath)
|
||||
}
|
||||
|
||||
postsPath := filepath.Join(cfgDir, "posts.json")
|
||||
|
||||
st, err := state.Load(statePath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not load state file: %v\n", err)
|
||||
st, _ = state.Load(statePath)
|
||||
}
|
||||
|
||||
ps, err := store.Load(postsPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not load posts file: %v\n", err)
|
||||
ps, _ = store.Load(postsPath)
|
||||
}
|
||||
|
||||
// Build API clients — nil if credentials are missing
|
||||
var twClient *twitter.Client
|
||||
if cfg.HasTwitterRead() {
|
||||
twClient = twitter.New(
|
||||
cfg.Credentials.Twitter.BearerToken,
|
||||
cfg.Credentials.Twitter.OAuth2AccessToken,
|
||||
cfg.Settings.MaxResults,
|
||||
)
|
||||
}
|
||||
|
||||
var ytClient *youtube.Client
|
||||
if cfg.HasYouTube() {
|
||||
ytClient = youtube.New(cfg.Credentials.YouTube.APIKey, cfg.Settings.MaxResults)
|
||||
}
|
||||
|
||||
f := fetcher.New(twClient, ytClient)
|
||||
|
||||
cl := claudeapi.New(
|
||||
cfg.Credentials.Claude.APIKey,
|
||||
cfg.Credentials.Claude.Model,
|
||||
cfgDir,
|
||||
cfg.Posting.MaxLength,
|
||||
)
|
||||
|
||||
var poster twitterPoster
|
||||
if cfg.HasTwitterPost() {
|
||||
poster = twClient
|
||||
}
|
||||
|
||||
var liPoster linkedinPoster
|
||||
if cfg.HasLinkedInPost() {
|
||||
liPoster = linkedinapi.New(
|
||||
cfg.Credentials.LinkedIn.AccessToken,
|
||||
cfg.Credentials.LinkedIn.AuthorURN,
|
||||
)
|
||||
}
|
||||
|
||||
model := ui.New(cfg, st, ps, f, cl, poster, liPoster)
|
||||
|
||||
p := tea.NewProgram(model, tea.WithAltScreen(), tea.WithMouseCellMotion())
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// twitterPoster is the subset of twitter.Client used for posting.
|
||||
type twitterPoster interface {
|
||||
PostTweet(ctx context.Context, text string) (string, error)
|
||||
}
|
||||
|
||||
// linkedinPoster is the subset of linkedin.Client used for posting.
|
||||
type linkedinPoster interface {
|
||||
PostUpdate(ctx context.Context, text, sourceURL string) (string, error)
|
||||
}
|
||||
|
||||
func runLinkedInAuth(cfgPath string) {
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg.Credentials.LinkedIn.ClientID == "" || cfg.Credentials.LinkedIn.ClientSecret == "" {
|
||||
fmt.Fprintf(os.Stderr, "Error: credentials.linkedin.client_id and client_secret must be set in %s\n", cfgPath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
result, err := auth.LinkedInOAuth2(context.Background(),
|
||||
cfg.Credentials.LinkedIn.ClientID,
|
||||
cfg.Credentials.LinkedIn.ClientSecret,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Authorization failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
authorURN, err := linkedinapi.GetAuthorURN(context.Background(), result.AccessToken)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not fetch author URN: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Authorized as %s\n", authorURN)
|
||||
|
||||
if err := config.UpdateLinkedInCredentials(cfgPath, result.AccessToken, authorURN); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not save credentials: %v\n", err)
|
||||
fmt.Printf("Add manually to %s:\n access_token: \"%s\"\n author_urn: \"%s\"\n", cfgPath, result.AccessToken, authorURN)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Success! LinkedIn credentials saved to %s\n", cfgPath)
|
||||
}
|
||||
|
||||
func runAuth(cfgPath string) {
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if cfg.Credentials.Twitter.ClientID == "" {
|
||||
fmt.Fprintf(os.Stderr, "Error: credentials.twitter.client_id must be set in %s\n", cfgPath)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
result, err := auth.TwitterOAuth2(context.Background(),
|
||||
cfg.Credentials.Twitter.ClientID,
|
||||
cfg.Credentials.Twitter.ClientSecret,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Authorization failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Verify the token and show which account was authorized.
|
||||
twClient := twitter.New("", result.AccessToken, 0)
|
||||
username, err := twClient.VerifyCredentials(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not verify token: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("Authorized as @%s\n", username)
|
||||
}
|
||||
|
||||
if err := config.UpdateOAuth2Token(cfgPath, result.AccessToken); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not save token automatically: %v\n", err)
|
||||
fmt.Printf("\nAdd this to %s manually:\n oauth2_access_token: \"%s\"\n", cfgPath, result.AccessToken)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Success! oauth2_access_token saved to %s\n", cfgPath)
|
||||
}
|
||||
Reference in New Issue
Block a user