-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
139 lines (128 loc) · 3.88 KB
/
config.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/shibukawa/configdir"
"github.com/urfave/cli/v2"
)
type Config struct {
ConfigVersion int `json:"configVersion"`
Accounts []*ConfigAccount `json:"accounts"`
// add the fields from previous versions here so that we can migrate them
LegacyAuthToken string `json:"authToken,omitempty"`
LegacyRefreshToken string `json:"refreshToken,omitempty"`
}
var ConfigKey = struct{}{}
func LoadConfig(c *cli.Context) error {
var configPath string
if c.String("config") != "" {
configPath = c.String("config")
} else {
folder := configDirs.QueryFolderContainsFile("config.json")
if folder == nil {
c.Context = context.WithValue(c.Context, ConfigKey, &Config{
ConfigVersion: 2,
Accounts: []*ConfigAccount{},
})
return nil
}
configPath = filepath.Join(folder.Path, "config.json")
}
configData, err := ioutil.ReadFile(configPath)
if err != nil {
return fmt.Errorf("failed to read config file from '%v': %w", configPath, err)
}
config := &Config{}
err = json.Unmarshal(configData, config)
if err != nil {
return fmt.Errorf("malformed %v: %w", configPath, err)
}
if config.ConfigVersion < 2 {
config = MigrateConfig(config)
c.Context = context.WithValue(c.Context, ConfigKey, config)
if err := SaveConfig(c); err != nil {
return fmt.Errorf("failed to save migrated config: %w", err)
}
return nil
}
c.Context = context.WithValue(c.Context, ConfigKey, config)
if len(c.StringSlice("phone-number")) > 0 && len(GetLimitedAccounts(c)) == 0 {
return fmt.Errorf("invalid phone number filter: %v, no accounts matching found", c.StringSlice("phone-number"))
}
return nil
}
func GetConfig(c *cli.Context) *Config {
return c.Context.Value(ConfigKey).(*Config)
}
func GetLimitedAccounts(c *cli.Context) []*ConfigAccount {
cfg := GetConfig(c)
accounts := make([]*ConfigAccount, 0)
phoneNumbersStr := c.StringSlice("phone-number")
for _, account := range cfg.Accounts {
if len(phoneNumbersStr) == 0 {
accounts = append(accounts, account)
continue
}
for _, phoneNumber := range phoneNumbersStr {
if account.PhoneNumber == phoneNumber {
accounts = append(accounts, account)
break
}
}
}
return accounts
}
func MigrateConfig(c *Config) *Config {
c.ConfigVersion = 2
if c.Accounts == nil {
c.Accounts = []*ConfigAccount{}
}
if c.LegacyAuthToken != "" && c.LegacyRefreshToken != "" {
tok, _ := jwt.ParseWithClaims(strings.TrimPrefix(c.LegacyAuthToken, "Bearer "), &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("lolxd"), nil
})
claims := tok.Claims.(*jwt.StandardClaims)
c.Accounts = append(c.Accounts, &ConfigAccount{
PhoneNumber: claims.Subject,
AuthToken: c.LegacyAuthToken,
RefreshToken: c.LegacyRefreshToken,
})
}
c.LegacyAuthToken = ""
c.LegacyRefreshToken = ""
log.Printf("Migrated config to version 2")
return c
}
func SaveConfig(c *cli.Context) error {
cfg := c.Context.Value(ConfigKey).(*Config)
configPath := c.String("config")
if configPath == "" {
folder := configDirs.QueryFolderContainsFile("config.json")
if folder != nil {
configPath = filepath.Join(folder.Path, "config.json")
} else {
configPath = filepath.Join(configDirs.QueryFolders(configdir.Global)[0].Path, "config.json")
}
} else {
configPath = filepath.Join(configPath, "config.json")
}
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
configData, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal config: %w", err)
}
if err := ioutil.WriteFile(configPath, configData, 0644); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
log.Printf("Saved config to %v", configPath)
return nil
}