183 lines
4.8 KiB
Go
183 lines
4.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"coremarketing/internal/models"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type TwitterCreds struct {
|
|
BearerToken string `yaml:"bearer_token"`
|
|
ClientID string `yaml:"client_id"`
|
|
ClientSecret string `yaml:"client_secret"`
|
|
OAuth2AccessToken string `yaml:"oauth2_access_token"`
|
|
}
|
|
|
|
type YouTubeCreds struct {
|
|
APIKey string `yaml:"api_key"`
|
|
}
|
|
|
|
type ClaudeCreds struct {
|
|
APIKey string `yaml:"api_key"`
|
|
Model string `yaml:"model"`
|
|
}
|
|
|
|
type LinkedInCreds struct {
|
|
ClientID string `yaml:"client_id"`
|
|
ClientSecret string `yaml:"client_secret"`
|
|
AccessToken string `yaml:"access_token"`
|
|
AuthorURN string `yaml:"author_urn"`
|
|
}
|
|
|
|
type Credentials struct {
|
|
Twitter TwitterCreds `yaml:"twitter"`
|
|
LinkedIn LinkedInCreds `yaml:"linkedin"`
|
|
YouTube YouTubeCreds `yaml:"youtube"`
|
|
Claude ClaudeCreds `yaml:"claude"`
|
|
}
|
|
|
|
type PostingConfig struct {
|
|
TwitterHandle string `yaml:"twitter_handle"`
|
|
MaxLength int `yaml:"max_length"`
|
|
}
|
|
|
|
type Settings struct {
|
|
StateFile string `yaml:"state_file"`
|
|
MaxResults int `yaml:"max_results"`
|
|
DefaultLookbackDays int `yaml:"default_lookback_days"`
|
|
}
|
|
|
|
type Config struct {
|
|
Applications []models.Application `yaml:"applications"`
|
|
Credentials Credentials `yaml:"credentials"`
|
|
Posting PostingConfig `yaml:"posting"`
|
|
Settings Settings `yaml:"settings"`
|
|
}
|
|
|
|
func Load(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading config file: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parsing config file: %w", err)
|
|
}
|
|
|
|
if err := cfg.setDefaults(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
func (c *Config) setDefaults() error {
|
|
if len(c.Applications) == 0 {
|
|
return fmt.Errorf("no applications configured in config.yaml")
|
|
}
|
|
if c.Credentials.Claude.APIKey == "" {
|
|
return fmt.Errorf("credentials.claude.api_key is required")
|
|
}
|
|
if c.Credentials.Claude.Model == "" {
|
|
c.Credentials.Claude.Model = "claude-sonnet-4-6"
|
|
}
|
|
if c.Settings.StateFile == "" {
|
|
c.Settings.StateFile = "state.json"
|
|
}
|
|
if c.Settings.MaxResults == 0 {
|
|
c.Settings.MaxResults = 20
|
|
}
|
|
if c.Settings.DefaultLookbackDays == 0 {
|
|
c.Settings.DefaultLookbackDays = 7
|
|
}
|
|
if c.Posting.MaxLength == 0 {
|
|
c.Posting.MaxLength = 280
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) HasTwitterRead() bool {
|
|
return c.Credentials.Twitter.BearerToken != ""
|
|
}
|
|
|
|
func (c *Config) HasTwitterPost() bool {
|
|
return c.Credentials.Twitter.OAuth2AccessToken != ""
|
|
}
|
|
|
|
func (c *Config) HasYouTube() bool {
|
|
return c.Credentials.YouTube.APIKey != ""
|
|
}
|
|
|
|
func (c *Config) HasLinkedInPost() bool {
|
|
l := c.Credentials.LinkedIn
|
|
return l.AccessToken != "" && l.AuthorURN != ""
|
|
}
|
|
|
|
var (
|
|
oauth2TokenRe = regexp.MustCompile(`oauth2_access_token:\s*"[^"]*"`)
|
|
liAccessTokenRe = regexp.MustCompile(`access_token:\s*"[^"]*"`)
|
|
liAuthorURNRe = regexp.MustCompile(`author_urn:\s*"[^"]*"`)
|
|
liSectionRe = regexp.MustCompile(`^ linkedin:\s*$`)
|
|
credentialSectionRe = regexp.MustCompile(`^ [a-z]`)
|
|
)
|
|
|
|
// UpdateOAuth2Token rewrites the oauth2_access_token value in the named
|
|
// config file in-place, preserving all other content and formatting.
|
|
func UpdateOAuth2Token(path, token string) error {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
updated := oauth2TokenRe.ReplaceAllLiteralString(string(data), `oauth2_access_token: "`+token+`"`)
|
|
if updated == string(data) {
|
|
return fmt.Errorf("oauth2_access_token field not found in %s", path)
|
|
}
|
|
return os.WriteFile(path, []byte(updated), 0600)
|
|
}
|
|
|
|
// UpdateLinkedInCredentials rewrites the access_token and author_urn values
|
|
// inside the linkedin credentials section, preserving all other content.
|
|
func UpdateLinkedInCredentials(path, accessToken, authorURN string) error {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
result := make([]string, len(lines))
|
|
inLinkedIn := false
|
|
tokenDone, urnDone := false, false
|
|
|
|
for i, line := range lines {
|
|
if liSectionRe.MatchString(line) {
|
|
inLinkedIn = true
|
|
result[i] = line
|
|
continue
|
|
}
|
|
if inLinkedIn && credentialSectionRe.MatchString(line) {
|
|
inLinkedIn = false
|
|
}
|
|
switch {
|
|
case inLinkedIn && !tokenDone && strings.Contains(line, "access_token:"):
|
|
result[i] = liAccessTokenRe.ReplaceAllLiteralString(line, `access_token: "`+accessToken+`"`)
|
|
tokenDone = true
|
|
case inLinkedIn && !urnDone && strings.Contains(line, "author_urn:"):
|
|
result[i] = liAuthorURNRe.ReplaceAllLiteralString(line, `author_urn: "`+authorURN+`"`)
|
|
urnDone = true
|
|
default:
|
|
result[i] = line
|
|
}
|
|
}
|
|
|
|
if !tokenDone || !urnDone {
|
|
return fmt.Errorf("linkedin credentials fields not found in %s", path)
|
|
}
|
|
return os.WriteFile(path, []byte(strings.Join(result, "\n")), 0600)
|
|
}
|