diff options
author | Charlie Doern <cdoern@redhat.com> | 2022-07-12 15:23:45 -0400 |
---|---|---|
committer | Charlie Doern <cdoern@redhat.com> | 2022-08-04 13:59:58 -0400 |
commit | 842c6c7c6748f8705698d25a29945f8437f1bed2 (patch) | |
tree | 944a6f4e50f76a9f5a2d5e1eae0f698a69f39fd4 /cmd/podman/generate | |
parent | 1cf6afb788a681fc379d58d7f1cb69eb1690dba9 (diff) | |
download | podman-842c6c7c6748f8705698d25a29945f8437f1bed2.tar.gz podman-842c6c7c6748f8705698d25a29945f8437f1bed2.tar.bz2 podman-842c6c7c6748f8705698d25a29945f8437f1bed2.zip |
podman generate spec
implement a new command `podman generate spec` which can formulate a json specgen to be consumed by both the pod
and container creation API.
supported flags are
--verbose (default true) print output to the terminal
--compact print the json output in a single line format to be piped to the API
--filename put the output in a file
--clone rename the pod/ctr in the spec so it won't conflict w/ an existing entity
Signed-off-by: Charlie Doern <cdoern@redhat.com>
Diffstat (limited to 'cmd/podman/generate')
-rw-r--r-- | cmd/podman/generate/spec.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/cmd/podman/generate/spec.go b/cmd/podman/generate/spec.go new file mode 100644 index 000000000..1cf967424 --- /dev/null +++ b/cmd/podman/generate/spec.go @@ -0,0 +1,71 @@ +package pods + +import ( + "fmt" + "io/ioutil" + + "github.com/containers/common/pkg/completion" + "github.com/containers/podman/v4/cmd/podman/common" + "github.com/containers/podman/v4/cmd/podman/registry" + "github.com/containers/podman/v4/cmd/podman/utils" + "github.com/containers/podman/v4/pkg/domain/entities" + "github.com/spf13/cobra" +) + +var ( + specCmd = &cobra.Command{ + Use: "spec [options] {CONTAINER|POD}", + Short: "Generate Specgen JSON based on containers or pods", + Long: "Generate Specgen JSON based on containers or pods", + RunE: spec, + Args: cobra.ExactArgs(1), + ValidArgsFunction: common.AutocompleteContainersAndPods, + Example: `podman generate spec ctrID`, + } +) + +var ( + opts *entities.GenerateSpecOptions +) + +func init() { + registry.Commands = append(registry.Commands, registry.CliCommand{ + Command: specCmd, + Parent: generateCmd, + }) + opts = &entities.GenerateSpecOptions{} + flags := specCmd.Flags() + + filenameFlagName := "filename" + flags.StringVarP(&opts.FileName, filenameFlagName, "f", "", "Write output to the specified path") + _ = specCmd.RegisterFlagCompletionFunc(filenameFlagName, completion.AutocompleteNone) + + compactFlagName := "compact" + flags.BoolVarP(&opts.Compact, compactFlagName, "c", false, "Print the json in a compact format for consumption") + + nameFlagName := "name" + flags.BoolVarP(&opts.Name, nameFlagName, "n", true, "Specify a new name for the generated spec") + + flags.SetNormalizeFunc(utils.AliasFlags) +} + +func spec(cmd *cobra.Command, args []string) error { + opts.ID = args[0] + report, err := registry.ContainerEngine().GenerateSpec(registry.GetContext(), opts) + if err != nil { + return err + } + + // if we are looking to print the output, do not mess it up by printing the path + // if we are using -v the user probably expects to pipe the output somewhere else + if len(opts.FileName) > 0 { + err = ioutil.WriteFile(opts.FileName, report.Data, 0644) + if err != nil { + return err + } + fmt.Println(opts.FileName) + } else { + fmt.Println(string(report.Data)) + } + return nil +} |