-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
844fba6
commit 26b1f56
Showing
16 changed files
with
273 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hostinger/fireactions/helper/printer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// newMicrovmsCmd returns the parent command for managing MicroVMs. | ||
func newMicrovmsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "microvm", | ||
Short: "Manage MicroVMs within a pool", | ||
GroupID: "microvm", | ||
} | ||
|
||
cmd.AddCommand(newMicrovmsListCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func newMicrovmsListCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list --pool <pool-name>", | ||
Short: "List all MicroVMs in the specified pool", | ||
Args: cobra.NoArgs, | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
poolName, err := cmd.Flags().GetString("pool") | ||
if err != nil { | ||
return fmt.Errorf("error retrieving pool flag: %w", err) | ||
} | ||
return runMicrovmsListCmd(cmd, poolName) | ||
}, | ||
} | ||
|
||
cmd.Flags().String("pool", "", "Pool name (required)") | ||
if err := cmd.MarkFlagRequired("pool"); err != nil { | ||
fmt.Fprintf(cmd.ErrOrStderr(), "failed to mark 'pool' flag as required: %v\n", err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runMicrovmsListCmd(cmd *cobra.Command, poolName string) error { | ||
microvms, _, err := client.ListMicroVMs(context.Background(), poolName) | ||
if err != nil { | ||
return fmt.Errorf("failed to list MicroVMs: %w", err) | ||
} | ||
|
||
printer.PrintText(microvms, cmd.OutOrStdout(), nil) | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/hostinger/fireactions" | ||
"github.com/hostinger/fireactions/commands/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMicrovmsList_Success(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockClient := mocks.NewClient(ctrl) | ||
mockResponse := &fireactions.Response{} | ||
mockMicroVMs := &fireactions.MicroVMs{} | ||
|
||
mockClient.EXPECT().ListMicroVMs(gomock.Any(), gomock.Any()).Return(mockMicroVMs, mockResponse, nil) | ||
|
||
client = mockClient | ||
|
||
cmd := newMicrovmsListCmd() | ||
if err := cmd.Flags().Set("pool", "pool-name"); err != nil { | ||
t.Fatalf("failed to set flag: %v", err) | ||
} | ||
|
||
err := cmd.RunE(cmd, []string{}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestMicrovmsList_Failure(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockClient := mocks.NewClient(ctrl) | ||
|
||
mockClient.EXPECT().ListMicroVMs(gomock.Any(), gomock.Any()).Return(nil, nil, fmt.Errorf("mock API failure")) | ||
|
||
client = mockClient | ||
|
||
cmd := newMicrovmsListCmd() | ||
|
||
if err := cmd.Flags().Set("pool", "pool-name"); err != nil { | ||
t.Fatalf("failed to set flag: %v", err) | ||
} | ||
|
||
err := cmd.RunE(cmd, []string{}) | ||
assert.ErrorContains(t, err, "mock API failure") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.