Skip to content

Commit 8eed120

Browse files
Merge branch 'main' into api-key-integration-test
2 parents dfd8b3d + 757051b commit 8eed120

File tree

15 files changed

+524
-158
lines changed

15 files changed

+524
-158
lines changed

cli/cmd/licenses.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Codesphere Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmd
5+
6+
import (
7+
"log"
8+
9+
"github.com/codesphere-cloud/oms/internal/tmpl"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type LicensesCmd struct {
14+
cmd *cobra.Command
15+
}
16+
17+
func (c *LicensesCmd) RunE(_ *cobra.Command, args []string) error {
18+
log.Println("OMS License:")
19+
log.Println(tmpl.License())
20+
21+
log.Println("=================================")
22+
23+
log.Println("Open source components included:")
24+
log.Println(tmpl.Notice())
25+
26+
return nil
27+
}
28+
29+
func AddLicensesCmd(rootCmd *cobra.Command) {
30+
licenses := LicensesCmd{
31+
cmd: &cobra.Command{
32+
Use: "licenses",
33+
Short: "Print license information",
34+
Long: `Print information about the OMS license and open source licenses of dependencies.`,
35+
},
36+
}
37+
rootCmd.AddCommand(licenses.cmd)
38+
licenses.cmd.RunE = licenses.RunE
39+
}

cli/cmd/mocks.go

Lines changed: 37 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func GetRootCmd() *cobra.Command {
3131
AddListCmd(rootCmd, opts)
3232
AddDownloadCmd(rootCmd, opts)
3333
AddBetaCmd(rootCmd, &opts)
34+
AddLicensesCmd(rootCmd)
3435

3536
// OMS API key management commands
3637
AddRegisterCmd(rootCmd, opts)

cli/cmd/testdata/testcli.tar.gz

10 Bytes
Binary file not shown.

cli/cmd/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package cmd
55

66
import (
7-
"fmt"
7+
"log"
88

99
"github.com/spf13/cobra"
1010
)
@@ -14,7 +14,7 @@ type UpdateCmd struct {
1414
}
1515

1616
func (c *UpdateCmd) RunE(_ *cobra.Command, args []string) error {
17-
fmt.Printf("running %s", c.cmd.Use)
17+
log.Printf("running %s", c.cmd.Use)
1818

1919
return nil
2020
}

cli/cmd/update_api_key.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"fmt"
8+
"log"
89
"time"
910

1011
"github.com/codesphere-cloud/oms/internal/portal"
@@ -39,7 +40,7 @@ func AddApiKeyUpdateCmd(parentCmd *cobra.Command) {
3940
}
4041

4142
apiKeyCmd.Flags().StringVarP(&cmdState.Opts.APIKeyID, "id", "i", "", "The ID of the API key to update")
42-
apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date in RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")")
43+
apiKeyCmd.Flags().StringVar(&cmdState.Opts.ExpiresAtStr, "valid-to", "", "The new expiration date in RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")")
4344

4445
util.MarkFlagRequired(apiKeyCmd, "id")
4546
util.MarkFlagRequired(apiKeyCmd, "valid-to")
@@ -58,6 +59,6 @@ func (c *UpdateAPIKeyCmd) UpdateAPIKey(p portal.Portal) error {
5859
return fmt.Errorf("failed to update API key: %w", err)
5960
}
6061

61-
fmt.Printf("Successfully updated API key '%s' with new expiration date %s.\n", c.Opts.APIKeyID, expiresAt.Format(time.RFC1123))
62+
log.Printf("Successfully updated API key '%s' with new expiration date %s.\n", c.Opts.APIKeyID, expiresAt.Format(time.RFC1123))
6263
return nil
6364
}

cli/cmd/update_oms.go

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ package cmd
55

66
import (
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+
2119
type OMSUpdater interface {
22-
Apply(update io.Reader) error
20+
Update(v semver.Version, repo string) (semver.Version, string, error)
2321
}
2422

2523
type 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

3134
type 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)
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

Comments
 (0)