Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions internal/system/cmd/module/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"

"github.com/deckhouse/deckhouse-cli/internal/system/cmd/edit/flags"
"github.com/deckhouse/deckhouse-cli/internal/system/cmd/module/deckhouse"
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
)
Expand All @@ -32,6 +31,10 @@ List enabled Deckhouse Kubernetes Platform modules.

© Flant JSC 2025`)

// The /module/list debug endpoint also matches ".text" in its route,
// but renders structured payloads as JSON anyway, so text is not offered.
var outputFormats = []string{"yaml", "json"}

func NewCommand() *cobra.Command {
listCmd := &cobra.Command{
Use: "list",
Expand All @@ -41,12 +44,17 @@ func NewCommand() *cobra.Command {
SilenceUsage: true,
RunE: listModule,
}
flags.AddFlags(listCmd.Flags())
utilk8s.AddOutputFlag(listCmd, "yaml", outputFormats...)

return listCmd
}

func listModule(cmd *cobra.Command, _ []string) error {
format, err := utilk8s.GetOutputFormat(cmd, outputFormats...)
if err != nil {
return err
}

kubeconfigPath, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
Expand All @@ -62,7 +70,7 @@ func listModule(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
}

err = deckhouse.QueryAPI(config, kubeCl, "list.yaml")
err = deckhouse.QueryAPI(config, kubeCl, "list."+format)
if err != nil {
return fmt.Errorf("Error list modules: %w", err)
}
Expand Down
14 changes: 13 additions & 1 deletion internal/system/cmd/module/cmd/snapshots/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Dump module hooks snapshots.

© Flant JSC 2025`)

var outputFormats = []string{"yaml", "json"}

func NewCommand() *cobra.Command {
snapshotsCmd := &cobra.Command{
Use: "snapshots",
Expand All @@ -41,13 +43,23 @@ func NewCommand() *cobra.Command {
SilenceUsage: true,
RunE: snapshotsModule,
}
utilk8s.AddOutputFlag(snapshotsCmd, "yaml", outputFormats...)

return snapshotsCmd
}

func snapshotsModule(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("this command requires exactly 1 argument: module name")
}

moduleName := args[0]

format, err := utilk8s.GetOutputFormat(cmd, outputFormats...)
if err != nil {
return err
}

kubeconfigPath, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
Expand All @@ -63,7 +75,7 @@ func snapshotsModule(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
}

pathFromOption := fmt.Sprintf("%s/snapshots.yaml", moduleName)
pathFromOption := fmt.Sprintf("%s/snapshots.%s", moduleName, format)

err = deckhouse.QueryAPI(config, kubeCl, pathFromOption)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion internal/system/cmd/module/cmd/values/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Dump module hooks values.

© Flant JSC 2025`)

var outputFormats = []string{"yaml", "json"}

func NewCommand() *cobra.Command {
valuesCmd := &cobra.Command{
Use: "values",
Expand All @@ -41,6 +43,7 @@ func NewCommand() *cobra.Command {
SilenceUsage: true,
RunE: valuesModule,
}
utilk8s.AddOutputFlag(valuesCmd, "yaml", outputFormats...)

return valuesCmd
}
Expand All @@ -52,6 +55,11 @@ func valuesModule(cmd *cobra.Command, args []string) error {

moduleName := args[0]

format, err := utilk8s.GetOutputFormat(cmd, outputFormats...)
if err != nil {
return err
}

kubeconfigPath, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
Expand All @@ -67,7 +75,7 @@ func valuesModule(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Failed to setup Kubernetes client: %w", err)
}

pathFromOption := fmt.Sprintf("%s/values.yaml", moduleName)
pathFromOption := fmt.Sprintf("%s/values.%s", moduleName, format)

err = deckhouse.QueryAPI(config, kubeCl, pathFromOption)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions internal/utilk8s/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"strings"

"github.com/spf13/cobra"
Expand All @@ -35,6 +36,21 @@ func AddOutputFlag(cmd *cobra.Command, defaultFmt string, formats ...string) {
_ = cmd.RegisterFlagCompletionFunc("output", CompleteOutputFormats(formats...))
}

// GetOutputFormat reads the "-o/--output" flag declared by AddOutputFlag
// and validates it against the accepted formats.
func GetOutputFormat(cmd *cobra.Command, formats ...string) (string, error) {
format, err := cmd.Flags().GetString("output")
if err != nil {
return "", fmt.Errorf("reading output flag: %w", err)
}

if !slices.Contains(formats, format) {
return "", fmt.Errorf("unsupported output format %q; use %s", format, strings.Join(formats, "|"))
}

return format, nil
}

// PrintObject writes an unstructured Kubernetes object to w in the given format.
// Supported formats: "json", "yaml"; anything else prints Kind/Name.
func PrintObject(w io.Writer, obj *unstructured.Unstructured, format string) error {
Expand Down
78 changes: 78 additions & 0 deletions internal/utilk8s/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utilk8s

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestGetOutputFormat(t *testing.T) {
tests := []struct {
name string
formats []string
args []string
expected string
wantErr string
}{
{
name: "default format when flag is not set",
formats: []string{"yaml", "json"},
args: []string{},
expected: "yaml",
},
{
name: "explicit allowed format",
formats: []string{"yaml", "json"},
args: []string{"-o", "json"},
expected: "json",
},
{
name: "unsupported format",
formats: []string{"yaml", "json"},
args: []string{"-o", "xml"},
wantErr: `unsupported output format "xml"; use yaml|json`,
},
{
name: "flag not registered",
formats: nil,
args: []string{},
wantErr: "reading output flag",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
if tt.formats != nil {
AddOutputFlag(cmd, tt.formats[0], tt.formats...)
}
assert.NoError(t, cmd.Flags().Parse(tt.args))

format, err := GetOutputFormat(cmd, tt.formats...)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr)
return
}

assert.NoError(t, err)
assert.Equal(t, tt.expected, format)
})
}
}
Loading