Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fleetctl: Add option to specify id for lock/release #118

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fleetctl/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 22 additions & 1 deletion pkg/fleetctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading