From 2bbe2d9fe48b56c088d89a10e6a26e21b84264b3 Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Wed, 8 Oct 2025 11:55:28 +0200 Subject: [PATCH 1/7] feat: add first working draft --- cli/cmd/mocks.go | 46 ++--- cli/cmd/root.go | 8 +- cli/cmd/update.go | 101 ++--------- cli/cmd/update_api_key.go | 61 +++++++ cli/cmd/update_api_key_test.go | 71 ++++++++ cli/cmd/update_oms.go | 110 ++++++++++++ .../{update_test.go => update_oms_test.go} | 8 +- internal/portal/http.go | 25 +++ internal/portal/mocks.go | 166 ++++++++++++++++-- internal/util/mocks.go | 56 ++++-- 10 files changed, 498 insertions(+), 154 deletions(-) create mode 100644 cli/cmd/update_api_key.go create mode 100644 cli/cmd/update_api_key_test.go create mode 100644 cli/cmd/update_oms.go rename cli/cmd/{update_test.go => update_oms_test.go} (95%) diff --git a/cli/cmd/mocks.go b/cli/cmd/mocks.go index 0aab4e6b..783bbde4 100644 --- a/cli/cmd/mocks.go +++ b/cli/cmd/mocks.go @@ -9,13 +9,13 @@ import ( "io" ) -// NewMockUpdater creates a new instance of MockUpdater. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// NewMockOMSUpdater creates a new instance of MockOMSUpdater. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. -func NewMockUpdater(t interface { +func NewMockOMSUpdater(t interface { mock.TestingT Cleanup(func()) -}) *MockUpdater { - mock := &MockUpdater{} +}) *MockOMSUpdater { + mock := &MockOMSUpdater{} mock.Mock.Test(t) t.Cleanup(func() { mock.AssertExpectations(t) }) @@ -23,21 +23,21 @@ func NewMockUpdater(t interface { return mock } -// MockUpdater is an autogenerated mock type for the Updater type -type MockUpdater struct { +// MockOMSUpdater is an autogenerated mock type for the OMSUpdater type +type MockOMSUpdater struct { mock.Mock } -type MockUpdater_Expecter struct { +type MockOMSUpdater_Expecter struct { mock *mock.Mock } -func (_m *MockUpdater) EXPECT() *MockUpdater_Expecter { - return &MockUpdater_Expecter{mock: &_m.Mock} +func (_m *MockOMSUpdater) EXPECT() *MockOMSUpdater_Expecter { + return &MockOMSUpdater_Expecter{mock: &_m.Mock} } -// Apply provides a mock function for the type MockUpdater -func (_mock *MockUpdater) Apply(update io.Reader) error { +// Apply provides a mock function for the type MockOMSUpdater +func (_mock *MockOMSUpdater) Apply(update io.Reader) error { ret := _mock.Called(update) if len(ret) == 0 { @@ -53,30 +53,36 @@ func (_mock *MockUpdater) Apply(update io.Reader) error { return r0 } -// MockUpdater_Apply_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Apply' -type MockUpdater_Apply_Call struct { +// MockOMSUpdater_Apply_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Apply' +type MockOMSUpdater_Apply_Call struct { *mock.Call } // Apply is a helper method to define mock.On call -// - update -func (_e *MockUpdater_Expecter) Apply(update interface{}) *MockUpdater_Apply_Call { - return &MockUpdater_Apply_Call{Call: _e.mock.On("Apply", update)} +// - update io.Reader +func (_e *MockOMSUpdater_Expecter) Apply(update interface{}) *MockOMSUpdater_Apply_Call { + return &MockOMSUpdater_Apply_Call{Call: _e.mock.On("Apply", update)} } -func (_c *MockUpdater_Apply_Call) Run(run func(update io.Reader)) *MockUpdater_Apply_Call { +func (_c *MockOMSUpdater_Apply_Call) Run(run func(update io.Reader)) *MockOMSUpdater_Apply_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(io.Reader)) + var arg0 io.Reader + if args[0] != nil { + arg0 = args[0].(io.Reader) + } + run( + arg0, + ) }) return _c } -func (_c *MockUpdater_Apply_Call) Return(err error) *MockUpdater_Apply_Call { +func (_c *MockOMSUpdater_Apply_Call) Return(err error) *MockOMSUpdater_Apply_Call { _c.Call.Return(err) return _c } -func (_c *MockUpdater_Apply_Call) RunAndReturn(run func(update io.Reader) error) *MockUpdater_Apply_Call { +func (_c *MockOMSUpdater_Apply_Call) RunAndReturn(run func(update io.Reader) error) *MockOMSUpdater_Apply_Call { _c.Call.Return(run) return _c } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 359b5644..c4d4fb68 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -26,14 +26,14 @@ func GetRootCmd() *cobra.Command { like downloading new versions.`), } AddVersionCmd(rootCmd) - AddUpdateCmd(rootCmd) + AddUpdateCmd(rootCmd, opts) AddListCmd(rootCmd, opts) AddDownloadCmd(rootCmd, opts) - - // OMS API key management commands + + // OMS API key management commands AddRegisterCmd(rootCmd, opts) AddRevokeCmd(rootCmd, opts) - + return rootCmd } diff --git a/cli/cmd/update.go b/cli/cmd/update.go index 819d10a0..68610dc6 100644 --- a/cli/cmd/update.go +++ b/cli/cmd/update.go @@ -5,111 +5,34 @@ package cmd import ( "fmt" - "io" - "strings" - "golang.org/x/sync/errgroup" - - "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 + cmd *cobra.Command } func (c *UpdateCmd) RunE(_ *cobra.Command, args []string) error { + fmt.Printf("running %s", c.cmd.Use) - p := portal.NewPortalClient() - - return c.SelfUpdate(p) + return nil } -func AddUpdateCmd(rootCmd *cobra.Command) { - update := UpdateCmd{ +func AddUpdateCmd(rootCmd *cobra.Command, opts GlobalOptions) { + updateCmd := UpdateCmd{ cmd: &cobra.Command{ Use: "update", - Short: "Update Codesphere OMS", - Long: `Updates the OMS to the latest release from OMS Portal.`, + Short: "Update various resources", + Long: `Updates various resources such as the OMS CLI or API keys.`, }, - 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.GetBuild(portal.OmsProduct, "", "") - if err != nil { - return fmt.Errorf("failed to query 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 -} + updateCmd.cmd.RunE = updateCmd.RunE -type SelfUpdater struct{} + AddDownloadPackageCmd(updateCmd.cmd, opts) + addOmsUpdateCmd(updateCmd.cmd) + addApiKeyUpdateCmd(updateCmd.cmd) -func (s *SelfUpdater) Apply(r io.Reader) error { - return update.Apply(r, update.Options{}) + rootCmd.AddCommand(updateCmd.cmd) } diff --git a/cli/cmd/update_api_key.go b/cli/cmd/update_api_key.go new file mode 100644 index 00000000..62def7c5 --- /dev/null +++ b/cli/cmd/update_api_key.go @@ -0,0 +1,61 @@ +// Copyright (c) Codesphere Inc. +// SPDX-License-Identifier: Apache-2.0 + +package cmd + +import ( + "fmt" + "time" + + "github.com/codesphere-cloud/oms/internal/portal" + "github.com/spf13/cobra" +) + +type UpdateAPIKeyCmd struct { + Opts UpdateAPIKeyOpts +} + +type UpdateAPIKeyOpts struct { + GlobalOptions + APIKeyID string + ExpiresAtStr string +} + +func addApiKeyUpdateCmd(parentCmd *cobra.Command) { + cmdState := &UpdateAPIKeyCmd{ + Opts: UpdateAPIKeyOpts{}, + } + + apiKeyCmd := &cobra.Command{ + Use: "api-key", + Short: "Update an API key's expiration date", + Long: `Updates the expiration date for a given API key using the --id and --valid-to flags. The expiration date must be provided in RFC3339 format (e.g., "2025-12-31T23:59:59Z").`, + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + p := portal.NewPortalClient() + return cmdState.UpdateAPIKey(p) + }, + } + + apiKeyCmd.Flags().StringVarP(&cmdState.Opts.APIKeyID, "id", "i", "", "The ID of the API key to update") + apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date (RFC3339 format)") + + _ = apiKeyCmd.MarkFlagRequired("id") + _ = apiKeyCmd.MarkFlagRequired("valid-to") + + parentCmd.AddCommand(apiKeyCmd) +} + +func (c *UpdateAPIKeyCmd) UpdateAPIKey(p portal.Portal) error { + expiresAt, err := time.Parse(time.RFC3339, c.Opts.ExpiresAtStr) + if err != nil { + return fmt.Errorf("invalid date format for : %w. Please use RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")", err) + } + + if err := p.UpdateAPIKey(c.Opts.APIKeyID, expiresAt); err != nil { + return fmt.Errorf("failed to update API key: %w", err) + } + + fmt.Printf("Successfully updated API key '%s' with new expiration date %s.\n", c.Opts.APIKeyID, expiresAt.Format(time.RFC1123)) + return nil +} diff --git a/cli/cmd/update_api_key_test.go b/cli/cmd/update_api_key_test.go new file mode 100644 index 00000000..4df50449 --- /dev/null +++ b/cli/cmd/update_api_key_test.go @@ -0,0 +1,71 @@ +// Copyright (c) Codesphere Inc. +// SPDX-License-Identifier: Apache-2.0 + +package cmd_test + +import ( + "fmt" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/codesphere-cloud/oms/cli/cmd" + "github.com/codesphere-cloud/oms/internal/portal" +) + +var _ = Describe("UpdateAPIKey", func() { + + var ( + mockPortal *portal.MockPortal + c cmd.UpdateAPIKeyCmd + ) + + BeforeEach(func() { + mockPortal = portal.NewMockPortal(GinkgoT()) + c = cmd.UpdateAPIKeyCmd{} + }) + + Describe("Run", func() { + It("successfully updates the API key when given valid input", func() { + apiKeyID := "aaaaaaaaaaaaaaaaaaaaaa" + expiresAtStr := "2027-12-31T23:59:59Z" + expectedExpiresAt, err := time.Parse(time.RFC3339, expiresAtStr) + Expect(err).NotTo(HaveOccurred()) + + c.Opts.APIKeyID = apiKeyID + c.Opts.ExpiresAtStr = expiresAtStr + + mockPortal.EXPECT().UpdateAPIKey(apiKeyID, expectedExpiresAt).Return(nil) + + err = c.UpdateAPIKey(mockPortal) + Expect(err).NotTo(HaveOccurred()) + }) + + It("returns an error for an invalid api key id format", func() { + apiKeyID := "not-a-valid-id" + expiresAtStr := "2027-12-31T23:59:59Z" + expectedExpiresAt, err := time.Parse(time.RFC3339, expiresAtStr) + Expect(err).NotTo(HaveOccurred()) + + c.Opts.APIKeyID = apiKeyID + c.Opts.ExpiresAtStr = expiresAtStr + + mockPortal.EXPECT().UpdateAPIKey(apiKeyID, expectedExpiresAt).Return(fmt.Errorf("invalid api key id format")) + + err = c.UpdateAPIKey(mockPortal) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("invalid api key id format")) + }) + + It("returns an error for an invalid date format", func() { + c.Opts.APIKeyID = "valid id" + c.Opts.ExpiresAtStr = "2025/123/123" + + err := c.UpdateAPIKey(mockPortal) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("invalid date format")) + }) + }) +}) diff --git a/cli/cmd/update_oms.go b/cli/cmd/update_oms.go new file mode 100644 index 00000000..f78dbbdb --- /dev/null +++ b/cli/cmd/update_oms.go @@ -0,0 +1,110 @@ +// Copyright (c) Codesphere Inc. +// SPDX-License-Identifier: Apache-2.0 + +package cmd + +import ( + "fmt" + "io" + "strings" + + "github.com/blang/semver" + "github.com/inconshreveable/go-update" + "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" + + "github.com/codesphere-cloud/oms/internal/portal" + "github.com/codesphere-cloud/oms/internal/util" + "github.com/codesphere-cloud/oms/internal/version" +) + +type OMSUpdater interface { + Apply(update io.Reader) error +} + +type OMSSelfUpdater struct{} + +func (s *OMSSelfUpdater) Apply(r io.Reader) error { + return update.Apply(r, update.Options{}) +} + +type UpdateOmsCmd struct { + Version version.Version + Updater OMSUpdater +} + +func addOmsUpdateCmd(parentCmd *cobra.Command) { + cmdState := &UpdateOmsCmd{ + Version: &version.Build{}, + Updater: &OMSSelfUpdater{}, + } + + omsCmd := &cobra.Command{ + Use: "oms", + Short: "Update the OMS CLI", + Long: `Updates the OMS CLI to the latest release from OMS Portal.`, + RunE: func(_ *cobra.Command, args []string) error { + p := portal.NewPortalClient() + return cmdState.SelfUpdate(p) + }, + } + parentCmd.AddCommand(omsCmd) +} + +func (c *UpdateOmsCmd) SelfUpdate(p portal.Portal) error { + currentVersion := semver.MustParse(c.Version.Version()) + + latest, err := p.GetBuild(portal.OmsProduct, "", "") + if err != nil { + return fmt.Errorf("failed to query 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 +} diff --git a/cli/cmd/update_test.go b/cli/cmd/update_oms_test.go similarity index 95% rename from cli/cmd/update_test.go rename to cli/cmd/update_oms_test.go index c2be6898..0a895b82 100644 --- a/cli/cmd/update_test.go +++ b/cli/cmd/update_oms_test.go @@ -30,16 +30,16 @@ var _ = Describe("Update", func() { var ( mockPortal *portal.MockPortal mockVersion *version.MockVersion - mockUpdater *cmd.MockUpdater + mockUpdater *cmd.MockOMSUpdater latestBuild portal.Build buildToDownload portal.Build - c cmd.UpdateCmd + c cmd.UpdateOmsCmd ) BeforeEach(func() { mockPortal = portal.NewMockPortal(GinkgoT()) mockVersion = version.NewMockVersion(GinkgoT()) - mockUpdater = cmd.NewMockUpdater(GinkgoT()) + mockUpdater = cmd.NewMockOMSUpdater(GinkgoT()) latestBuild = portal.Build{ Version: "0.0.42", @@ -55,7 +55,7 @@ var _ = Describe("Update", func() { {Filename: "fakeos_fakearch.tar.gz"}, }, } - c = cmd.UpdateCmd{ + c = cmd.UpdateOmsCmd{ Version: mockVersion, Updater: mockUpdater, } diff --git a/internal/portal/http.go b/internal/portal/http.go index fcc0b5e9..9a2ddab0 100644 --- a/internal/portal/http.go +++ b/internal/portal/http.go @@ -25,6 +25,7 @@ type Portal interface { DownloadBuildArtifact(product Product, build Build, file io.Writer) error RegisterAPIKey(owner string, organization string, role string, expiresAt time.Time) error RevokeAPIKey(key string) error + UpdateAPIKey(key string, expiresAt time.Time) error } type PortalClient struct { @@ -253,3 +254,27 @@ func (c *PortalClient) RevokeAPIKey(key string) error { fmt.Println("API key revoked successfully") return nil } + +func (c *PortalClient) UpdateAPIKey(key string, expiresAt time.Time) error { + req := struct { + Key string `json:"key"` + ExpiresAt time.Time `json:"expires_at"` + }{ + Key: key, + ExpiresAt: expiresAt, + } + + reqBody, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("failed to generate request body: %w", err) + } + + resp, err := c.HttpRequest(http.MethodPost, "/key/update", reqBody) + if err != nil { + return fmt.Errorf("POST request to update API key failed: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + fmt.Println("API key updated successfully") + return nil +} diff --git a/internal/portal/mocks.go b/internal/portal/mocks.go index 8279e003..f0466cf4 100644 --- a/internal/portal/mocks.go +++ b/internal/portal/mocks.go @@ -61,16 +61,32 @@ type MockPortal_DownloadBuildArtifact_Call struct { } // DownloadBuildArtifact is a helper method to define mock.On call -// - product -// - build -// - file +// - product Product +// - build Build +// - file io.Writer func (_e *MockPortal_Expecter) DownloadBuildArtifact(product interface{}, build interface{}, file interface{}) *MockPortal_DownloadBuildArtifact_Call { return &MockPortal_DownloadBuildArtifact_Call{Call: _e.mock.On("DownloadBuildArtifact", product, build, file)} } func (_c *MockPortal_DownloadBuildArtifact_Call) Run(run func(product Product, build Build, file io.Writer)) *MockPortal_DownloadBuildArtifact_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(Product), args[1].(Build), args[2].(io.Writer)) + var arg0 Product + if args[0] != nil { + arg0 = args[0].(Product) + } + var arg1 Build + if args[1] != nil { + arg1 = args[1].(Build) + } + var arg2 io.Writer + if args[2] != nil { + arg2 = args[2].(io.Writer) + } + run( + arg0, + arg1, + arg2, + ) }) return _c } @@ -117,16 +133,32 @@ type MockPortal_GetBuild_Call struct { } // GetBuild is a helper method to define mock.On call -// - product -// - version -// - hash +// - product Product +// - version string +// - hash string func (_e *MockPortal_Expecter) GetBuild(product interface{}, version interface{}, hash interface{}) *MockPortal_GetBuild_Call { return &MockPortal_GetBuild_Call{Call: _e.mock.On("GetBuild", product, version, hash)} } func (_c *MockPortal_GetBuild_Call) Run(run func(product Product, version string, hash string)) *MockPortal_GetBuild_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(Product), args[1].(string), args[2].(string)) + var arg0 Product + if args[0] != nil { + arg0 = args[0].(Product) + } + var arg1 string + if args[1] != nil { + arg1 = args[1].(string) + } + var arg2 string + if args[2] != nil { + arg2 = args[2].(string) + } + run( + arg0, + arg1, + arg2, + ) }) return _c } @@ -173,14 +205,20 @@ type MockPortal_ListBuilds_Call struct { } // ListBuilds is a helper method to define mock.On call -// - product +// - product Product func (_e *MockPortal_Expecter) ListBuilds(product interface{}) *MockPortal_ListBuilds_Call { return &MockPortal_ListBuilds_Call{Call: _e.mock.On("ListBuilds", product)} } func (_c *MockPortal_ListBuilds_Call) Run(run func(product Product)) *MockPortal_ListBuilds_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(Product)) + var arg0 Product + if args[0] != nil { + arg0 = args[0].(Product) + } + run( + arg0, + ) }) return _c } @@ -218,17 +256,38 @@ type MockPortal_RegisterAPIKey_Call struct { } // RegisterAPIKey is a helper method to define mock.On call -// - owner -// - organization -// - role -// - expiresAt +// - owner string +// - organization string +// - role string +// - expiresAt time.Time func (_e *MockPortal_Expecter) RegisterAPIKey(owner interface{}, organization interface{}, role interface{}, expiresAt interface{}) *MockPortal_RegisterAPIKey_Call { return &MockPortal_RegisterAPIKey_Call{Call: _e.mock.On("RegisterAPIKey", owner, organization, role, expiresAt)} } func (_c *MockPortal_RegisterAPIKey_Call) Run(run func(owner string, organization string, role string, expiresAt time.Time)) *MockPortal_RegisterAPIKey_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string), args[1].(string), args[2].(string), args[3].(time.Time)) + var arg0 string + if args[0] != nil { + arg0 = args[0].(string) + } + var arg1 string + if args[1] != nil { + arg1 = args[1].(string) + } + var arg2 string + if args[2] != nil { + arg2 = args[2].(string) + } + var arg3 time.Time + if args[3] != nil { + arg3 = args[3].(time.Time) + } + run( + arg0, + arg1, + arg2, + arg3, + ) }) return _c } @@ -266,14 +325,20 @@ type MockPortal_RevokeAPIKey_Call struct { } // RevokeAPIKey is a helper method to define mock.On call -// - key +// - key string func (_e *MockPortal_Expecter) RevokeAPIKey(key interface{}) *MockPortal_RevokeAPIKey_Call { return &MockPortal_RevokeAPIKey_Call{Call: _e.mock.On("RevokeAPIKey", key)} } func (_c *MockPortal_RevokeAPIKey_Call) Run(run func(key string)) *MockPortal_RevokeAPIKey_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + var arg0 string + if args[0] != nil { + arg0 = args[0].(string) + } + run( + arg0, + ) }) return _c } @@ -288,6 +353,63 @@ func (_c *MockPortal_RevokeAPIKey_Call) RunAndReturn(run func(key string) error) return _c } +// UpdateAPIKey provides a mock function for the type MockPortal +func (_mock *MockPortal) UpdateAPIKey(key string, expiresAt time.Time) error { + ret := _mock.Called(key, expiresAt) + + if len(ret) == 0 { + panic("no return value specified for UpdateAPIKey") + } + + var r0 error + if returnFunc, ok := ret.Get(0).(func(string, time.Time) error); ok { + r0 = returnFunc(key, expiresAt) + } else { + r0 = ret.Error(0) + } + return r0 +} + +// MockPortal_UpdateAPIKey_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdateAPIKey' +type MockPortal_UpdateAPIKey_Call struct { + *mock.Call +} + +// UpdateAPIKey is a helper method to define mock.On call +// - key string +// - expiresAt time.Time +func (_e *MockPortal_Expecter) UpdateAPIKey(key interface{}, expiresAt interface{}) *MockPortal_UpdateAPIKey_Call { + return &MockPortal_UpdateAPIKey_Call{Call: _e.mock.On("UpdateAPIKey", key, expiresAt)} +} + +func (_c *MockPortal_UpdateAPIKey_Call) Run(run func(key string, expiresAt time.Time)) *MockPortal_UpdateAPIKey_Call { + _c.Call.Run(func(args mock.Arguments) { + var arg0 string + if args[0] != nil { + arg0 = args[0].(string) + } + var arg1 time.Time + if args[1] != nil { + arg1 = args[1].(time.Time) + } + run( + arg0, + arg1, + ) + }) + return _c +} + +func (_c *MockPortal_UpdateAPIKey_Call) Return(err error) *MockPortal_UpdateAPIKey_Call { + _c.Call.Return(err) + return _c +} + +func (_c *MockPortal_UpdateAPIKey_Call) RunAndReturn(run func(key string, expiresAt time.Time) error) *MockPortal_UpdateAPIKey_Call { + _c.Call.Return(run) + return _c +} + // NewMockHttpClient creates a new instance of MockHttpClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewMockHttpClient(t interface { @@ -349,14 +471,20 @@ type MockHttpClient_Do_Call struct { } // Do is a helper method to define mock.On call -// - request +// - request *http.Request func (_e *MockHttpClient_Expecter) Do(request interface{}) *MockHttpClient_Do_Call { return &MockHttpClient_Do_Call{Call: _e.mock.On("Do", request)} } func (_c *MockHttpClient_Do_Call) Run(run func(request *http.Request)) *MockHttpClient_Do_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(*http.Request)) + var arg0 *http.Request + if args[0] != nil { + arg0 = args[0].(*http.Request) + } + run( + arg0, + ) }) return _c } diff --git a/internal/util/mocks.go b/internal/util/mocks.go index bf8044cf..b27691fc 100644 --- a/internal/util/mocks.go +++ b/internal/util/mocks.go @@ -71,14 +71,20 @@ type MockFileWriter_Create_Call struct { } // Create is a helper method to define mock.On call -// - filename +// - filename string func (_e *MockFileWriter_Expecter) Create(filename interface{}) *MockFileWriter_Create_Call { return &MockFileWriter_Create_Call{Call: _e.mock.On("Create", filename)} } func (_c *MockFileWriter_Create_Call) Run(run func(filename string)) *MockFileWriter_Create_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + var arg0 string + if args[0] != nil { + arg0 = args[0].(string) + } + run( + arg0, + ) }) return _c } @@ -137,8 +143,8 @@ type MockTableWriter_AppendHeader_Call struct { } // AppendHeader is a helper method to define mock.On call -// - row -// - configs +// - row table.Row +// - configs ...table.RowConfig func (_e *MockTableWriter_Expecter) AppendHeader(row interface{}, configs ...interface{}) *MockTableWriter_AppendHeader_Call { return &MockTableWriter_AppendHeader_Call{Call: _e.mock.On("AppendHeader", append([]interface{}{row}, configs...)...)} @@ -146,13 +152,20 @@ func (_e *MockTableWriter_Expecter) AppendHeader(row interface{}, configs ...int func (_c *MockTableWriter_AppendHeader_Call) Run(run func(row table.Row, configs ...table.RowConfig)) *MockTableWriter_AppendHeader_Call { _c.Call.Run(func(args mock.Arguments) { - variadicArgs := make([]table.RowConfig, len(args)-1) - for i, a := range args[1:] { - if a != nil { - variadicArgs[i] = a.(table.RowConfig) - } + var arg0 table.Row + if args[0] != nil { + arg0 = args[0].(table.Row) + } + var arg1 []table.RowConfig + var variadicArgs []table.RowConfig + if len(args) > 1 { + variadicArgs = args[1].([]table.RowConfig) } - run(args[0].(table.Row), variadicArgs...) + arg1 = variadicArgs + run( + arg0, + arg1..., + ) }) return _c } @@ -184,8 +197,8 @@ type MockTableWriter_AppendRow_Call struct { } // AppendRow is a helper method to define mock.On call -// - row -// - configs +// - row table.Row +// - configs ...table.RowConfig func (_e *MockTableWriter_Expecter) AppendRow(row interface{}, configs ...interface{}) *MockTableWriter_AppendRow_Call { return &MockTableWriter_AppendRow_Call{Call: _e.mock.On("AppendRow", append([]interface{}{row}, configs...)...)} @@ -193,13 +206,20 @@ func (_e *MockTableWriter_Expecter) AppendRow(row interface{}, configs ...interf func (_c *MockTableWriter_AppendRow_Call) Run(run func(row table.Row, configs ...table.RowConfig)) *MockTableWriter_AppendRow_Call { _c.Call.Run(func(args mock.Arguments) { - variadicArgs := make([]table.RowConfig, len(args)-1) - for i, a := range args[1:] { - if a != nil { - variadicArgs[i] = a.(table.RowConfig) - } + var arg0 table.Row + if args[0] != nil { + arg0 = args[0].(table.Row) + } + var arg1 []table.RowConfig + var variadicArgs []table.RowConfig + if len(args) > 1 { + variadicArgs = args[1].([]table.RowConfig) } - run(args[0].(table.Row), variadicArgs...) + arg1 = variadicArgs + run( + arg0, + arg1..., + ) }) return _c } From bf0ff21475f2e4e99b75256c2b74ebd1d3fab966 Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Wed, 8 Oct 2025 15:26:21 +0200 Subject: [PATCH 2/7] fix: json keys --- internal/portal/api_key.go | 2 +- internal/portal/http.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/portal/api_key.go b/internal/portal/api_key.go index 5b997b4f..af0b17c9 100644 --- a/internal/portal/api_key.go +++ b/internal/portal/api_key.go @@ -4,7 +4,7 @@ import "time" type ApiKey struct { RID string `json:"rid"` - ApiKey string `json:"api_key"` + ApiKey string `json:"apiKey"` Owner string `json:"owner"` Organization string `json:"organization"` Role string `json:"role"` diff --git a/internal/portal/http.go b/internal/portal/http.go index 1afe4be9..c43ed8ae 100644 --- a/internal/portal/http.go +++ b/internal/portal/http.go @@ -237,7 +237,7 @@ func (c *PortalClient) RegisterAPIKey(owner string, organization string, role st func (c *PortalClient) RevokeAPIKey(keyId string) error { req := struct { - KeyID string `json:"key_id"` + KeyID string `json:"keyId"` }{ KeyID: keyId, } @@ -260,8 +260,8 @@ func (c *PortalClient) RevokeAPIKey(keyId string) error { func (c *PortalClient) UpdateAPIKey(key string, expiresAt time.Time) error { req := struct { - Key string `json:"key"` - ExpiresAt time.Time `json:"expires_at"` + Key string `json:"keyId"` + ExpiresAt time.Time `json:"expiresAt"` }{ Key: key, ExpiresAt: expiresAt, From 88db295f27d9481affa0ec6998f8a41fa925c94e Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Thu, 9 Oct 2025 13:25:10 +0200 Subject: [PATCH 3/7] fix: ApiKey struct fields with correct json key --- internal/portal/api_key.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/portal/api_key.go b/internal/portal/api_key.go index af0b17c9..20de7892 100644 --- a/internal/portal/api_key.go +++ b/internal/portal/api_key.go @@ -3,11 +3,12 @@ package portal import "time" type ApiKey struct { - RID string `json:"rid"` - ApiKey string `json:"apiKey"` + KeyID string `json:"keyId"` Owner string `json:"owner"` Organization string `json:"organization"` Role string `json:"role"` CreatedAt time.Time `json:"createdAt"` ExpiresAt time.Time `json:"expiresAt"` + // Temp + ApiKey string `json:"apiKey"` } From 8db2a33d6a17dcc552c74a702e4e16158218833f Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Thu, 9 Oct 2025 15:57:09 +0200 Subject: [PATCH 4/7] refactor: replace direct flag requirement calls with util function --- cli/cmd/update_api_key.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/cmd/update_api_key.go b/cli/cmd/update_api_key.go index 62def7c5..0a07309b 100644 --- a/cli/cmd/update_api_key.go +++ b/cli/cmd/update_api_key.go @@ -8,6 +8,7 @@ import ( "time" "github.com/codesphere-cloud/oms/internal/portal" + "github.com/codesphere-cloud/oms/internal/util" "github.com/spf13/cobra" ) @@ -40,8 +41,8 @@ func addApiKeyUpdateCmd(parentCmd *cobra.Command) { apiKeyCmd.Flags().StringVarP(&cmdState.Opts.APIKeyID, "id", "i", "", "The ID of the API key to update") apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date (RFC3339 format)") - _ = apiKeyCmd.MarkFlagRequired("id") - _ = apiKeyCmd.MarkFlagRequired("valid-to") + util.MarkFlagRequired(apiKeyCmd, "id") + util.MarkFlagRequired(apiKeyCmd, "valid-to") parentCmd.AddCommand(apiKeyCmd) } From 4b7851eee750bb002d1061629345b83e4796f792 Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Thu, 9 Oct 2025 16:25:28 +0200 Subject: [PATCH 5/7] fix: update command descriptions for clarity and consistency --- cli/cmd/update.go | 4 ++-- cli/cmd/update_api_key.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cli/cmd/update.go b/cli/cmd/update.go index 68610dc6..a21975ff 100644 --- a/cli/cmd/update.go +++ b/cli/cmd/update.go @@ -23,8 +23,8 @@ func AddUpdateCmd(rootCmd *cobra.Command, opts GlobalOptions) { updateCmd := UpdateCmd{ cmd: &cobra.Command{ Use: "update", - Short: "Update various resources", - Long: `Updates various resources such as the OMS CLI or API keys.`, + Short: "Update OMS related resources", + Long: `Updates resources, e.g. OMS or OMS API keys.`, }, } diff --git a/cli/cmd/update_api_key.go b/cli/cmd/update_api_key.go index 0a07309b..aa824a32 100644 --- a/cli/cmd/update_api_key.go +++ b/cli/cmd/update_api_key.go @@ -30,7 +30,7 @@ func addApiKeyUpdateCmd(parentCmd *cobra.Command) { apiKeyCmd := &cobra.Command{ Use: "api-key", Short: "Update an API key's expiration date", - Long: `Updates the expiration date for a given API key using the --id and --valid-to flags. The expiration date must be provided in RFC3339 format (e.g., "2025-12-31T23:59:59Z").`, + Long: `Updates the expiration date for a given API key using the --id and --valid-to flags.`, Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { p := portal.NewPortalClient() @@ -39,7 +39,7 @@ func addApiKeyUpdateCmd(parentCmd *cobra.Command) { } apiKeyCmd.Flags().StringVarP(&cmdState.Opts.APIKeyID, "id", "i", "", "The ID of the API key to update") - apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date (RFC3339 format)") + apiKeyCmd.Flags().StringVarP(&cmdState.Opts.ExpiresAtStr, "valid-to", "v", "", "The new expiration date in RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")") util.MarkFlagRequired(apiKeyCmd, "id") util.MarkFlagRequired(apiKeyCmd, "valid-to") @@ -49,8 +49,9 @@ func addApiKeyUpdateCmd(parentCmd *cobra.Command) { func (c *UpdateAPIKeyCmd) UpdateAPIKey(p portal.Portal) error { expiresAt, err := time.Parse(time.RFC3339, c.Opts.ExpiresAtStr) + if err != nil { - return fmt.Errorf("invalid date format for : %w. Please use RFC3339 format (e.g., \"2025-12-31T23:59:59Z\")", err) + return fmt.Errorf("invalid date format for : %w", err) } if err := p.UpdateAPIKey(c.Opts.APIKeyID, expiresAt); err != nil { From 1ffd6f84ba9ae3ca08a91d54dbff1f72e73bc189 Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Thu, 9 Oct 2025 16:39:51 +0200 Subject: [PATCH 6/7] fix: update function names to export them --- cli/cmd/update.go | 4 ++-- cli/cmd/update_api_key.go | 2 +- cli/cmd/update_oms.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cmd/update.go b/cli/cmd/update.go index a21975ff..7ab6500b 100644 --- a/cli/cmd/update.go +++ b/cli/cmd/update.go @@ -31,8 +31,8 @@ func AddUpdateCmd(rootCmd *cobra.Command, opts GlobalOptions) { updateCmd.cmd.RunE = updateCmd.RunE AddDownloadPackageCmd(updateCmd.cmd, opts) - addOmsUpdateCmd(updateCmd.cmd) - addApiKeyUpdateCmd(updateCmd.cmd) + AddOmsUpdateCmd(updateCmd.cmd) + AddApiKeyUpdateCmd(updateCmd.cmd) rootCmd.AddCommand(updateCmd.cmd) } diff --git a/cli/cmd/update_api_key.go b/cli/cmd/update_api_key.go index aa824a32..dc2be180 100644 --- a/cli/cmd/update_api_key.go +++ b/cli/cmd/update_api_key.go @@ -22,7 +22,7 @@ type UpdateAPIKeyOpts struct { ExpiresAtStr string } -func addApiKeyUpdateCmd(parentCmd *cobra.Command) { +func AddApiKeyUpdateCmd(parentCmd *cobra.Command) { cmdState := &UpdateAPIKeyCmd{ Opts: UpdateAPIKeyOpts{}, } diff --git a/cli/cmd/update_oms.go b/cli/cmd/update_oms.go index f78dbbdb..8d14b26a 100644 --- a/cli/cmd/update_oms.go +++ b/cli/cmd/update_oms.go @@ -33,7 +33,7 @@ type UpdateOmsCmd struct { Updater OMSUpdater } -func addOmsUpdateCmd(parentCmd *cobra.Command) { +func AddOmsUpdateCmd(parentCmd *cobra.Command) { cmdState := &UpdateOmsCmd{ Version: &version.Build{}, Updater: &OMSSelfUpdater{}, From 05db3060a79ece8ffb0a15989145900b92b3083a Mon Sep 17 00:00:00 2001 From: OliverTrautvetter <66372584+OliverTrautvetter@users.noreply.github.com> Date: Fri, 10 Oct 2025 09:42:42 +0200 Subject: [PATCH 7/7] refactor: update mock function signatures and fix missing bracket --- cli/cmd/mocks.go | 10 +-- internal/portal/http.go | 1 + internal/portal/mocks.go | 189 +++++++++++---------------------------- internal/util/mocks.go | 63 +++++-------- 4 files changed, 79 insertions(+), 184 deletions(-) diff --git a/cli/cmd/mocks.go b/cli/cmd/mocks.go index 783bbde4..ea7610c5 100644 --- a/cli/cmd/mocks.go +++ b/cli/cmd/mocks.go @@ -59,20 +59,14 @@ type MockOMSUpdater_Apply_Call struct { } // Apply is a helper method to define mock.On call -// - update io.Reader +// - update func (_e *MockOMSUpdater_Expecter) Apply(update interface{}) *MockOMSUpdater_Apply_Call { return &MockOMSUpdater_Apply_Call{Call: _e.mock.On("Apply", update)} } func (_c *MockOMSUpdater_Apply_Call) Run(run func(update io.Reader)) *MockOMSUpdater_Apply_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 io.Reader - if args[0] != nil { - arg0 = args[0].(io.Reader) - } - run( - arg0, - ) + run(args[0].(io.Reader)) }) return _c } diff --git a/internal/portal/http.go b/internal/portal/http.go index a490ecf3..8f1a383f 100644 --- a/internal/portal/http.go +++ b/internal/portal/http.go @@ -281,6 +281,7 @@ func (c *PortalClient) UpdateAPIKey(key string, expiresAt time.Time) error { fmt.Println("API key updated successfully") return nil +} func (c *PortalClient) ListAPIKeys() ([]ApiKey, error) { res, _, err := c.GetBody("/keys") diff --git a/internal/portal/mocks.go b/internal/portal/mocks.go index 9f08172c..6b85a82b 100644 --- a/internal/portal/mocks.go +++ b/internal/portal/mocks.go @@ -5,11 +5,10 @@ package portal import ( + mock "github.com/stretchr/testify/mock" "io" "net/http" "time" - - mock "github.com/stretchr/testify/mock" ) // NewMockPortal creates a new instance of MockPortal. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. @@ -62,32 +61,16 @@ type MockPortal_DownloadBuildArtifact_Call struct { } // DownloadBuildArtifact is a helper method to define mock.On call -// - product Product -// - build Build -// - file io.Writer +// - product +// - build +// - file func (_e *MockPortal_Expecter) DownloadBuildArtifact(product interface{}, build interface{}, file interface{}) *MockPortal_DownloadBuildArtifact_Call { return &MockPortal_DownloadBuildArtifact_Call{Call: _e.mock.On("DownloadBuildArtifact", product, build, file)} } func (_c *MockPortal_DownloadBuildArtifact_Call) Run(run func(product Product, build Build, file io.Writer)) *MockPortal_DownloadBuildArtifact_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 Product - if args[0] != nil { - arg0 = args[0].(Product) - } - var arg1 Build - if args[1] != nil { - arg1 = args[1].(Build) - } - var arg2 io.Writer - if args[2] != nil { - arg2 = args[2].(io.Writer) - } - run( - arg0, - arg1, - arg2, - ) + run(args[0].(Product), args[1].(Build), args[2].(io.Writer)) }) return _c } @@ -134,32 +117,16 @@ type MockPortal_GetBuild_Call struct { } // GetBuild is a helper method to define mock.On call -// - product Product -// - version string -// - hash string +// - product +// - version +// - hash func (_e *MockPortal_Expecter) GetBuild(product interface{}, version interface{}, hash interface{}) *MockPortal_GetBuild_Call { return &MockPortal_GetBuild_Call{Call: _e.mock.On("GetBuild", product, version, hash)} } func (_c *MockPortal_GetBuild_Call) Run(run func(product Product, version string, hash string)) *MockPortal_GetBuild_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 Product - if args[0] != nil { - arg0 = args[0].(Product) - } - var arg1 string - if args[1] != nil { - arg1 = args[1].(string) - } - var arg2 string - if args[2] != nil { - arg2 = args[2].(string) - } - run( - arg0, - arg1, - arg2, - ) + run(args[0].(Product), args[1].(string), args[2].(string)) }) return _c } @@ -174,32 +141,6 @@ func (_c *MockPortal_GetBuild_Call) RunAndReturn(run func(product Product, versi return _c } -// ListBuilds provides a mock function for the type MockPortal -func (_mock *MockPortal) ListBuilds(product Product) (Builds, error) { - ret := _mock.Called(product) - - if len(ret) == 0 { - panic("no return value specified for ListBuilds") - } - - var r0 Builds - var r1 error - if returnFunc, ok := ret.Get(0).(func(Product) (Builds, error)); ok { - return returnFunc(product) - } - if returnFunc, ok := ret.Get(0).(func(Product) Builds); ok { - r0 = returnFunc(product) - } else { - r0 = ret.Get(0).(Builds) - } - if returnFunc, ok := ret.Get(1).(func(Product) error); ok { - r1 = returnFunc(product) - } else { - r1 = ret.Error(1) - } - return r0, r1 -} - // ListAPIKeys provides a mock function for the type MockPortal func (_mock *MockPortal) ListAPIKeys() ([]ApiKey, error) { ret := _mock.Called() @@ -216,7 +157,9 @@ func (_mock *MockPortal) ListAPIKeys() ([]ApiKey, error) { if returnFunc, ok := ret.Get(0).(func() []ApiKey); ok { r0 = returnFunc() } else { - r0 = ret.Get(0).([]ApiKey) + if ret.Get(0) != nil { + r0 = ret.Get(0).([]ApiKey) + } } if returnFunc, ok := ret.Get(1).(func() error); ok { r1 = returnFunc() @@ -243,8 +186,8 @@ func (_c *MockPortal_ListAPIKeys_Call) Run(run func()) *MockPortal_ListAPIKeys_C return _c } -func (_c *MockPortal_ListAPIKeys_Call) Return(keys []ApiKey, err error) *MockPortal_ListAPIKeys_Call { - _c.Call.Return(keys, err) +func (_c *MockPortal_ListAPIKeys_Call) Return(apiKeys []ApiKey, err error) *MockPortal_ListAPIKeys_Call { + _c.Call.Return(apiKeys, err) return _c } @@ -253,26 +196,46 @@ func (_c *MockPortal_ListAPIKeys_Call) RunAndReturn(run func() ([]ApiKey, error) return _c } +// ListBuilds provides a mock function for the type MockPortal +func (_mock *MockPortal) ListBuilds(product Product) (Builds, error) { + ret := _mock.Called(product) + + if len(ret) == 0 { + panic("no return value specified for ListBuilds") + } + + var r0 Builds + var r1 error + if returnFunc, ok := ret.Get(0).(func(Product) (Builds, error)); ok { + return returnFunc(product) + } + if returnFunc, ok := ret.Get(0).(func(Product) Builds); ok { + r0 = returnFunc(product) + } else { + r0 = ret.Get(0).(Builds) + } + if returnFunc, ok := ret.Get(1).(func(Product) error); ok { + r1 = returnFunc(product) + } else { + r1 = ret.Error(1) + } + return r0, r1 +} + // MockPortal_ListBuilds_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListBuilds' type MockPortal_ListBuilds_Call struct { *mock.Call } // ListBuilds is a helper method to define mock.On call -// - product Product +// - product func (_e *MockPortal_Expecter) ListBuilds(product interface{}) *MockPortal_ListBuilds_Call { return &MockPortal_ListBuilds_Call{Call: _e.mock.On("ListBuilds", product)} } func (_c *MockPortal_ListBuilds_Call) Run(run func(product Product)) *MockPortal_ListBuilds_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 Product - if args[0] != nil { - arg0 = args[0].(Product) - } - run( - arg0, - ) + run(args[0].(Product)) }) return _c } @@ -310,38 +273,17 @@ type MockPortal_RegisterAPIKey_Call struct { } // RegisterAPIKey is a helper method to define mock.On call -// - owner string -// - organization string -// - role string -// - expiresAt time.Time +// - owner +// - organization +// - role +// - expiresAt func (_e *MockPortal_Expecter) RegisterAPIKey(owner interface{}, organization interface{}, role interface{}, expiresAt interface{}) *MockPortal_RegisterAPIKey_Call { return &MockPortal_RegisterAPIKey_Call{Call: _e.mock.On("RegisterAPIKey", owner, organization, role, expiresAt)} } func (_c *MockPortal_RegisterAPIKey_Call) Run(run func(owner string, organization string, role string, expiresAt time.Time)) *MockPortal_RegisterAPIKey_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 string - if args[0] != nil { - arg0 = args[0].(string) - } - var arg1 string - if args[1] != nil { - arg1 = args[1].(string) - } - var arg2 string - if args[2] != nil { - arg2 = args[2].(string) - } - var arg3 time.Time - if args[3] != nil { - arg3 = args[3].(time.Time) - } - run( - arg0, - arg1, - arg2, - arg3, - ) + run(args[0].(string), args[1].(string), args[2].(string), args[3].(time.Time)) }) return _c } @@ -379,20 +321,14 @@ type MockPortal_RevokeAPIKey_Call struct { } // RevokeAPIKey is a helper method to define mock.On call -// - key string +// - key func (_e *MockPortal_Expecter) RevokeAPIKey(key interface{}) *MockPortal_RevokeAPIKey_Call { return &MockPortal_RevokeAPIKey_Call{Call: _e.mock.On("RevokeAPIKey", key)} } func (_c *MockPortal_RevokeAPIKey_Call) Run(run func(key string)) *MockPortal_RevokeAPIKey_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 string - if args[0] != nil { - arg0 = args[0].(string) - } - run( - arg0, - ) + run(args[0].(string)) }) return _c } @@ -430,26 +366,15 @@ type MockPortal_UpdateAPIKey_Call struct { } // UpdateAPIKey is a helper method to define mock.On call -// - key string -// - expiresAt time.Time +// - key +// - expiresAt func (_e *MockPortal_Expecter) UpdateAPIKey(key interface{}, expiresAt interface{}) *MockPortal_UpdateAPIKey_Call { return &MockPortal_UpdateAPIKey_Call{Call: _e.mock.On("UpdateAPIKey", key, expiresAt)} } func (_c *MockPortal_UpdateAPIKey_Call) Run(run func(key string, expiresAt time.Time)) *MockPortal_UpdateAPIKey_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 string - if args[0] != nil { - arg0 = args[0].(string) - } - var arg1 time.Time - if args[1] != nil { - arg1 = args[1].(time.Time) - } - run( - arg0, - arg1, - ) + run(args[0].(string), args[1].(time.Time)) }) return _c } @@ -525,20 +450,14 @@ type MockHttpClient_Do_Call struct { } // Do is a helper method to define mock.On call -// - request *http.Request +// - request func (_e *MockHttpClient_Expecter) Do(request interface{}) *MockHttpClient_Do_Call { return &MockHttpClient_Do_Call{Call: _e.mock.On("Do", request)} } func (_c *MockHttpClient_Do_Call) Run(run func(request *http.Request)) *MockHttpClient_Do_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 *http.Request - if args[0] != nil { - arg0 = args[0].(*http.Request) - } - run( - arg0, - ) + run(args[0].(*http.Request)) }) return _c } diff --git a/internal/util/mocks.go b/internal/util/mocks.go index 72ff7a65..c9fcabf6 100644 --- a/internal/util/mocks.go +++ b/internal/util/mocks.go @@ -5,9 +5,10 @@ package util import ( + "os" + "github.com/jedib0t/go-pretty/v6/table" mock "github.com/stretchr/testify/mock" - "os" ) // NewMockFileIO creates a new instance of MockFileIO. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. @@ -71,20 +72,14 @@ type MockFileIO_Create_Call struct { } // Create is a helper method to define mock.On call -// - filename string -func (_e *MockFileWriter_Expecter) Create(filename interface{}) *MockFileWriter_Create_Call { - return &MockFileWriter_Create_Call{Call: _e.mock.On("Create", filename)} +// - filename +func (_e *MockFileIO_Expecter) Create(filename interface{}) *MockFileIO_Create_Call { + return &MockFileIO_Create_Call{Call: _e.mock.On("Create", filename)} } func (_c *MockFileIO_Create_Call) Run(run func(filename string)) *MockFileIO_Create_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 string - if args[0] != nil { - arg0 = args[0].(string) - } - run( - arg0, - ) + run(args[0].(string)) }) return _c } @@ -473,8 +468,8 @@ type MockTableWriter_AppendHeader_Call struct { } // AppendHeader is a helper method to define mock.On call -// - row table.Row -// - configs ...table.RowConfig +// - row +// - configs func (_e *MockTableWriter_Expecter) AppendHeader(row interface{}, configs ...interface{}) *MockTableWriter_AppendHeader_Call { return &MockTableWriter_AppendHeader_Call{Call: _e.mock.On("AppendHeader", append([]interface{}{row}, configs...)...)} @@ -482,20 +477,13 @@ func (_e *MockTableWriter_Expecter) AppendHeader(row interface{}, configs ...int func (_c *MockTableWriter_AppendHeader_Call) Run(run func(row table.Row, configs ...table.RowConfig)) *MockTableWriter_AppendHeader_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 table.Row - if args[0] != nil { - arg0 = args[0].(table.Row) + variadicArgs := make([]table.RowConfig, len(args)-1) + for i, a := range args[1:] { + if a != nil { + variadicArgs[i] = a.(table.RowConfig) + } } - var arg1 []table.RowConfig - var variadicArgs []table.RowConfig - if len(args) > 1 { - variadicArgs = args[1].([]table.RowConfig) - } - arg1 = variadicArgs - run( - arg0, - arg1..., - ) + run(args[0].(table.Row), variadicArgs...) }) return _c } @@ -527,8 +515,8 @@ type MockTableWriter_AppendRow_Call struct { } // AppendRow is a helper method to define mock.On call -// - row table.Row -// - configs ...table.RowConfig +// - row +// - configs func (_e *MockTableWriter_Expecter) AppendRow(row interface{}, configs ...interface{}) *MockTableWriter_AppendRow_Call { return &MockTableWriter_AppendRow_Call{Call: _e.mock.On("AppendRow", append([]interface{}{row}, configs...)...)} @@ -536,20 +524,13 @@ func (_e *MockTableWriter_Expecter) AppendRow(row interface{}, configs ...interf func (_c *MockTableWriter_AppendRow_Call) Run(run func(row table.Row, configs ...table.RowConfig)) *MockTableWriter_AppendRow_Call { _c.Call.Run(func(args mock.Arguments) { - var arg0 table.Row - if args[0] != nil { - arg0 = args[0].(table.Row) - } - var arg1 []table.RowConfig - var variadicArgs []table.RowConfig - if len(args) > 1 { - variadicArgs = args[1].([]table.RowConfig) + variadicArgs := make([]table.RowConfig, len(args)-1) + for i, a := range args[1:] { + if a != nil { + variadicArgs[i] = a.(table.RowConfig) + } } - arg1 = variadicArgs - run( - arg0, - arg1..., - ) + run(args[0].(table.Row), variadicArgs...) }) return _c }