From 16cf7fdf4197a376155d51b0b43df3a597e0d96b Mon Sep 17 00:00:00 2001 From: "Joel D. Elkins" Date: Tue, 26 Jul 2022 15:22:06 -0500 Subject: [PATCH] Add show subcommand --- cmd/show.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 cmd/show.go diff --git a/cmd/show.go b/cmd/show.go new file mode 100644 index 0000000..0ff2f93 --- /dev/null +++ b/cmd/show.go @@ -0,0 +1,59 @@ +/* +Copyright © 2022 Joel D. Elkins + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + "os" + + "gitea.elkins.co/Networking/ccl/internal/pkg/config" + "gitea.elkins.co/Networking/ccl/internal/pkg/container" + toml "github.com/pelletier/go-toml" + "github.com/spf13/cobra" +) + +// showCmd represents the show command +var showCmd = &cobra.Command{ + Use: "show", + Aliases: []string{"dump", "info"}, + Short: "Show container details in toml format", + ValidArgsFunction: validNouns, + Long: `Output a toml format for the listed containers, which should be adequate +to use as a ccl config file.`, + Run: func(cmd *cobra.Command, args []string) { + type parse struct { + Containers []container.Container + } + + conts := config.Union(args) + output, err := toml.Marshal(parse{conts}) + if err != nil { + fmt.Println("could not marshal containers:", err) + os.Exit(1) + } + fmt.Fprintln(cmd.OutOrStdout(), string(output)) + }, +} + +func init() { + rootCmd.AddCommand(showCmd) +}