1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package config
import (
"encoding/json"
"fmt"
"io"
"log"
"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:"LinkedInAccessToken,omitempty"`
// Will be updated by gos automatically, after successful oauth2
LinkedInPersonID string `json:"LinkedInPersonID,omitempty"`
}
func NewSecrets(configPath string) (Secrets, error) {
var sec Secrets
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 {
log.Println("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)
}
|