-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathupdater_example_test.go
99 lines (83 loc) · 3.12 KB
/
updater_example_test.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
package updater_test
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/kinvolk/nebraska/updater"
)
func someFunctionThatDownloadsAFile(ctx context.Context, url string) (string, error) {
// Download file logic goes here
return "/tmp/downloads/examplefile.txt", nil
}
func someFunctionThatExtractsTheUpdateAndInstallIt(ctx context.Context, filePath string) error {
// Extract and install update logic goes here
return nil
}
// ExampleUpdater shows how to use the updater package to
// update an application manually.
func ExampleUpdater() error {
conf := updater.Config{
OmahaURL: "http://test.omahaserver.com/v1/update/",
AppID: "application_id",
Channel: "stable",
InstanceID: uuid.NewString(),
InstanceVersion: "0.0.1",
}
appUpdater, err := updater.New(conf)
if err != nil {
return fmt.Errorf("init updater: %w", err)
}
ctx := context.TODO()
updateInfo, err := appUpdater.CheckForUpdates(ctx)
if err != nil {
return fmt.Errorf("checking updates for app: %q, err: %w", conf.AppID, err)
}
if !updateInfo.HasUpdate {
return fmt.Errorf("No update exists for the application")
}
// So we got an update, let's report we'll start downloading it.
if err := appUpdater.ReportProgress(ctx, updater.ProgressDownloadStarted); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("Reporting progress error:", progressErr)
}
return fmt.Errorf("reporting download started: %w", err)
}
// This should be implemented by the caller.
filePath, err := someFunctionThatDownloadsAFile(ctx, updateInfo.URL())
if err != nil {
// Oops something went wrong.
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting error:", progressErr)
}
return fmt.Errorf("downloading update: %w", err)
}
// The download was successful, let's inform that to the Omaha server.
if err := appUpdater.ReportProgress(ctx, updater.ProgressDownloadFinished); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("Reporting progress error:", progressErr)
}
return fmt.Errorf("reporting download finished: %w", err)
}
// We got our update file, let's install it!
if err := appUpdater.ReportProgress(ctx, updater.ProgressInstallationStarted); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
return fmt.Errorf("reporting installation started: %w", err)
}
// This should be your own implementation.
if err := someFunctionThatExtractsTheUpdateAndInstallIt(ctx, filePath); err != nil {
// Oops something went wrong.
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("Reporting error:", progressErr)
}
return fmt.Errorf("applying update: %w", err)
}
if err := appUpdater.CompleteUpdate(ctx, updateInfo); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
return fmt.Errorf("reporting complete update: %w", err)
}
return nil
}