This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
259 lines (198 loc) · 5.4 KB
/
app.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"time"
"github.com/exograd/go-program"
"github.com/google/go-github/v40/github"
)
type App struct {
Config *Config
Client *Client
HTTPClient *http.Client
UserAgent string
HomePath string
projectIdOption *string
projectNameOption *string
}
func NewApp(config *Config, client *Client) (*App, error) {
a := &App{
Config: config,
Client: client,
HTTPClient: NewHTTPClient(),
}
a.UserAgent = fmt.Sprintf("evcli/%s (%s; %s)",
buildId, runtime.GOOS, runtime.GOARCH)
homePath, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("cannot locate user home directory: %w", err)
}
a.HomePath = homePath
client.httpClient = a.HTTPClient
return a, nil
}
func (a *App) LoadAPIKey() {
if key := os.Getenv("EVENTLINE_API_KEY"); key != "" {
p.Debug(1, "using api key from EVENTLINE_API_KEY environment variable")
a.Client.APIKey = key
return
}
if key := a.Config.API.Key; key != "" {
p.Debug(1, "using api key from configuration")
a.Client.APIKey = key
return
}
p.Error("missing or empty API key")
p.Info("\nYou need to provide an API key to interact with Eventline. " +
"You can either edit the evcli configuration file or use the " +
"following command:")
p.Info("\n\tevcli set-config api.key <key>")
p.Info("\nAlternatively, you can set the EVENTLINE_API_KEY environment " +
"variable.")
os.Exit(1)
}
func (a *App) IdentifyCurrentProject() {
id, err := a.identifyCurrentProject()
if err != nil {
p.Fatal("%v", err)
}
p.Debug(1, "using project %s as current project", id)
a.Client.ProjectId = id
}
func (a *App) identifyCurrentProject() (string, error) {
if a.projectIdOption != nil {
return *a.projectIdOption, nil
}
if a.projectNameOption != nil {
name := *a.projectNameOption
project, err := a.Client.FetchProjectByName(name)
if err != nil {
p.Fatal("cannot fetch project %q: %v", name, err)
}
return project.Id, nil
}
if id := os.Getenv("EVENTLINE_PROJECT_ID"); id != "" {
return id, nil
}
if name := os.Getenv("EVENTLINE_PROJECT_NAME"); name != "" {
project, err := a.Client.FetchProjectByName(name)
if err != nil {
p.Fatal("cannot fetch project %q: %v", name, err)
}
return project.Id, nil
}
id, err := a.loadProjectDirectory(".")
if err != nil {
return "", err
} else if id != "" {
return id, nil
}
return "", fmt.Errorf("cannot identify the current project")
}
func (a *App) loadProjectDirectory(dirPath string) (string, error) {
var projectFile ProjectFile
if err := projectFile.Read(dirPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", err
}
return projectFile.Id, nil
}
func (a *App) LookForLastBuild() {
lastCheck, err := a.loadLastBuildIdCheckDate()
if err != nil {
p.Error("cannot look for last build: %v", err)
}
now := time.Now()
if lastCheck != nil && now.Sub(*lastCheck) < 24*time.Hour {
return
}
lastBuildId, err := a.lookForLastBuild()
if err != nil {
p.Error("cannot find last build: %v", err)
return
}
if err := a.updateLastBuildIdCheckDate(now); err != nil {
p.Error("cannot update last build check date: %v", err)
}
if lastBuildId == nil {
p.Debug(1, "evcli is up-to-date")
return
}
p.Info("evcli %v is now available: run \"evcli update\" to install it", lastBuildId)
}
func (a *App) lookForLastBuild() (*program.BuildId, error) {
p.Debug(1, "looking for the last build")
currentBuildId := a.currentBuildId()
lastBuildId, err := a.lastBuildId()
if err != nil {
return nil, fmt.Errorf("cannot retrieve last build id: %w", err)
} else if lastBuildId == nil {
return nil, nil
}
if lastBuildId.LowerThanOrEqualTo(currentBuildId) {
return nil, nil
}
return lastBuildId, nil
}
func (a *App) currentBuildId() program.BuildId {
var id program.BuildId
id.Parse(buildId)
return id
}
func (a *App) lastBuildId() (*program.BuildId, error) {
httpClient := a.HTTPClient
client := github.NewClient(httpClient)
ctx := context.Background()
release, _, err := client.Repositories.GetLatestRelease(ctx,
"exograd", "evcli")
if err != nil {
return nil, fmt.Errorf("cannot fetch latest release: %w", err)
}
tagName := release.GetTagName()
var buildId program.BuildId
if err := buildId.Parse(tagName); err != nil {
return nil, fmt.Errorf("invalid build id %q: %w", tagName, err)
}
return &buildId, nil
}
func (a *App) loadLastBuildIdCheckDate() (*time.Time, error) {
filePath := a.lastBuildIdCheckPath()
data, err := ioutil.ReadFile(filePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("cannot read %s: %w", filePath, err)
}
i, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return nil, fmt.Errorf("%s: invalid data", filePath)
}
t := time.Unix(i, 0)
return &t, nil
}
func (a *App) updateLastBuildIdCheckDate(t time.Time) error {
filePath := a.lastBuildIdCheckPath()
data := []byte(strconv.FormatInt(t.Unix(), 10))
dirPath := filepath.Dir(filePath)
if err := os.MkdirAll(dirPath, 0700); err != nil {
return fmt.Errorf("cannot create directory %s: %w", dirPath, err)
}
if err := ioutil.WriteFile(filePath, data, 0600); err != nil {
return fmt.Errorf("cannot write %s: %w", filePath, err)
}
return nil
}
func (a *App) lastBuildIdCheckPath() string {
return path.Join(a.HomePath, ".cache", "evcli", "last-build-id-check")
}