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

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  // DaemonConnectionFlags encodes top-level Docker command line flags that
     9  // control the Docker daemon connection. These flags are shared between the
    10  // Docker CLI and Docker Compose. These flags can be loaded from Mutagen URL
    11  // parameters or used as command line flag storage. The zero value of this
    12  // structure is a valid value corresponding to the absence of any of these
    13  // flags.
    14  type DaemonConnectionFlags struct {
    15  	// Config stores the value of the --config flag.
    16  	Config string
    17  	// Host stores the value of the -H/--host flag.
    18  	Host string
    19  	// Context stores the value of the -c/--context flag.
    20  	Context string
    21  	// TLS indicates the presence of the --tls flag.
    22  	TLS bool
    23  	// TLSCACert stores the value of the --tlscacert flag.
    24  	TLSCACert string
    25  	// TLSCert stores the value of the --tlscert flag.
    26  	TLSCert string
    27  	// TLSKey stores the value of the --tlskey flag.
    28  	TLSKey string
    29  	// TLSVerify indicates the presence of the --tlsverify flag.
    30  	TLSVerify bool
    31  }
    32  
    33  // LoadDaemonConnectionFlagsFromURLParameters loads top-level Docker daemon
    34  // connection flags from Mutagen URL parameters.
    35  func LoadDaemonConnectionFlagsFromURLParameters(parameters map[string]string) (*DaemonConnectionFlags, error) {
    36  	// Create a zero-valued result (corresponding to no flags).
    37  	result := &DaemonConnectionFlags{}
    38  
    39  	// Validate and convert parameters.
    40  	for key, value := range parameters {
    41  		switch key {
    42  		case "config":
    43  			if value == "" {
    44  				return nil, errors.New("config parameter has empty value")
    45  			}
    46  			result.Config = value
    47  		case "context":
    48  			if value == "" {
    49  				return nil, errors.New("context parameter has empty value")
    50  			}
    51  			result.Context = value
    52  		case "host":
    53  			if value == "" {
    54  				return nil, errors.New("host parameter has empty value")
    55  			}
    56  			result.Host = value
    57  		case "tls":
    58  			if value != "" {
    59  				return nil, errors.New("tls parameter has non-empty value")
    60  			}
    61  			result.TLS = true
    62  		case "tlscacert":
    63  			if value == "" {
    64  				return nil, errors.New("tlacacert parameter has empty value")
    65  			}
    66  			result.TLSCACert = value
    67  		case "tlscert":
    68  			if value == "" {
    69  				return nil, errors.New("tlscert parameter has empty value")
    70  			}
    71  			result.TLSCert = value
    72  		case "tlskey":
    73  			if value == "" {
    74  				return nil, errors.New("tlskey parameter has empty value")
    75  			}
    76  			result.TLSKey = value
    77  		case "tlsverify":
    78  			if value != "" {
    79  				return nil, errors.New("tlsverify parameter has non-empty value")
    80  			}
    81  			result.TLSVerify = true
    82  		default:
    83  			return nil, fmt.Errorf("unknown parameter: %s", key)
    84  		}
    85  	}
    86  
    87  	// Success.
    88  	return result, nil
    89  }
    90  
    91  // ToFlags reconstitues top-level daemon connection flags so that they can be
    92  // passed to a Docker CLI or Docker Compose command.
    93  func (f *DaemonConnectionFlags) ToFlags() []string {
    94  	// Set up the result.
    95  	var result []string
    96  
    97  	// Add flags as necessary.
    98  	if f.Config != "" {
    99  		result = append(result, "--config", f.Config)
   100  	}
   101  	if f.Host != "" {
   102  		result = append(result, "--host", f.Host)
   103  	}
   104  	if f.Context != "" {
   105  		result = append(result, "--context", f.Context)
   106  	}
   107  	if f.TLS {
   108  		result = append(result, "--tls")
   109  	}
   110  	if f.TLSCACert != "" {
   111  		result = append(result, "--tlscacert", f.TLSCACert)
   112  	}
   113  	if f.TLSCert != "" {
   114  		result = append(result, "--tlscert", f.TLSCert)
   115  	}
   116  	if f.TLSKey != "" {
   117  		result = append(result, "--tlskey", f.TLSKey)
   118  	}
   119  	if f.TLSVerify {
   120  		result = append(result, "--tlsverify")
   121  	}
   122  
   123  	// Done.
   124  	return result
   125  }
   126  
   127  // ToURLParameters converts top-level daemon connection flags to parameters that
   128  // can be embedded in a Mutagen URL. These parameters can be converted back
   129  // using LoadDaemonConnectionFlagsFromURLParameters.
   130  func (f *DaemonConnectionFlags) ToURLParameters() map[string]string {
   131  	// Create an empty set of parameters.
   132  	result := make(map[string]string)
   133  
   134  	// Add parameters as necessary.
   135  	if f.Config != "" {
   136  		result["config"] = f.Config
   137  	}
   138  	if f.Host != "" {
   139  		result["host"] = f.Host
   140  	}
   141  	if f.Context != "" {
   142  		result["context"] = f.Context
   143  	}
   144  	if f.TLS {
   145  		result["tls"] = ""
   146  	}
   147  	if f.TLSCACert != "" {
   148  		result["tlscacert"] = f.TLSCACert
   149  	}
   150  	if f.TLSCert != "" {
   151  		result["tlscert"] = f.TLSCert
   152  	}
   153  	if f.TLSKey != "" {
   154  		result["tlskey"] = f.TLSKey
   155  	}
   156  	if f.TLSVerify {
   157  		result["tlsverify"] = ""
   158  	}
   159  
   160  	// Done.
   161  	return result
   162  }