github.com/bkosm/gompose/v2@v2.3.1/down.go (about)

     1  package gompose
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"strings"
     7  )
     8  
     9  // Down stops and removes containers, networks, images, and volumes specified in compose file.
    10  // It can be configured with a custom compose file path.
    11  // Returns an error if shell command fails.
    12  // Skips the command invocation if the current shell has a CSV environment variable with the key of SkipEnv
    13  // set with a value of SkipDown. This allows for retaining the services between runs without altering source code.
    14  func Down(opts ...Option) error {
    15  	if shouldSkipCommand() {
    16  		return nil
    17  	}
    18  
    19  	var customFile customFile
    20  	for _, opt := range opts {
    21  		if fn := opt.withCustomFileFunc; fn != nil {
    22  			fn(&customFile)
    23  		}
    24  	}
    25  
    26  	var args []string
    27  	if customFile != "" {
    28  		args = []string{"-f", string(customFile)}
    29  	}
    30  	args = append(args, "down")
    31  
    32  	_, err := run(*exec.Command("docker-compose", args...))
    33  	return err
    34  }
    35  
    36  func shouldSkipCommand() bool {
    37  	val, ok := os.LookupEnv(SkipEnv)
    38  	if !ok {
    39  		return false
    40  	}
    41  
    42  	for _, token := range strings.Split(val, ",") {
    43  		if strings.ToLower(token) == SkipDown {
    44  			return true
    45  		}
    46  	}
    47  	return false
    48  }