-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
executable file
·64 lines (58 loc) · 1.6 KB
/
main.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
// Copyright (C) 2019 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"../../common"
)
func dockerSetDescrs(token string, image string, description string, fullDescription string) error {
reqData := map[string]string{
"registry": "registry-1.docker.io",
"description": description,
"full_description": fullDescription,
}
jsonReq, err := json.MarshalIndent(reqData, "", "")
if err != nil {
return err
}
baseUrl := "https://cloud.docker.com/v2/repositories"
url := fmt.Sprintf("%s/%s/", baseUrl, image)
req, err := http.NewRequest("PATCH", url, strings.NewReader(string(jsonReq)))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("JWT %s", token))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() {
err := resp.Body.Close()
common.PanicIf(err)
}()
if resp.StatusCode != 200 {
return fmt.Errorf("status: %d", resp.StatusCode)
}
return nil
}
func cmdLineParse() (string, string, string) {
if len(os.Args) != 4 {
panic(fmt.Sprintf("could not parse %s", os.Args))
}
return os.Args[1], os.Args[2], os.Args[3]
}
func main() {
image, description, fullDescription := cmdLineParse()
token := os.Getenv("DOCKERHUB_TOKEN")
err := dockerSetDescrs(token, image, description, fullDescription)
if err != nil {
panic(fmt.Sprintf("error setting descriptions: %s", err))
}
}