-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (117 loc) · 3.61 KB
/
Copy pathmain.go
File metadata and controls
152 lines (117 loc) · 3.61 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/rawnly/splash-cli/cmd"
"github.com/rawnly/splash-cli/config"
"github.com/rawnly/splash-cli/lib"
"github.com/rawnly/splash-cli/lib/keys"
"github.com/rawnly/splash-cli/unsplash"
"github.com/rawnly/splash-cli/unsplash/tokens"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func updateTopics(api *unsplash.Api) {
topics, err := api.GetTopics()
if err != nil {
return
}
viper.Set("topics.list", topics)
viper.Set("topics.last_update", time.Now().Unix())
}
func init() {
setupLogs()
viper.SetConfigType("json")
viper.SetConfigName("splash-cli")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("$HOME/.splash-cli")
config.DefaultUserConfig.LoadDefaults()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Debug().Msg("No settings file found")
// Create file if not exists
if err = viper.SafeWriteConfig(); err != nil {
cobra.CheckErr(err)
}
} else {
log.Fatal().Msgf("Error reading settings file: %s", err)
}
}
}
func main() {
coreLogger := log.With().Str("service", "core").Logger()
anaylticsLogger := log.With().Str("service", "analytics").Logger() //.With().Str("service", "analytics").Logger
clientID, clientSecret := config.GetKeys()
ctx := context.Background()
analyticsClient := setupPosthog()
api := unsplash.Api{
ClientId: clientID,
RedirectUri: "http://localhost:5835",
ClientSecret: clientSecret,
Client: http.Client{},
}
// Load settings
accessToken := tokens.GetAccessToken()
refreshToken := tokens.GetRefreshToken()
ctx = context.WithValue(ctx, keys.IsLogged, accessToken != "" && refreshToken != "")
ctx = context.WithValue(ctx, keys.APIInstance, api)
ctx = context.WithValue(ctx, keys.Analytics, analyticsClient)
go setupSentry()
defer sentry.Flush(2 * time.Second)
go updateTopics(&api)
// update photo of the day cache
go func() {
log := log.With().Str("service", "photo-of-the-day").Logger()
lastCheck := viper.GetTime("photo_of_the_day.last_update")
refreshInterval := viper.GetDuration("photo_of_the_day.refresh_interval")
if lastCheck.IsZero() {
log.Debug().Msg("no cached photo of the day timestamp found")
return
}
if time.Since(lastCheck) > refreshInterval {
log.Debug().Msg("cache expired")
viper.Set("photo_of_the_day.last_update", 0)
viper.Set("photo_of_the_day.id", "")
// fail silently
_ = viper.WriteConfig()
return
}
}()
defer func() {
anaylticsLogger.Trace().Msg("closing client")
if err := analyticsClient.Close(); err != nil {
anaylticsLogger.Error().Msgf("closing analytics client: %s", err)
}
}()
coreLogger.Trace().Str("service", "core").Msg("checking if first run")
if !viper.GetBool("has_run_before") {
coreLogger.Trace().Msg("first run detected")
viper.Set("has_run_before", true)
viper.Set("user_id", uuid.NewString())
if analyticsClient.PromptConsent() {
analyticsClient.Capture("installation", nil)
}
err := viper.WriteConfig()
cobra.CheckErr(err)
}
// check for download path and create if not exists
downloadPath := viper.GetString("download_dir")
downloadPath = lib.ExpandPath(downloadPath)
if _, err := os.Stat(downloadPath); os.IsNotExist(err) {
coreLogger.Debug().Str("download-path", downloadPath).Msg("Download path does not exist")
err := os.MkdirAll(downloadPath, 0o755)
cobra.CheckErr(err)
}
analyticsClient.Capture("app_open", nil)
cmd.Execute(ctx)
}