-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.go
More file actions
111 lines (89 loc) · 2.74 KB
/
update.go
File metadata and controls
111 lines (89 loc) · 2.74 KB
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
package cmd
import (
"fmt"
"golang.org/x/sync/errgroup"
"io"
"strings"
"github.com/blang/semver"
"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/util"
"github.com/codesphere-cloud/oms/internal/version"
)
type UpdateCmd struct {
cmd *cobra.Command
Version version.Version
Updater Updater
}
func (c *UpdateCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
return c.SelfUpdate(p)
}
func AddUpdateCmd(rootCmd *cobra.Command) {
update := UpdateCmd{
cmd: &cobra.Command{
Use: "update",
Short: "Update Codesphere OMS",
Long: `Updates the OMS to the latest release from OMS Portal.`,
},
Version: &version.Build{},
Updater: &SelfUpdater{},
}
rootCmd.AddCommand(update.cmd)
update.cmd.RunE = update.RunE
}
func (c *UpdateCmd) SelfUpdate(p portal.Portal) error {
currentVersion := semver.MustParse(c.Version.Version())
latest, err := p.GetLatestBuild(portal.OmsProduct)
if err != nil {
return fmt.Errorf("failed to OMS Portal for latest version: %w", err)
}
latestVersion := semver.MustParse(strings.TrimPrefix(latest.Version, "oms-v"))
fmt.Printf("current version: %v\n", currentVersion)
fmt.Printf("latest version: %v\n", latestVersion)
if latestVersion.Equals(currentVersion) {
fmt.Println("Current OMS CLI is already the latest version", c.Version.Version())
return nil
}
// Need a build with a single artifact to download it
download, err := latest.GetBuildForDownload(fmt.Sprintf("%s_%s.tar.gz", c.Version.Os(), c.Version.Arch()))
if err != nil {
return fmt.Errorf("failed to find OMS CLI in package: %w", err)
}
// Use a pipe to unzip the file while downloading without storing on the filesystem
reader, writer := io.Pipe()
defer func() { _ = reader.Close() }()
eg := errgroup.Group{}
eg.Go(func() error {
defer func() { _ = writer.Close() }()
err = p.DownloadBuildArtifact(portal.OmsProduct, download, writer)
if err != nil {
return fmt.Errorf("failed to download latest OMS package: %w", err)
}
return nil
})
cliReader, err := util.StreamFileFromGzip(reader, "oms-cli")
if err != nil {
return fmt.Errorf("failed to extract binary from archive: %w", err)
}
err = c.Updater.Apply(cliReader)
if err != nil {
return fmt.Errorf("failed to apply update: %w", err)
}
_, _ = io.Copy(io.Discard, reader)
// Wait for download to finish and handle any error from the go routine
err = eg.Wait()
if err != nil {
return err
}
fmt.Println("Update finished successfully.")
return nil
}
type Updater interface {
Apply(update io.Reader) error
}
type SelfUpdater struct{}
func (s *SelfUpdater) Apply(r io.Reader) error {
return update.Apply(r, update.Options{})
}