|
| 1 | +package trellis |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + suffix "golang.org/x/net/publicsuffix" |
| 6 | + "gopkg.in/yaml.v2" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const DefaultSiteName = "example.com" |
| 13 | + |
| 14 | +type Site struct { |
| 15 | + SiteHosts []SiteHost `yaml:"site_hosts"` |
| 16 | + LocalPath string `yaml:"local_path"` |
| 17 | + AdminEmail string `yaml:"admin_email,omitempty"` |
| 18 | + Branch string `yaml:"branch,omitempty"` |
| 19 | + Repo string `yaml:"repo,omitempty"` |
| 20 | + RepoSubtreePath string `yaml:"repo_subtree_path,omitempty"` |
| 21 | + Multisite map[string]interface{} `yaml:"multisite"` |
| 22 | + Ssl map[string]interface{} `yaml:"ssl"` |
| 23 | + Cache map[string]interface{} `yaml:"cache"` |
| 24 | +} |
| 25 | + |
| 26 | +type SiteHost struct { |
| 27 | + Canonical string `yaml:"canonical"` |
| 28 | + Redirects []string `yaml:"redirects"` |
| 29 | +} |
| 30 | + |
| 31 | +type Config struct { |
| 32 | + WordPressSites map[string]*Site `yaml:"wordpress_sites"` |
| 33 | +} |
| 34 | + |
| 35 | +func (t *Trellis) ParseConfig(path string) *Config { |
| 36 | + configYaml, err := ioutil.ReadFile(path) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + log.Fatalln(err) |
| 40 | + } |
| 41 | + |
| 42 | + config := &Config{} |
| 43 | + |
| 44 | + if err = yaml.Unmarshal(configYaml, &config); err != nil { |
| 45 | + log.Fatalln(err) |
| 46 | + } |
| 47 | + |
| 48 | + return config |
| 49 | +} |
| 50 | + |
| 51 | +func (t *Trellis) GenerateSite(site *Site, name string, host string, env string) { |
| 52 | + var redirect string |
| 53 | + |
| 54 | + if env == "development" { |
| 55 | + tld, _ := suffix.PublicSuffix(host) |
| 56 | + host = strings.Replace(host, tld, "test", 1) |
| 57 | + |
| 58 | + site.AdminEmail = fmt.Sprintf("admin@%s", host) |
| 59 | + site.Branch = "" |
| 60 | + site.Repo = "" |
| 61 | + site.RepoSubtreePath = "" |
| 62 | + } else { |
| 63 | + site.AdminEmail = "" |
| 64 | + } |
| 65 | + |
| 66 | + if host[:4] == "www." { |
| 67 | + redirect = strings.Replace(host, "www.", "", 1) |
| 68 | + } else { |
| 69 | + redirect = fmt.Sprintf("www.%s", host) |
| 70 | + } |
| 71 | + |
| 72 | + siteHost := SiteHost{ |
| 73 | + Canonical: host, |
| 74 | + Redirects: []string{redirect}, |
| 75 | + } |
| 76 | + |
| 77 | + site.SiteHosts = []SiteHost{siteHost} |
| 78 | +} |
| 79 | + |
| 80 | +func (t *Trellis) UpdateDefaultConfig(config *Config, name string, host string, env string) { |
| 81 | + config.WordPressSites[name] = config.WordPressSites[DefaultSiteName] |
| 82 | + delete(config.WordPressSites, DefaultSiteName) |
| 83 | + t.GenerateSite(config.WordPressSites[name], name, host, env) |
| 84 | +} |
| 85 | + |
| 86 | +func (t *Trellis) WriteConfigYaml(config *Config, path string) error { |
| 87 | + configYaml, err := yaml.Marshal(config) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + log.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + return t.WriteYamlFile(path, configYaml) |
| 94 | +} |
0 commit comments