From 25297fc604709a0d82bcde8f253be4c1529bfbc8 Mon Sep 17 00:00:00 2001 From: "Joel D. Elkins" Date: Mon, 25 Jul 2022 10:19:08 -0500 Subject: [PATCH] Add `rm` command --- cmd/rm.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 cmd/rm.go diff --git a/cmd/rm.go b/cmd/rm.go new file mode 100644 index 0000000..72aa37d --- /dev/null +++ b/cmd/rm.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" + + "gitea.elkins.co/Networking/ccl/internal/pkg/config" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// rmCmd represents the rm command +var rmCmd = &cobra.Command{ + Use: "rm", + Aliases: []string{"remove", "delete"}, + Short: "Remove container definitions", + ValidArgsFunction: validNouns, + Args: cobra.OnlyValidArgs, + Long: `Remove defined containers or categories if they have been created. +If running, they will first be stopped.`, + Run: func(cmd *cobra.Command, args []string) { + conts := config.Union(args) + for _, c := range conts { + fmt.Fprintln(output, "REMOVE", c.Name) + for _, cmd := range c.DestroyCommands() { + if err := cmd.Execute(output, fake); err != nil { + log.WithFields(log.Fields{ + "container": c.Name, + "cmd": cmd, + }).Errorln("Remove failed") + } + } + } + }, +} + +func init() { + rootCmd.AddCommand(rmCmd) +}