Add --endpoint flag to remove command

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-06 23:27:30 -04:00
parent ba9f9b8725
commit 042fde2c79

View File

@ -1,6 +1,7 @@
package cmd
import (
"github.com/greenled/portainer-stack-utils/client"
"github.com/greenled/portainer-stack-utils/common"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -16,6 +17,39 @@ var stackRemoveCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
stackName := args[0]
endpointId := viper.GetUint32("stack.remove.endpoint")
var endpointSwarmClusterId string
var stack client.Stack
if endpointId == 0 {
logrus.WithFields(logrus.Fields{
"flag": "--endpoint",
}).Fatal("Provide required flag")
} else {
var selectionErr, stackRetrievalErr error
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId)
switch selectionErr.(type) {
case nil:
// It's a swarm cluster
logrus.WithFields(logrus.Fields{
"stack": stackName,
"endpoint": endpointId,
"swarm": endpointSwarmClusterId,
}).Debug("Getting stack")
stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, endpointId)
common.CheckError(stackRetrievalErr)
case *common.StackClusterNotFoundError:
// It's not a swarm cluster
logrus.WithFields(logrus.Fields{
"stack": stackName,
"endpoint": endpointId,
}).Debug("Getting stack")
stack, stackRetrievalErr = common.GetStackByName(stackName, "", endpointId)
common.CheckError(stackRetrievalErr)
default:
// Something else happened
common.CheckError(selectionErr)
}
}
logrus.WithFields(logrus.Fields{
"stack": stackName,
}).Debug("Getting stack")
@ -55,5 +89,7 @@ func init() {
stackCmd.AddCommand(stackRemoveCmd)
stackRemoveCmd.Flags().Bool("strict", false, "Fail if stack does not exist.")
stackRemoveCmd.Flags().Uint32("endpoint", 0, "Endpoint ID.")
viper.BindPFlag("stack.remove.strict", stackRemoveCmd.Flags().Lookup("strict"))
viper.BindPFlag("stack.remove.endpoint", stackRemoveCmd.Flags().Lookup("endpoint"))
}