From 2bc4585db96da4000fbf727beadfc25af64bcd4a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 8 Oct 2024 10:32:13 +0300 Subject: initial restructure for oauth2 --- internal/config/args.go | 13 +++++++------ internal/config/secrets.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'internal/config') diff --git a/internal/config/args.go b/internal/config/args.go index b380dcd..601a2d1 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -10,12 +10,13 @@ import ( var validPlatforms = []string{"mastodon", "linkedin"} type Args struct { - GosDir string - DryRun bool - Platforms []string - Target int - Lookback time.Duration - Secrets Secrets + GosDir string + DryRun bool + Platforms []string + Target int + Lookback time.Duration + SecretsConfigPath string + Secrets Secrets } func (a Args) Validate() error { diff --git a/internal/config/secrets.go b/internal/config/secrets.go index e480e67..dc3f8e7 100644 --- a/internal/config/secrets.go +++ b/internal/config/secrets.go @@ -7,12 +7,17 @@ import ( "os" ) +// 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:"LinedInAccessToken,omitempty"` + // Will be updated by gos automatically, after successful oauth2 + LinkedInPersonID string `json:"LinedInPersonID,omitempty"` } func NewSecrets(configPath string) (Secrets, error) { @@ -34,3 +39,22 @@ func NewSecrets(configPath string) (Secrets, error) { return sec, nil } + +func (s Secrets) WriteToDisk(configPath string) error { + 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