• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / create-artifact-worker / 1806891426

08 May 2025 08:07AM UTC coverage: 15.957%. Remained the same
1806891426

Pull #209

gitlab-ci

alfrunes
chore: :broom: Clean up use of deprecated `io/ioutil`

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #209: :building_construction: Dependency updates

0 of 3 new or added lines in 2 files covered. (0.0%)

75 of 470 relevant lines covered (15.96%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/client/s3.go
1
// Copyright 2022 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package client
15

16
import (
17
        "context"
18
        "crypto/tls"
19
        "fmt"
20
        "io"
21
        "net/http"
22
        "os"
23

24
        "github.com/pkg/errors"
25
)
26

27
type Storage interface {
28
        Download(ctx context.Context, url, path string) error
29
        Delete(ctx context.Context, url string) error
30
}
31

32
type storage struct {
33
        c *http.Client
34
}
35

36
func NewStorage(skipSsl bool) Storage {
×
37
        tr := &http.Transport{
×
38
                TLSClientConfig: &tls.Config{
×
39
                        InsecureSkipVerify: skipSsl,
×
40
                },
×
41
        }
×
42

×
43
        c := &http.Client{
×
44
                Transport: tr,
×
45
        }
×
46

×
47
        return &storage{
×
48
                c: c,
×
49
        }
×
50
}
×
51

52
func (s *storage) Download(ctx context.Context, url, path string) error {
×
53
        ctx, cancel := context.WithTimeout(ctx, timeoutSec)
×
54
        defer cancel()
×
55

×
56
        req, err := http.NewRequest(http.MethodGet, url, nil)
×
57
        if err != nil {
×
58
                return err
×
59
        }
×
60

61
        req = req.WithContext(ctx)
×
62

×
63
        res, err := s.c.Do(req)
×
64
        if err != nil {
×
65
                return err
×
66
        }
×
67
        defer res.Body.Close()
×
68

×
69
        if res.StatusCode != http.StatusOK {
×
70
                var body string
×
71

×
NEW
72
                bbody, err := io.ReadAll(res.Body)
×
73
                if err != nil {
×
74
                        body = "<failed to read body>"
×
75
                } else {
×
76
                        body = string(bbody)
×
77
                }
×
78

79
                return errors.New(fmt.Sprintf(
×
80
                        "failed to download artifact at url %s, http %d, response: \n %s",
×
81
                        url,
×
82
                        res.StatusCode,
×
83
                        body,
×
84
                ))
×
85
        }
86

87
        out, err := os.Create(path)
×
88
        if err != nil {
×
89
                return err
×
90
        }
×
91
        defer out.Close()
×
92

×
93
        _, err = io.Copy(out, res.Body)
×
94
        return err
×
95
}
96

97
func (s *storage) Delete(ctx context.Context, url string) error {
×
98
        ctx, cancel := context.WithTimeout(ctx, timeoutSec)
×
99
        defer cancel()
×
100

×
101
        req, err := http.NewRequest(http.MethodDelete, url, nil)
×
102
        if err != nil {
×
103
                return err
×
104
        }
×
105

106
        req = req.WithContext(ctx)
×
107

×
108
        res, err := s.c.Do(req)
×
109
        if err != nil {
×
110
                return err
×
111
        }
×
112
        defer res.Body.Close()
×
113

×
114
        if res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusAccepted {
×
115
                var body string
×
116

×
NEW
117
                bbody, err := io.ReadAll(res.Body)
×
118
                if err != nil {
×
119
                        body = "<failed to read body>"
×
120
                } else {
×
121
                        body = string(bbody)
×
122
                }
×
123

124
                return errors.New(fmt.Sprintf(
×
125
                        "failed to delete artifact at url %s, http %d, response: \n %s",
×
126
                        url,
×
127
                        res.StatusCode,
×
128
                        body,
×
129
                ))
×
130
        }
131

132
        return nil
×
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc