Stop committing credentials; fix posting length, partial posts, and data safety

- Untrack config.yaml (now gitignored) and add config.example.yaml with
  blank credentials; install.sh copies the example and sets mode 600
- Reserve room for the appended source link when asking Claude for a
  post, count length the way X does, and refuse over-length tweets
  before posting
- Return https://x.com/i/status/<id> for posted tweets
- Track which platforms each queued post reached so a partial failure
  can be retried without double-posting
- Write config, state, posts and dismissed files atomically with 0600
- Keep refreshed Twitter tokens in memory if saving them fails, and log
  the failure instead of writing to the hidden stderr
- Exit with a clear message on corrupt data files instead of panicking
- Surface save errors in the UI; show status/error messages on home
- Add tests for X length counting, the post store, and token updates

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 06:14:40 -07:00
co-authored by Claude Opus 5.5
parent c48ee22f99
commit 4d304cd65b
18 changed files with 391 additions and 96 deletions
+37 -15
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"
tea "github.com/charmbracelet/bubbletea"
@@ -64,13 +65,17 @@ func main() {
if tw.OAuth2RefreshToken != "" {
result, err := auth.RefreshTwitterToken(context.Background(), tw.ClientID, tw.ClientSecret, tw.OAuth2RefreshToken)
if err == nil {
if saveErr := config.UpdateOAuth2Tokens(cfgPath, result.AccessToken, result.RefreshToken); saveErr != nil {
fmt.Fprintf(os.Stderr, "Warning: could not save refreshed token: %v\n", saveErr)
// Use the new tokens from memory rather than reloading the file,
// so a failed save doesn't also lose them for this session.
cfg.Credentials.Twitter.OAuth2AccessToken = result.AccessToken
if result.RefreshToken != "" {
cfg.Credentials.Twitter.OAuth2RefreshToken = result.RefreshToken
}
cfg, err = config.Load(cfgPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reloading config: %v\n", err)
os.Exit(1)
if saveErr := config.UpdateOAuth2Tokens(cfgPath, result.AccessToken, result.RefreshToken); saveErr != nil {
// X refresh tokens are single-use: the old one in the file is
// now dead, so the next launch will need `coremarketing auth twitter`.
fmt.Fprintf(os.Stderr, "Warning: could not save refreshed token to %s: %v\n", cfgPath, saveErr)
fmt.Fprintf(os.Stderr, "You will need to run `coremarketing auth twitter` next time.\n")
}
} else {
fmt.Printf("Token refresh failed (%v), re-authorizing...\n", err)
@@ -104,22 +109,21 @@ func main() {
postsPath := filepath.Join(cfgDir, "posts.json")
dismissedPath := filepath.Join(cfgDir, "dismissed.json")
// Refuse to start on an unreadable data file: carrying on with an empty
// store would overwrite it (and lose its contents) on the next save.
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)
dataFileError(statePath, err)
}
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)
dataFileError(postsPath, err)
}
ds, err := store.LoadDismissed(dismissedPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not load dismissed file: %v\n", err)
ds, _ = store.LoadDismissed(dismissedPath)
dataFileError(dismissedPath, err)
}
// Build API clients — nil if credentials are missing
@@ -137,10 +141,13 @@ func main() {
if err != nil {
return "", "", err
}
if result.RefreshToken != "" {
currentRefreshToken = result.RefreshToken
}
if saveErr := config.UpdateOAuth2Tokens(cfgPath, result.AccessToken, result.RefreshToken); saveErr != nil {
fmt.Fprintf(os.Stderr, "Warning: could not save refreshed token: %v\n", saveErr)
// stderr is hidden behind the TUI's alt screen, so log it.
appendLog(cfgDir, "could not save refreshed Twitter token to %s: %v — run `coremarketing auth twitter` before next launch", cfgPath, saveErr)
}
currentRefreshToken = result.RefreshToken
return result.AccessToken, result.RefreshToken, nil
})
}
@@ -157,7 +164,6 @@ func main() {
cfg.Credentials.Claude.APIKey,
cfg.Credentials.Claude.Model,
cfgDir,
cfg.Posting.MaxLength,
)
var poster twitterPoster
@@ -264,3 +270,19 @@ func runAuth(cfgPath string) {
fmt.Printf("Success! oauth2_access_token saved to %s\n", cfgPath)
}
func dataFileError(path string, err error) {
fmt.Fprintf(os.Stderr, "Error: could not load %s: %v\n", path, err)
fmt.Fprintf(os.Stderr, "Fix or move the file aside, then start again.\n")
os.Exit(1)
}
// appendLog writes a timestamped line to coremarketing.log in dir.
func appendLog(dir, format string, args ...any) {
f, err := os.OpenFile(filepath.Join(dir, "coremarketing.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return
}
defer f.Close()
fmt.Fprintf(f, "[%s] %s\n", time.Now().Format(time.RFC3339), fmt.Sprintf(format, args...))
}