forked from rsyabuta/go-aptly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishedrepo.go
158 lines (137 loc) · 3.87 KB
/
publishedrepo.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
package aptly
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)
type PublishedRepoService struct {
Client *Client
}
type PublishedRepo struct {
Storage string `json:"Storage"`
Prefix string `json:"Prefix"`
Distribution string `json:"Distribution"`
SourceKind string `json:"SourceKind"`
Sources []Source `json:"Sources"`
Architectures []string `json:"Architectures"`
Label string `json:"Label"`
Origin string `json:"Origin"`
Signing Signing `json:"Signing"`
}
type UpdateRepo struct {
Snapshots []Source `json:"Snapshots"`
ForceOverwrite bool `json:"ForceOverwrite"`
Signing Signing `json:"Signing"`
}
type Source struct {
Name string `json:"Name"`
Component string `json:"Component"`
}
type Signing struct {
Skip bool `json:"Skip"`
Batch bool `json:"Batch"`
GpgKey string `json:"GpgKey"`
Keyring string `json:"Keyring"`
SecretKeyring string `json:"SecretKeyring"`
Passphrase string `json:"Passphrase"`
PassphraseFile string `json:"PassphraseFile"`
}
type PublishedRepoCollection struct {
PublishedRepos []PublishedRepo
}
func (service *PublishedRepoService) List() (*PublishedRepoCollection, error) {
resp, err := service.Client.Get("publish")
defer resp.Body.Close()
if err != nil {
return nil, err
}
var collection PublishedRepoCollection
err = json.NewDecoder(resp.Body).Decode(&collection.PublishedRepos)
if err != nil {
return nil, err
}
return &collection, err
}
func (service *PublishedRepoService) Update(publishedrepo *PublishedRepo) (*PublishedRepo, error) {
ur := &UpdateRepo{
Snapshots: publishedrepo.Sources,
Signing: publishedrepo.Signing,
}
reqBody, err := json.Marshal(ur)
if err != nil {
return nil, err
}
resp, err := service.Client.Put(fmt.Sprintf("publish/%s/%s", publishedrepo.Prefix, publishedrepo.Distribution), nil, bytes.NewBuffer(reqBody))
defer resp.Body.Close()
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 400 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
} else if resp.StatusCode == 404 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
} else if resp.StatusCode == 500 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
}
var pr PublishedRepo
err = json.Unmarshal(body, &pr)
if err != nil {
return nil, err
}
return &pr, nil
}
func (service *PublishedRepoService) Publish(publishedrepo *PublishedRepo) (*PublishedRepo, error) {
reqBody, err := json.Marshal(publishedrepo)
if err != nil {
return nil, err
}
prefix := ""
if publishedrepo.Prefix != "" {
prefix = fmt.Sprintf("/%s", publishedrepo.Prefix)
}
resp, err := service.Client.Post(fmt.Sprintf("publish%s", prefix), "application/json", nil, bytes.NewBuffer(reqBody))
defer resp.Body.Close()
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 400 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
} else if resp.StatusCode == 404 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
} else if resp.StatusCode == 500 {
return nil, errors.New(fmt.Sprintf("aptly: %s", body))
}
var pr PublishedRepo
err = json.Unmarshal(body, &pr)
if err != nil {
return nil, err
}
return &pr, nil
}
func (service *PublishedRepoService) Drop(publishedrepo *PublishedRepo) error {
resp, err := service.Client.Delete(fmt.Sprintf("publish/%s/%s", publishedrepo.Prefix, publishedrepo.Distribution), nil, nil)
defer resp.Body.Close()
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == 400 {
return errors.New(fmt.Sprintf("aptly: %s", body))
} else if resp.StatusCode == 409 {
return errors.New(fmt.Sprintf("aptly: %s", body))
}
return nil
}