Skip to content

Commit

Permalink
Add list command too
Browse files Browse the repository at this point in the history
Signed-off-by: Greg Haynes <[email protected]>
  • Loading branch information
greghaynes committed Feb 15, 2024
1 parent d415367 commit ee9a96f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
21 changes: 6 additions & 15 deletions pkg/cmd/delete/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package delete

import (
"flag"
"fmt"
"os"

"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/pkg/errors"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/kind/pkg/cluster"
)

var (
Expand All @@ -24,7 +23,7 @@ var DeleteCmd = &cobra.Command{
}

func init() {
DeleteCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name for build (Prefix for kind cluster name, pod names, etc).")
DeleteCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name of the kind cluster to be deleted.")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Expand All @@ -37,17 +36,9 @@ func init() {
}

func delete(cmd *cobra.Command, args []string) error {
if buildName == "" {
fmt.Print("Must specify buildName\n")
os.Exit(1)
}

cluster, err := kind.NewCluster(buildName, "", "", "", "")
if err != nil {
return err
}
if err := cluster.Delete(); err != nil {
return err
provider := cluster.NewProvider(cluster.ProviderWithDocker())
if err := provider.Delete(buildName, ""); err != nil {
return errors.Wrapf(err, "failed to delete cluster %q", buildName)
}
return nil
}
48 changes: 48 additions & 0 deletions pkg/cmd/list/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package list

import (
"flag"
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/kind/pkg/cluster"
)

var (
// Flags
buildName string
)

var ListCmd = &cobra.Command{
Use: "list",
Short: "List idp clusters",
Long: ``,
RunE: delete,
}

func init() {
ListCmd.PersistentFlags().StringVar(&buildName, "buildName", "localdev", "Name of the kind cluster to be deleted.")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Development: true,
}
opts.BindFlags(zapfs)
ListCmd.Flags().AddGoFlagSet(zapfs)

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
}

func delete(cmd *cobra.Command, args []string) error {
provider := cluster.NewProvider(cluster.ProviderWithDocker())
clusters, err := provider.List()
if err != nil {
return errors.Wrapf(err, "failed to delete cluster %q", buildName)
}

fmt.Printf("Clusters: %v\n", clusters)
return nil
}
2 changes: 2 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cnoe-io/idpbuilder/pkg/cmd/create"
"github.com/cnoe-io/idpbuilder/pkg/cmd/delete"
"github.com/cnoe-io/idpbuilder/pkg/cmd/list"
"github.com/cnoe-io/idpbuilder/pkg/cmd/version"
"github.com/spf13/cobra"
)
Expand All @@ -19,6 +20,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(create.CreateCmd)
rootCmd.AddCommand(delete.DeleteCmd)
rootCmd.AddCommand(list.ListCmd)
rootCmd.AddCommand(version.VersionCmd)
}

Expand Down

0 comments on commit ee9a96f

Please sign in to comment.