mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 18:38:40 +00:00
refactor: introduce experimental cli package (#8222)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
2ee28fff04
commit
5c482646d1
3 changed files with 79 additions and 11 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apply"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/create"
|
||||
|
@ -13,13 +12,12 @@ import (
|
|||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/jp"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/oci"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/experimental"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/version"
|
||||
"github.com/kyverno/kyverno/pkg/logging"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const enableExperimentalEnv = "KYVERNO_EXPERIMENTAL"
|
||||
|
||||
func main() {
|
||||
cli := &cobra.Command{
|
||||
Use: "kyverno",
|
||||
|
@ -42,13 +40,6 @@ func configureLogs(cli *cobra.Command) {
|
|||
cli.PersistentFlags().AddGoFlagSet(flag.CommandLine)
|
||||
}
|
||||
|
||||
func enableExperimental() bool {
|
||||
if b, err := strconv.ParseBool(os.Getenv(enableExperimentalEnv)); err == nil {
|
||||
return b
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func registerCommands(cli *cobra.Command) {
|
||||
cli.AddCommand(
|
||||
apply.Command(),
|
||||
|
@ -58,7 +49,7 @@ func registerCommands(cli *cobra.Command) {
|
|||
test.Command(),
|
||||
version.Command(),
|
||||
)
|
||||
if enableExperimental() {
|
||||
if experimental.IsExperimentalEnabled() {
|
||||
cli.AddCommand(
|
||||
fix.Command(),
|
||||
oci.Command(),
|
||||
|
|
15
cmd/cli/kubectl-kyverno/utils/experimental/experimental.go
Normal file
15
cmd/cli/kubectl-kyverno/utils/experimental/experimental.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package experimental
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const experimentalEnv = "KYVERNO_EXPERIMENTAL"
|
||||
|
||||
func IsExperimentalEnabled() bool {
|
||||
if b, err := strconv.ParseBool(os.Getenv(experimentalEnv)); err == nil {
|
||||
return b
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package experimental
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsExperimentalEnabled(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "enabled",
|
||||
env: map[string]string{
|
||||
experimentalEnv: "true",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "enabled",
|
||||
env: map[string]string{
|
||||
experimentalEnv: "1",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "enabled",
|
||||
env: map[string]string{
|
||||
experimentalEnv: "t",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "disabled",
|
||||
env: map[string]string{
|
||||
experimentalEnv: "false",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "not specified",
|
||||
env: map[string]string{},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "bad format",
|
||||
env: map[string]string{
|
||||
experimentalEnv: "maybe",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for k, v := range tt.env {
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
if got := IsExperimentalEnabled(); got != tt.want {
|
||||
t.Errorf("IsExperimentalEnabled() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue