Skip to content

Commit

Permalink
finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
Telemaco019 committed Jul 6, 2024
1 parent d7ba826 commit eccbbea
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
7 changes: 6 additions & 1 deletion pkg/cmd/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/telemaco019/duplik8s/pkg/core"
"github.com/telemaco019/duplik8s/pkg/duplicators"
"github.com/telemaco019/duplik8s/pkg/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func NewDeployCmd(duplicator core.Duplicator, client core.Client) *cobra.Command {
Expand All @@ -35,7 +36,11 @@ func NewDeployCmd(duplicator core.Duplicator, client core.Client) *cobra.Command
Short: "Duplicate a Deployment.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
run := newDuplicateCmd(factory, client, "Select a Deployment")
run := newDuplicateCmd(factory, client, schema.GroupVersionResource{
Group: "apps",
Version: "v1",
Resource: "deployments",
})
return run(cmd, args)
},
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/telemaco019/duplik8s/pkg/core"
"github.com/telemaco019/duplik8s/pkg/duplicators"
"github.com/telemaco019/duplik8s/pkg/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func NewPodCmd(duplicator core.Duplicator, client core.Client) *cobra.Command {
Expand All @@ -35,7 +36,11 @@ func NewPodCmd(duplicator core.Duplicator, client core.Client) *cobra.Command {
Short: "Duplicate a Pod.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
run := newDuplicateCmd(factory, client, "Select a Pod")
run := newDuplicateCmd(factory, client, schema.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
})
return run(cmd, args)
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Test_NoPodsAvailable(t *testing.T) {
mocks.ListPodsResult{},
nil,
)
cmd := NewRootCmd(podClient, nil)
cmd := NewRootCmd(podClient, podClient)

Check failure on line 32 in pkg/cmd/pod_test.go

View workflow job for this annotation

GitHub Actions / Check

cannot use podClient (variable of type *mocks.PodClient) as core.Client value in argument to NewRootCmd: *mocks.PodClient does not implement core.Client (wrong type for method ListDuplicable)
output, err := test.ExecuteCommand(cmd, "pod")
assert.NotEmpty(t, output)
assert.Error(t, err)
Expand All @@ -40,7 +40,7 @@ func Test_Success(t *testing.T) {
mocks.ListPodsResult{},
nil,
)
cmd := NewRootCmd(podClient, nil)
cmd := NewRootCmd(podClient, podClient)

Check failure on line 43 in pkg/cmd/pod_test.go

View workflow job for this annotation

GitHub Actions / Check

cannot use podClient (variable of type *mocks.PodClient) as core.Client value in argument to NewRootCmd: *mocks.PodClient does not implement core.Client (wrong type for method ListDuplicable)
_, err := test.ExecuteCommand(cmd, "pod", "pod-1")
assert.NoError(t, err)
}
Expand All @@ -50,7 +50,7 @@ func Test_DuplicateError(t *testing.T) {
mocks.ListPodsResult{},
fmt.Errorf("error"),
)
cmd := NewRootCmd(podClient, nil)
cmd := NewRootCmd(podClient, podClient)

Check failure on line 53 in pkg/cmd/pod_test.go

View workflow job for this annotation

GitHub Actions / Check

cannot use podClient (variable of type *mocks.PodClient) as core.Client value in argument to NewRootCmd: *mocks.PodClient does not implement core.Client (wrong type for method ListDuplicable)
_, err := test.ExecuteCommand(cmd, "pod", "pod-1")
assert.EqualError(t, err, "error")
}
49 changes: 41 additions & 8 deletions pkg/cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@
package cmd

import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/telemaco019/duplik8s/pkg/clients"
"github.com/telemaco019/duplik8s/pkg/cmd/flags"
"github.com/telemaco019/duplik8s/pkg/core"
"github.com/telemaco019/duplik8s/pkg/utils"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type duplicatorFactory func(opts utils.KubeOptions) (core.Duplicator, error)

func newDuplicateCmd(factory duplicatorFactory, client core.Client, selectMessage string) func(cmd *cobra.Command, args []string) error {
func newDuplicateCmd(newDuplicator duplicatorFactory, client core.Client, gvr schema.GroupVersionResource) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
opts, err := NewKubeOptions(cmd, args)
if err != nil {
return err
}
duplicator, err := factory(opts)
duplicator, err := newDuplicator(opts)
if err != nil {
return err
}
if client == nil {
client, err = clients.NewDuplik8sClient(opts)
if err != nil {
return err
}
}
cmdOverride, err := cmd.Flags().GetStringSlice(flags.COMMAND_OVERRIDE)
if err != nil {
return err
Expand All @@ -51,19 +63,40 @@ func newDuplicateCmd(factory duplicatorFactory, client core.Client, selectMessag
Args: argsOverride,
}

// If available, duplicate the resource provided as argument
var obj core.DuplicableObject
if len(args) == 0 {
obj, err = utils.SelectItem(client, opts.Namespace, selectMessage)
if err != nil {
return err
}
} else {
if len(args) > 0 {
obj = core.DuplicableObject{
Name: args[0],
Namespace: opts.Namespace,
}
return duplicator.Duplicate(obj, options)
}

// Otherwise, list available resources
objs, err := client.ListDuplicable(
context.Background(),
gvr,
opts.Namespace,
)
if err != nil {
return err
}
if len(objs) == 0 {
return fmt.Errorf("no %s available in namespace %q", gvr.Resource, opts.Namespace)
}
caser := cases.Title(language.English)
obj, err = utils.SelectItem(
objs,
fmt.Sprintf("%s [%s]", caser.String(gvr.Resource), opts.Namespace),
)
if err != nil {
return err
}
obj = core.DuplicableObject{
Name: args[0],
Namespace: opts.Namespace,
}
return duplicator.Duplicate(obj, options)
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/telemaco019/duplik8s/pkg/core"
"github.com/telemaco019/duplik8s/pkg/duplicators"
"github.com/telemaco019/duplik8s/pkg/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func NewStatefulSetCmd(duplicator core.Duplicator, client core.Client) *cobra.Command {
Expand All @@ -35,7 +36,11 @@ func NewStatefulSetCmd(duplicator core.Duplicator, client core.Client) *cobra.Co
Short: "Duplicate a StatefulSet.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
run := newDuplicateCmd(factory, client, "Select a StatefulSet")
run := newDuplicateCmd(factory, client, schema.GroupVersionResource{
Group: "apps",
Version: "v1",
Resource: "statefulsets",
})
return run(cmd, args)
},
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"context"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -27,7 +28,7 @@ type Duplicator interface {
}

type Client interface {
ListDuplicable(namespace string) ([]DuplicableObject, error)
ListDuplicable(ctx context.Context, resource schema.GroupVersionResource, namespace string) ([]DuplicableObject, error)
}

type PodOverrideOptions struct {
Expand Down
5 changes: 4 additions & 1 deletion pkg/test/mocks/pod_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type ListPodsResult struct {
func NewListPodResults(pods []string, namespace string, err error) ListPodsResult {
var objs = make([]core.DuplicableObject, 0)
for _, pod := range pods {
objs = append(objs, core.NewPod(pod, namespace))
objs = append(objs, core.DuplicableObject{
Name: pod,
Namespace: namespace,
})
}
return ListPodsResult{
Objs: objs,
Expand Down
20 changes: 6 additions & 14 deletions pkg/utils/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,18 @@
package utils

import (
"fmt"
"github.com/charmbracelet/huh"
"github.com/telemaco019/duplik8s/pkg/core"
)

func SelectItem(client core.Client, namespace, selectMessage string) (core.DuplicableObject, error) {
var selected = core.DuplicableObject{}
objs, err := client.ListDuplicable(namespace)
if err != nil {
return selected, err
}
if len(objs) == 0 {
return selected, fmt.Errorf("no Pods found in namespace %q", namespace)
}
options := make([]huh.Option[core.DuplicableObject], len(objs))
for i, o := range objs {
func SelectItem(items []core.DuplicableObject, selectMessage string) (core.DuplicableObject, error) {
var selected core.DuplicableObject
options := make([]huh.Option[core.DuplicableObject], len(items))
for i, o := range items {
options[i] = huh.NewOption(o.Name, o)
}
err = huh.NewSelect[core.DuplicableObject]().
Title(fmt.Sprintf("%s [%s]", selectMessage, namespace)).
err := huh.NewSelect[core.DuplicableObject]().
Title(selectMessage).
Options(options...).
Value(&selected).
Run()
Expand Down

0 comments on commit eccbbea

Please sign in to comment.