From 5fea8fad693753389f35eba07d9a2b5259ed30f2 Mon Sep 17 00:00:00 2001 From: Heathcliff Date: Mon, 23 Dec 2024 16:43:58 +0100 Subject: [PATCH] fleetctl: Add option to specify id for lock/release Signed-off-by: Heathcliff --- pkg/client/client.go | 24 ++++++++++++++++++++++++ pkg/client/client_test.go | 13 +++++++++++++ pkg/fleetctl/release.go | 2 +- pkg/fleetctl/utils.go | 23 ++++++++++++++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 469cc83..a7d3f14 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -145,3 +145,27 @@ func (c *FleetlockClient) SetGroup(group string) error { c.group = group return nil } + +// Get the fleetlock id +func (c *FleetlockClient) GetID() string { + if c == nil { + return "" + } + + c.mutex.RLock() + defer c.mutex.RUnlock() + + return c.appID +} + +// Change the fleetlock id +func (c *FleetlockClient) SetID(id string) error { + c.mutex.Lock() + defer c.mutex.Unlock() + + if id == "" { + return fmt.Errorf("the fleetlock id can't be empty") + } + c.appID = id + return nil +} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f559625..4f46508 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -126,6 +126,19 @@ func TestGetAndSet(t *testing.T) { assert.Error(c.SetGroup(""), "Should not accept empty group") }) + t.Run("ID", func(t *testing.T) { + assert := assert.New(t) + + var c *FleetlockClient + assert.Empty(c.GetID(), "Should not panic when reading id from nil pointer") + + c = &FleetlockClient{} + + assert.NoError(c.SetID("testid"), "Should set id without error") + assert.Equal("testid", c.GetID(), "id should match") + + assert.Error(c.SetID(""), "Should not accept empty id") + }) } func NewFakeServer(t *testing.T, statusCode int, path string) (*FleetlockClient, *fake.FakeServer) { diff --git a/pkg/fleetctl/release.go b/pkg/fleetctl/release.go index aeb8df1..af7266f 100644 --- a/pkg/fleetctl/release.go +++ b/pkg/fleetctl/release.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -// Create a new lock command +// Create a new release command func NewReleaseCommand() *cobra.Command { cmd := &cobra.Command{ Use: "release", diff --git a/pkg/fleetctl/utils.go b/pkg/fleetctl/utils.go index 4c08237..a2482f4 100644 --- a/pkg/fleetctl/utils.go +++ b/pkg/fleetctl/utils.go @@ -10,21 +10,42 @@ import ( const ( flagNameGroup = "group" + flagNameID = "id" ) func addCommonFlagsToCMD(cmd *cobra.Command) { cmd.Flags().StringP(flagNameGroup, "g", "default", "Name of the lock group") + cmd.Flags().StringP(flagNameID, "i", "", "Specify the id to use, defaults to zincati appID") } +// Takes care if parsing the arguments and creating a client from them func getClientFromCMD(cmd *cobra.Command, args []string) (*client.FleetlockClient, error) { group, err := cmd.Flags().GetString(flagNameGroup) if err != nil { return nil, err } + + id, err := cmd.Flags().GetString(flagNameID) + if err != nil { + return nil, err + } + if len(args) < 1 { return nil, fmt.Errorf("missing url") } - return client.NewClient(args[0], group) + c, err := client.NewClient(args[0], group) + if err != nil { + return nil, err + } + + if id != "" { + err = c.SetID(id) + if err != nil { + return nil, err + } + } + + return c, nil } // Print the error information on stderr and exit with code 1