github.com/searchspring/haus@v0.1.8-0.20200414161854-a7ca8bb9ea93/app/docker.go (about)

     1  package haus
     2  
     3  import(
     4  	"io/ioutil"
     5  	"gopkg.in/yaml.v2"
     6  )
     7  // DockerCfg represents a single docker-compose config.
     8  type DockerCfg struct {
     9  	Build string `yaml:"build,omitempty"`
    10  	Cap_add []string `yaml:"cap_add,omitempty"`
    11  	Cap_drop []string `yaml:"cap_drop,omitempty"`
    12  	Command string `yaml:"command,omitempty"`
    13  	Cpu_shares string `yaml:"cpu_shares,omitempty"`
    14  	Dns []string `yaml:"dns,omitempty"`
    15  	Dns_search []string `yaml:"dns_search,omitempty"`
    16  	Domainname string `yaml:"domainname,omitempty"`
    17  	Entrypoint []string `yaml:"entrypoint,omitempty"`
    18  	Env_file []string `yaml:"env_file,omitempty"`
    19  	Environment []string `yaml:"environment,omitempty"`
    20  	Expose []string `yaml:"expose,omitempty"`
    21  	External_links []string `yaml:"external_links,omitempty"`
    22  	Hostname string `yaml:"hostname,omitempty"`
    23  	Image string `yaml:"image,omitempty"`
    24  	Links []string `yaml:"links,omitempty"`
    25  	Mem_limit string `yaml:"mem_limit,omitempty"`
    26  	Net string `yaml:"net,omitempty"`
    27  	Ports []string `yaml:"ports,omitempty"`
    28  	Privileged string `yaml:"privileged,omitempty"`
    29  	Restart string `yaml:"restart,omitempty"`
    30  	Stdin_open string `yaml:"stdin_open,omitempty"`
    31  	Tty string `yaml:"tty,omitempty"`
    32  	User string `yaml:"user,omitempty"`
    33  	Volumes []string `yaml:"volumes,omitempty"`
    34  	Volumes_from []string `yaml:"volumes_from,omitempty"`
    35  	Working_dir string `yaml:"working_dir,omitempty"`
    36  }
    37  
    38  // DockerYml represents a collection of docker-compose configs to be turned into YAML.
    39  type DockerYml struct {
    40  	Cfg map[string]DockerCfg
    41  }
    42  
    43  
    44  // AddCfg adds a map[string]DockerCfg to DockerYml.
    45  func (y *DockerYml) AddCfg(d map[string]DockerCfg) {
    46  	for k,v := range d {
    47  		if y.Cfg == nil {
    48  			y.Cfg = d 
    49  		} else {
    50  			y.Cfg[k] = v
    51  		}
    52  	}
    53  }
    54  
    55  // Cfgs returns map[string]DockerCfg from DockerYml.
    56  func (y *DockerYml) Cfgs() map[string]DockerCfg {
    57  	return y.Cfg
    58  }
    59  
    60  // WriteYml writes out Cfgs to yaml file.  Hand it full path
    61  // to yaml file you wish to write out.  Returns string of yaml text.  
    62  func (y *DockerYml) WriteYml(filename string) (string,error) {
    63  	yaml,err := yaml.Marshal(y.Cfg)
    64  	if err != nil {
    65  		return "",err
    66  	}
    67  	err = ioutil.WriteFile(filename, yaml, 0644)
    68  	if err != nil {
    69  		return "",err
    70  	}
    71  	return string(yaml[:]),err	
    72  }