summaryrefslogtreecommitdiff
path: root/cmd/podman/secrets/inspect.go
blob: 1fcc676b4dd6dc4c3ba5ada535e98f4b42370e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package secrets

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/containers/common/pkg/report"
	"github.com/containers/podman/v4/cmd/podman/common"
	"github.com/containers/podman/v4/cmd/podman/registry"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/spf13/cobra"
)

var (
	inspectCmd = &cobra.Command{
		Use:               "inspect [options] SECRET [SECRET...]",
		Short:             "Inspect a secret",
		Long:              "Display detail information on one or more secrets",
		RunE:              inspect,
		Example:           "podman secret inspect MYSECRET",
		Args:              cobra.MinimumNArgs(1),
		ValidArgsFunction: common.AutocompleteSecrets,
	}
)

var format string

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Command: inspectCmd,
		Parent:  secretCmd,
	})
	flags := inspectCmd.Flags()
	formatFlagName := "format"
	flags.StringVar(&format, formatFlagName, "", "Format volume output using Go template")
	_ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SecretInfoReport{}))
}

func inspect(cmd *cobra.Command, args []string) error {
	inspected, errs, _ := registry.ContainerEngine().SecretInspect(context.Background(), args)

	// always print valid list
	if len(inspected) == 0 {
		inspected = []*entities.SecretInfoReport{}
	}

	if cmd.Flags().Changed("format") {
		row := report.NormalizeFormat(format)
		formatted := report.EnforceRange(row)

		tmpl, err := report.NewTemplate("inspect").Parse(formatted)
		if err != nil {
			return err
		}

		w, err := report.NewWriterDefault(os.Stdout)
		if err != nil {
			return err
		}
		defer w.Flush()
		if err := tmpl.Execute(w, inspected); err != nil {
			return err
		}
	} else {
		buf, err := json.MarshalIndent(inspected, "", "    ")
		if err != nil {
			return err
		}
		fmt.Println(string(buf))
	}

	if len(errs) > 0 {
		if len(errs) > 1 {
			for _, err := range errs[1:] {
				fmt.Fprintf(os.Stderr, "error inspecting secret: %v\n", err)
			}
		}
		return fmt.Errorf("inspecting secret: %w", errs[0])
	}
	return nil
}