/
update.go
182 lines (146 loc) · 4.06 KB
/
update.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmd
import (
"encoding/json"
"fmt"
"github.com/n1ckl0sk0rtge/cpm/command"
"github.com/n1ckl0sk0rtge/cpm/config"
"github.com/n1ckl0sk0rtge/cpm/cruntime"
"github.com/n1ckl0sk0rtge/cpm/helper"
"github.com/spf13/cobra"
"os"
"os/exec"
"strings"
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update [command]",
Args: cobra.MaximumNArgs(1),
Short: "Update a specific or all images related to commands",
Long: `
Update a specific or all images related to commands.
If you not set a specific command to update, all images will be updated to
the latest version. That will only include images with a tag that could change
over time. In case an image is referenced by digest it will not be updated.
`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := cruntime.Available(); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
updateAll()
} else {
command := args[0]
updateCommand(command)
}
},
}
func init() {
rootCmd.AddCommand(updateCmd)
}
func updateAll() {
data := GetCommands()
for _, value := range data {
name := value[0]
updateCommand(name)
}
}
type imageInspect []struct {
Id string `json:"Id"`
RepoDigests []string `json:"RepoDigests"`
}
type manifest struct {
Manifests []struct {
Digest string `json:"digest"`
Platform struct {
Architecture string `json:"architecture"`
OS string `json:"os"`
} `json:"platform"`
} `json:"manifests"`
}
func updateCommand(c string) {
if !command.Exists(c, config.GetConfigProperties()) {
err := fmt.Errorf("command does not exists")
fmt.Println(err)
return
}
output := fmt.Sprintf("Check for updates for %s...", c)
fmt.Println(output)
maybeCommandConfig := command.ReadConfig(c, config.GetConfigProperties())
if maybeCommandConfig == nil {
err := fmt.Errorf("could not read/found command command config")
fmt.Println(err)
return
}
commandConfig := *maybeCommandConfig
var imageRef string
if strings.Contains(commandConfig[command.Tag], "sha") {
fmt.Println("No updates for images referenced by digest. Skip")
return
} else {
imageRef = commandConfig[command.Image] + ":" + commandConfig[command.Tag]
}
helper.Dprintln(imageRef)
getCurrentDigestCommand :=
fmt.Sprintf("%s inspect %s", config.Instance.GetString(config.Runtime), imageRef)
currentDigest, err := exec.Command("sh", "-c", getCurrentDigestCommand).Output()
if err != nil {
fmt.Println(err)
return
}
var imageInspect imageInspect
err = json.Unmarshal(currentDigest, &imageInspect)
if err != nil {
fmt.Println(err)
return
}
digests := imageInspect[0].RepoDigests
helper.DprintlnSlice(digests)
// fetch remote digest
getRemoteDigestsCommand :=
fmt.Sprintf("%s manifest inspect %s", config.Instance.Get(config.Runtime), imageRef)
remoteDigests, err := exec.Command("sh", "-c", getRemoteDigestsCommand).Output()
if err != nil {
fmt.Println(err)
return
}
var manifest manifest
err = json.Unmarshal(remoteDigests, &manifest)
if err != nil {
fmt.Println(err)
return
}
// TODO check for different os/arch
remoteDigest := manifest.Manifests[0].Digest
helper.Dprintln(remoteDigest)
localDigest := ""
for _, repoDigest := range imageInspect[0].RepoDigests {
if strings.Contains(repoDigest, remoteDigest) {
localDigest = repoDigest
}
}
helper.Dprintln(localDigest)
if len(localDigest) == 0 {
output = fmt.Sprintf("Update availabe for %s!", c)
fmt.Println(output)
output = fmt.Sprintf("=> Download new version %s@%s", commandConfig[command.Image], remoteDigest)
fmt.Println(output)
pullNewVersionCommand :=
fmt.Sprintf("%s pull %s", config.Instance.Get(config.Runtime), imageRef)
helper.Dprintln(pullNewVersionCommand)
pullCommand := exec.Command("sh", "-c", pullNewVersionCommand)
_, err := pullCommand.Output()
if err != nil {
fmt.Println(err)
return
}
output = fmt.Sprintf("%s updated successfuly", c)
fmt.Println(output)
} else {
output = fmt.Sprintf("%s is up to date", c)
fmt.Println(output)
return
}
}