-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
160 lines (133 loc) · 3.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2018-2022, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package updater
import (
"errors"
logger "log"
"os"
"path/filepath"
"runtime"
"github.com/sirupsen/logrus"
)
// Config configures the updater
type Config struct {
TargetFile string
SourceRepo string
Version string
CurrentVersion string
PublicKey []byte
Log logrus.StdLogger
OperatingSystem string
Architecture string
Downloader Downloader
}
func newConfig(opts ...Option) (*Config, error) {
c := &Config{
TargetFile: os.Args[0],
Architecture: runtime.GOARCH,
OperatingSystem: runtime.GOOS,
}
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}
if c.Downloader == nil {
err := DownloadMethod(&HTTPDownloader{})(c)
if err != nil {
return nil, err
}
}
if c.Log == nil {
c.Log = logger.New(os.Stdout, "updater:", 0)
}
err := c.Validate()
if err != nil {
return nil, err
}
return c, nil
}
// Validate confirms that the configuration meets the minimum requirements
func (c *Config) Validate() error {
if c.TargetFile == "" {
return errors.New("no target file specified, please use TargetFile()")
}
if c.SourceRepo == "" {
return errors.New("no source repo given, please use SourceRepo()")
}
if c.Version == "" {
return errors.New("no version given, please use Version()")
}
if c.Log == nil {
return errors.New("no logger given, please use Logger()")
}
return nil
}
// Option is a function that configures the auto updater
type Option func(*Config) error
// Arch overrides the architecture to update to
func Arch(a string) Option {
return func(c *Config) error {
c.Architecture = a
return nil
}
}
// OS overrides the OS to update to
func OS(os string) Option {
return func(c *Config) error {
c.OperatingSystem = os
return nil
}
}
// CurrentVersion sets the current version of the app and allows for an early exit if update is not needed, always updates when not set
func CurrentVersion(v string) Option {
return func(c *Config) error {
c.CurrentVersion = v
return nil
}
}
// Logger sets the logger to use during updates
func Logger(l logrus.StdLogger) Option {
return func(c *Config) error {
c.Log = l
return nil
}
}
// PublicKey sets the public key to use
func PublicKey(k []byte) Option {
return func(c *Config) error {
c.PublicKey = k
return nil
}
}
// TargetFile sets the file to update
func TargetFile(f string) Option {
return func(c *Config) error {
var err error
c.TargetFile, err = filepath.Abs(f)
return err
}
}
// Version sets the version to deploy
func Version(v string) Option {
return func(c *Config) error {
c.Version = v
return nil
}
}
// SourceRepo where the releases are found
func SourceRepo(r string) Option {
return func(c *Config) error {
c.SourceRepo = r
return nil
}
}
// DownloadMethod configures a downloader to use
func DownloadMethod(m Downloader) Option {
return func(c *Config) error {
c.Downloader = m
return c.Downloader.Configure(c)
}
}