psu/cmd/stackRemove.go

60 lines
1.5 KiB
Go
Raw Normal View History

2019-07-21 02:00:04 +00:00
package cmd
import (
"github.com/greenled/portainer-stack-utils/common"
"github.com/sirupsen/logrus"
2019-07-21 02:00:04 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2019-07-21 02:00:04 +00:00
)
// stackRemoveCmd represents the remove command
var stackRemoveCmd = &cobra.Command{
Use: "remove STACK_NAME",
Short: "Remove a stack",
Aliases: []string{"rm", "down"},
Example: "psu stack rm mystack",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
stackName := args[0]
logrus.WithFields(logrus.Fields{
"stack": stackName,
}).Debug("Getting stack")
stack, err := common.GetStackByName(stackName, "", 0)
2019-07-21 02:00:04 +00:00
switch err.(type) {
case nil:
// The stack exists
stackId := stack.Id
client, err := common.GetClient()
common.CheckError(err)
2019-07-21 02:00:04 +00:00
logrus.WithFields(logrus.Fields{
"stack": stackName,
}).Info("Removing stack")
err = client.DeleteStack(stackId)
common.CheckError(err)
2019-07-21 02:00:04 +00:00
case *common.StackNotFoundError:
// The stack does not exist
logrus.WithFields(logrus.Fields{
"stack": stackName,
}).Debug("Stack not found")
2019-07-21 02:00:04 +00:00
if viper.GetBool("stack.remove.strict") {
logrus.WithFields(logrus.Fields{
"stack": stackName,
}).Fatal("Stack does not exist")
2019-07-21 02:00:04 +00:00
}
default:
// Something else happened
common.CheckError(err)
2019-07-21 02:00:04 +00:00
}
},
}
func init() {
stackCmd.AddCommand(stackRemoveCmd)
2019-08-06 22:51:56 +00:00
stackRemoveCmd.Flags().Bool("strict", false, "Fail if stack does not exist.")
2019-07-21 02:00:04 +00:00
viper.BindPFlag("stack.remove.strict", stackRemoveCmd.Flags().Lookup("strict"))
}