From 547afd8c0921a7de16f0a5ce8b5d8d09bb2268c8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 26 Feb 2025 15:24:33 +0200 Subject: introduce run interval --- internal/config/args.go | 33 +++++++++---------- internal/config/config.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++ internal/config/secrets.go | 69 ---------------------------------------- 3 files changed, 96 insertions(+), 85 deletions(-) create mode 100644 internal/config/config.go delete mode 100644 internal/config/secrets.go (limited to 'internal/config') diff --git a/internal/config/args.go b/internal/config/args.go index e2b9dc1..d1a23b9 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -9,22 +9,23 @@ import ( ) type Args struct { - GosDir string - CacheDir string - DryRun bool - Platforms map[string]int // Platform and post size limits - Target int - MinQueued int - MaxDaysQueued int - PauseDays int - Lookback time.Duration - SecretsConfigPath string - Secrets Secrets - OAuth2Browser string - GeminiSummaryFor []string - GemtexterEnable bool - GeminiCapsules []string - ComposeMode bool + GosDir string + CacheDir string + DryRun bool + Platforms map[string]int // Platform and post size limits + Target int + MinQueued int + MaxDaysQueued int + PauseDays int + RunInterval time.Duration + Lookback time.Duration + ConfigPath string + Config Config + OAuth2Browser string + GeminiSummaryFor []string + GemtexterEnable bool + GeminiCapsules []string + ComposeMode bool } func (a *Args) ParsePlatforms(platformStrs string) error { diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..f865b67 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,79 @@ +package config + +import ( + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + + "codeberg.org/snonux/gos/internal/colour" +) + +// The config file containing all the secrets and credentials plus maybe more. +type Config struct { + LastRunEpoch int64 `json:"LastRunEpoch,omitempty"` + MastodonURL string + MastodonAccessToken string + LinkedInClientID string + LinkedInSecret string + LinkedInRedirectURL string + // Will be updated by gos automatically, after successful oauth2 + LinkedInAccessToken string `json:"LinkedInAccessToken,omitempty"` + // Will be updated by gos automatically, after successful oauth2 + LinkedInPersonID string `json:"LinkedInPersonID,omitempty"` +} + +func New(configPath string, composeEntry bool) (Config, error) { + var conf Config + + _, err := os.Stat(configPath) + if os.IsNotExist(err) { + if !composeEntry { + return conf, fmt.Errorf("No config file %s", configPath) + } + // Create empty new config for compose mode. + return conf, conf.WriteToDisk(configPath) + } + + file, err := os.Open(configPath) + if err != nil { + return conf, fmt.Errorf("failed to open file: %w", err) + } + defer file.Close() + + bytes, err := io.ReadAll(file) + if err != nil { + return conf, fmt.Errorf("failed to read file: %w", err) + } + + if err := json.Unmarshal(bytes, &conf); err != nil { + return conf, fmt.Errorf("failed to unmarshal JSON: %w", err) + } + + return conf, nil +} + +func (s Config) WriteToDisk(configPath string) error { + colour.Infoln("Writing", configPath) + if err := os.MkdirAll(filepath.Dir(configPath), os.ModePerm); err != nil { + return err + } + + bytes, err := json.MarshalIndent(s, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal JSON: %w", err) + } + tmpConfigPath := fmt.Sprintf("%s.tmp", configPath) + file, err := os.Create(tmpConfigPath) + if err != nil { + return fmt.Errorf("failed to create file: %w", err) + } + defer file.Close() + + if _, err := file.Write(bytes); err != nil { + return fmt.Errorf("failed to write to file: %w", err) + } + + return os.Rename(tmpConfigPath, configPath) +} diff --git a/internal/config/secrets.go b/internal/config/secrets.go deleted file mode 100644 index b4e629f..0000000 --- a/internal/config/secrets.go +++ /dev/null @@ -1,69 +0,0 @@ -package config - -import ( - "encoding/json" - "fmt" - "io" - "os" - - "codeberg.org/snonux/gos/internal/colour" -) - -// The config file containing all the secrets and credentials. -type Secrets struct { - MastodonURL string - MastodonAccessToken string - LinkedInClientID string - LinkedInSecret string - LinkedInRedirectURL string - // Will be updated by gos automatically, after successful oauth2 - LinkedInAccessToken string `json:"LinkedInAccessToken,omitempty"` - // Will be updated by gos automatically, after successful oauth2 - LinkedInPersonID string `json:"LinkedInPersonID,omitempty"` -} - -func NewSecrets(configPath string, composeEntry bool) (Secrets, error) { - var sec Secrets - if composeEntry { - // In compose mode, no need to read the secrets. - return sec, nil - } - - file, err := os.Open(configPath) - if err != nil { - return sec, fmt.Errorf("failed to open file: %w", err) - } - defer file.Close() - - bytes, err := io.ReadAll(file) - if err != nil { - return sec, fmt.Errorf("failed to read file: %w", err) - } - - if err := json.Unmarshal(bytes, &sec); err != nil { - return sec, fmt.Errorf("failed to unmarshal JSON: %w", err) - } - - return sec, nil -} - -func (s Secrets) WriteToDisk(configPath string) error { - colour.Infoln("Writing", configPath) - - bytes, err := json.MarshalIndent(s, "", " ") - if err != nil { - return fmt.Errorf("failed to marshal JSON: %w", err) - } - tmpConfigPath := fmt.Sprintf("%s.tmp", configPath) - file, err := os.Create(tmpConfigPath) - if err != nil { - return fmt.Errorf("failed to create file: %w", err) - } - defer file.Close() - - if _, err := file.Write(bytes); err != nil { - return fmt.Errorf("failed to write to file: %w", err) - } - - return os.Rename(tmpConfigPath, configPath) -} -- cgit v1.2.3