@@ -22,10 +22,12 @@ THE SOFTWARE.
2222package cluster
2323
2424import (
25+ "context"
2526 "errors"
2627 "fmt"
2728 "os"
2829 "path"
30+ "strings"
2931
3032 "github.com/k3d-io/k3d/v5/cmd/util"
3133 cliconfig "github.com/k3d-io/k3d/v5/cmd/util/config"
@@ -35,13 +37,31 @@ import (
3537 "github.com/k3d-io/k3d/v5/pkg/runtimes"
3638 k3d "github.com/k3d-io/k3d/v5/pkg/types"
3739 k3dutil "github.com/k3d-io/k3d/v5/pkg/util"
40+ "github.com/sirupsen/logrus"
41+ "sigs.k8s.io/yaml"
3842
3943 "github.com/spf13/cobra"
4044 "github.com/spf13/viper"
4145)
4246
43- var clusterDeleteConfigFile string
44- var clusterDeleteCfgViper = viper .New ()
47+ var (
48+ clusterDeleteCfgViper = viper .New ()
49+ clusterDeletePpViper = viper .New ()
50+ )
51+
52+ func initClusterDeleteConfig () error {
53+ // Viper for pre-processed config options
54+ clusterDeletePpViper .SetEnvPrefix ("K3D" )
55+ clusterDeletePpViper .AutomaticEnv ()
56+ clusterDeletePpViper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
57+
58+ if l .Log ().GetLevel () >= logrus .DebugLevel {
59+ c , _ := yaml .Marshal (clusterDeletePpViper .AllSettings ())
60+ l .Log ().Debugf ("Additional CLI Configuration:\n %s" , c )
61+ }
62+
63+ return cliconfig .InitViperWithConfigFile (clusterDeleteCfgViper , clusterDeletePpViper .GetString ("config" ))
64+ }
4565
4666// NewCmdClusterDelete returns a new cobra command
4767func NewCmdClusterDelete () * cobra.Command {
@@ -54,7 +74,7 @@ func NewCmdClusterDelete() *cobra.Command {
5474 Args : cobra .MinimumNArgs (0 ), // 0 or n arguments; 0 = default cluster name
5575 ValidArgsFunction : util .ValidArgsAvailableClusters ,
5676 PreRunE : func (cmd * cobra.Command , args []string ) error {
57- return cliconfig . InitViperWithConfigFile ( clusterDeleteCfgViper , clusterDeleteConfigFile )
77+ return initClusterDeleteConfig ( )
5878 },
5979 Run : func (cmd * cobra.Command , args []string ) {
6080 clusters := parseDeleteClusterCmd (cmd , args )
@@ -99,7 +119,8 @@ func NewCmdClusterDelete() *cobra.Command {
99119 * Config File *
100120 ***************/
101121
102- cmd .Flags ().StringVarP (& clusterDeleteConfigFile , "config" , "c" , "" , "Path of a config file to use" )
122+ cmd .Flags ().StringP ("config" , "c" , "" , "Path of a config file to use" )
123+ _ = clusterDeletePpViper .BindPFlag ("config" , cmd .Flags ().Lookup ("config" ))
103124 if err := cmd .MarkFlagFilename ("config" , "yaml" , "yml" ); err != nil {
104125 l .Log ().Fatalln ("Failed to mark flag 'config' as filename flag" )
105126 }
@@ -110,66 +131,54 @@ func NewCmdClusterDelete() *cobra.Command {
110131
111132// parseDeleteClusterCmd parses the command input into variables required to delete clusters
112133func parseDeleteClusterCmd (cmd * cobra.Command , args []string ) []* k3d.Cluster {
113- var clusters []* k3d.Cluster
114-
115134 // --all
116135 all , err := cmd .Flags ().GetBool ("all" )
117136 if err != nil {
118137 l .Log ().Fatalln (err )
119138 }
120139
121- // --config
122- if clusterDeleteConfigFile != "" {
123- // not allowed with --all or more args
124- if len (args ) > 0 || all {
125- l .Log ().Fatalln ("failed to delete cluster: cannot use `--config` flag with additional arguments or `--all`" )
126- }
127-
128- cfg , err := config .SimpleConfigFromViper (clusterDeleteCfgViper )
129- if err != nil {
130- l .Log ().Fatalln (err )
131- }
132-
133- if cfg .Name == "" {
134- l .Log ().Fatalln ("failed to delete cluster via config file: no name in config file" )
135- }
136-
137- c , err := client .ClusterGet (cmd .Context (), runtimes .SelectedRuntime , & k3d.Cluster {Name : cfg .Name })
138- if errors .Is (err , client .ClusterGetNoNodesFoundError ) {
139- l .Log ().Infof ("No nodes found for cluster '%s', nothing to delete." , cfg .Name )
140- return nil
141- }
142-
143- clusters = append (clusters , c )
144- return clusters
145- }
146-
147140 // --all was set
148141 if all {
149142 l .Log ().Infoln ("Deleting all clusters..." )
150- clusters , err = client .ClusterList (cmd .Context (), runtimes .SelectedRuntime )
143+ clusters , err : = client .ClusterList (cmd .Context (), runtimes .SelectedRuntime )
151144 if err != nil {
152145 l .Log ().Fatalln (err )
153146 }
154147 return clusters
155148 }
156149
157- // args only
158- clusternames := []string {k3d .DefaultClusterName }
150+ // args
159151 if len (args ) != 0 {
160- clusternames = args
152+ return getClusters (cmd .Context (), args ... )
153+ }
154+
155+ // --config
156+ if clusterDeletePpViper .GetString ("config" ) != "" {
157+ cfg , err := config .SimpleConfigFromViper (clusterDeleteCfgViper )
158+ if err != nil {
159+ l .Log ().Fatalln (err )
160+ }
161+ if cfg .Name != "" {
162+ return getClusters (cmd .Context (), cfg .Name )
163+ }
161164 }
162165
166+ // default
167+ return getClusters (cmd .Context (), k3d .DefaultClusterName )
168+ }
169+
170+ func getClusters (ctx context.Context , clusternames ... string ) []* k3d.Cluster {
171+ var clusters []* k3d.Cluster
163172 for _ , name := range clusternames {
164- c , err := client .ClusterGet (cmd . Context () , runtimes .SelectedRuntime , & k3d.Cluster {Name : name })
173+ c , err := client .ClusterGet (ctx , runtimes .SelectedRuntime , & k3d.Cluster {Name : name })
165174 if err != nil {
166- if err == client .ClusterGetNoNodesFoundError {
175+ if errors .Is (err , client .ClusterGetNoNodesFoundError ) {
176+ l .Log ().Infof ("No nodes found for cluster '%s', nothing to delete." , name )
167177 continue
168178 }
169179 l .Log ().Fatalln (err )
170180 }
171181 clusters = append (clusters , c )
172182 }
173-
174183 return clusters
175184}
0 commit comments