@@ -5,27 +5,30 @@ package cmd
55
66import (
77 "fmt"
8- "io"
9- "strings"
8+ "log"
109
1110 "github.com/blang/semver"
12- "github.com/inconshreveable /go-update "
11+ "github.com/rhysd /go-github-selfupdate/selfupdate "
1312 "github.com/spf13/cobra"
14- "golang.org/x/sync/errgroup"
1513
16- "github.com/codesphere-cloud/oms/internal/portal"
17- "github.com/codesphere-cloud/oms/internal/util"
1814 "github.com/codesphere-cloud/oms/internal/version"
1915)
2016
17+ const GitHubRepo = "codesphere-cloud/oms"
18+
2119type OMSUpdater interface {
22- Apply ( update io. Reader ) error
20+ Update ( v semver. Version , repo string ) (semver. Version , string , error )
2321}
2422
2523type OMSSelfUpdater struct {}
2624
27- func (s * OMSSelfUpdater ) Apply (r io.Reader ) error {
28- return update .Apply (r , update.Options {})
25+ func (s * OMSSelfUpdater ) Update (v semver.Version , repo string ) (semver.Version , string , error ) {
26+ latest , err := selfupdate .UpdateSelf (v , repo )
27+ if err != nil {
28+ return v , "" , err
29+ }
30+
31+ return latest .Version , latest .ReleaseNotes , nil
2932}
3033
3134type UpdateOmsCmd struct {
@@ -42,69 +45,27 @@ func AddOmsUpdateCmd(parentCmd *cobra.Command) {
4245 omsCmd := & cobra.Command {
4346 Use : "oms" ,
4447 Short : "Update the OMS CLI" ,
45- Long : `Updates the OMS CLI to the latest release from OMS Portal .` ,
48+ Long : `Updates the OMS CLI to the latest release from GitHub .` ,
4649 RunE : func (_ * cobra.Command , args []string ) error {
47- p := portal .NewPortalClient ()
48- return cmdState .SelfUpdate (p )
50+ return cmdState .SelfUpdate ()
4951 },
5052 }
5153 parentCmd .AddCommand (omsCmd )
5254}
53-
54- func (c * UpdateOmsCmd ) SelfUpdate (p portal.Portal ) error {
55- currentVersion := semver .MustParse (c .Version .Version ())
56-
57- latest , err := p .GetBuild (portal .OmsProduct , "" , "" )
55+ func (c * UpdateOmsCmd ) SelfUpdate () error {
56+ v := semver .MustParse (c .Version .Version ())
57+ latestVersion , releaseNotes , err := c .Updater .Update (v , GitHubRepo )
5858 if err != nil {
59- return fmt .Errorf ("failed to query OMS Portal for latest version : %w" , err )
59+ return fmt .Errorf ("update failed : %w" , err )
6060 }
61- latestVersion := semver .MustParse (strings .TrimPrefix (latest .Version , "oms-v" ))
6261
63- fmt .Printf ("current version: %v\n " , currentVersion )
64- fmt .Printf ("latest version: %v\n " , latestVersion )
65- if latestVersion .Equals (currentVersion ) {
66- fmt .Println ("Current OMS CLI is already the latest version" , c .Version .Version ())
62+ if latestVersion .Equals (v ) {
63+ log .Println ("Current OMS CLI is the latest version" , c .Version .Version ())
6764 return nil
6865 }
6966
70- // Need a build with a single artifact to download it
71- download , err := latest .GetBuildForDownload (fmt .Sprintf ("%s_%s.tar.gz" , c .Version .Os (), c .Version .Arch ()))
72- if err != nil {
73- return fmt .Errorf ("failed to find OMS CLI in package: %w" , err )
74- }
75-
76- // Use a pipe to unzip the file while downloading without storing on the filesystem
77- reader , writer := io .Pipe ()
78- defer func () { _ = reader .Close () }()
79-
80- eg := errgroup.Group {}
81- eg .Go (func () error {
82- defer func () { _ = writer .Close () }()
83- err = p .DownloadBuildArtifact (portal .OmsProduct , download , writer , false )
84- if err != nil {
85- return fmt .Errorf ("failed to download latest OMS package: %w" , err )
86- }
87- return nil
88- })
89-
90- cliReader , err := util .StreamFileFromGzip (reader , "oms-cli" )
91- if err != nil {
92- return fmt .Errorf ("failed to extract binary from archive: %w" , err )
93- }
94-
95- err = c .Updater .Apply (cliReader )
96- if err != nil {
97- return fmt .Errorf ("failed to apply update: %w" , err )
98- }
99-
100- _ , _ = io .Copy (io .Discard , reader )
101-
102- // Wait for download to finish and handle any error from the go routine
103- err = eg .Wait ()
104- if err != nil {
105- return err
106- }
67+ log .Printf ("Successfully updated from %s to %s\n " , v .String (), latestVersion .String ())
68+ log .Println ("Release notes:\n " , releaseNotes )
10769
108- fmt .Println ("Update finished successfully." )
10970 return nil
11071}
0 commit comments