Skip to content

Commit f5be5e3

Browse files
committed
add: little refactoring
1 parent 6daec1d commit f5be5e3

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed

cmd/arcmon/main.go

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,22 @@ func (s *Server) Tick(ctx context.Context) {
9696
logrus.Errorf("Failed getting checksum: (%v)", err)
9797
continue
9898
}
99-
version, err := s.GetVersion(ctx)
100-
if err != nil {
101-
logrus.Errorf("Failed getting version: (%v)", err)
102-
continue
103-
}
10499
if s.arcdps.CheckSum == "" {
105100
logrus.Infof("Setting initial version")
106-
s.arcdps.CheckSum = check
107-
s.arcdps.Timestamp = version
101+
s.arcdps.CheckSum = check.Checksum
102+
s.arcdps.Timestamp = check.LastModified
108103
continue
109104
}
110-
if s.arcdps.CheckSum != check {
105+
if s.arcdps.CheckSum != check.Checksum {
111106
// new version
112107
if err := s.SendWebHook(ctx,
113-
fmt.Sprintf("`%s`", check),
114-
fmt.Sprintf("`%s`", version.String()),
108+
fmt.Sprintf("`%s`", check.Checksum),
109+
fmt.Sprintf("`%s`", check.LastModified.String()),
115110
); err != nil {
116111
logrus.Errorf("unable to send webhook: (%v)\n", err)
117112
}
118-
s.arcdps.CheckSum = check
119-
s.arcdps.Timestamp = version
113+
s.arcdps.CheckSum = check.Checksum
114+
s.arcdps.Timestamp = check.LastModified
120115
}
121116
case <-ctx.Done():
122117
ticker.Stop()
@@ -125,60 +120,44 @@ func (s *Server) Tick(ctx context.Context) {
125120
}
126121
}
127122

128-
func (s *Server) GetChecksum(ctx context.Context) (string, error) {
123+
// Checksum : Used to compare local cache to remote
124+
type Checksum struct {
125+
Checksum string
126+
LastModified time.Time
127+
}
128+
129+
func (s *Server) GetChecksum(ctx context.Context) (*Checksum, error) {
129130
req, err := http.NewRequestWithContext(ctx, "GET", ArcDPSCheckSumURL, nil)
130131
if err != nil {
131-
return "", err
132+
return nil, err
132133
}
133134

134135
resp, err := s.http.Do(req)
135136
if err != nil {
136-
return "", err
137+
return nil, err
137138
}
138139
defer resp.Body.Close()
139140

140141
body, err := io.ReadAll(resp.Body)
141142
if err != nil {
142-
return "", err
143+
return nil, err
143144
}
144145

145146
if resp.StatusCode > 299 {
146-
return "", fmt.Errorf("bad response from delta: (%s)", string(body))
147-
}
148-
149-
checkSumSplit := strings.Split(string(body), " ")
150-
if len(checkSumSplit) < 2 {
151-
return "", fmt.Errorf("incorrect size of checksum split")
152-
}
153-
return checkSumSplit[0], nil
154-
155-
}
156-
157-
func (s *Server) GetVersion(ctx context.Context) (time.Time, error) {
158-
req, err := http.NewRequestWithContext(ctx, "HEAD", ArcDPSDLLURL, nil)
159-
if err != nil {
160-
return time.Time{}, err
147+
return nil, fmt.Errorf("bad response from delta: (%s)", string(body))
161148
}
162149

163-
resp, err := s.http.Do(req)
150+
lastModified, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
164151
if err != nil {
165-
return time.Time{}, err
152+
return nil, fmt.Errorf("unable to parse time: (%v)", err)
166153
}
167-
defer resp.Body.Close()
168154

169-
if resp.StatusCode > 299 {
170-
body, err := io.ReadAll(resp.Body)
171-
if err != nil {
172-
return time.Time{}, err
173-
}
174-
return time.Time{}, fmt.Errorf("bad response from delta: (%s)", string(body))
175-
}
176-
lastModified, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
177-
if err != nil {
178-
return time.Time{}, fmt.Errorf("unable to parse time: (%v)", err)
155+
checkSumSplit := strings.Split(string(body), " ")
156+
if len(checkSumSplit) < 2 {
157+
return nil, fmt.Errorf("incorrect size of checksum split")
179158
}
180159

181-
return lastModified, nil
160+
return &Checksum{Checksum: checkSumSplit[0], LastModified: lastModified}, nil
182161
}
183162

184163
func (s *Server) SendWebHook(ctx context.Context, checksum, time string) error {

0 commit comments

Comments
 (0)