github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/docker/docker.go (about)

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  
     9  	"github.com/mutagen-io/mutagen/pkg/platform"
    10  )
    11  
    12  // CommandPath returns the absolute path specification to use for invoking
    13  // Docker. It will use the MUTAGEN_DOCKER_PATH environment variable if provided,
    14  // otherwise falling back to a platform-specific implementation.
    15  func CommandPath() (string, error) {
    16  	// If MUTAGEN_DOCKER_PATH is specified, then use it to perform the lookup.
    17  	if searchPath := os.Getenv("MUTAGEN_DOCKER_PATH"); searchPath != "" {
    18  		return platform.FindCommand("docker", []string{searchPath})
    19  	}
    20  
    21  	// Otherwise fall back to the platform-specific implementation.
    22  	return commandPathForPlatform()
    23  }
    24  
    25  // Command prepares (but does not start) a Docker command with the specified
    26  // arguments and scoped to lifetime of the provided context.
    27  func Command(ctx context.Context, args ...string) (*exec.Cmd, error) {
    28  	// Identify the command path.
    29  	commandPath, err := CommandPath()
    30  	if err != nil {
    31  		return nil, fmt.Errorf("unable to identify 'docker' command: %w", err)
    32  	}
    33  
    34  	// Create the command.
    35  	return exec.CommandContext(ctx, commandPath, args...), nil
    36  }