Skip to content

Commit

Permalink
Merge pull request #23 from roots/add-yaml-comment-headers
Browse files Browse the repository at this point in the history
Add Yaml comment headers to configs
  • Loading branch information
swalkinshaw authored Feb 1, 2019
2 parents 70f505b + 7287858 commit a7cab6e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
33 changes: 24 additions & 9 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ import (
)

type NewCommand struct {
UI cli.Ui
flags *flag.FlagSet
trellis *trellis.Trellis
force bool
vaultPass string
UI cli.Ui
CliVersion string
flags *flag.FlagSet
trellis *trellis.Trellis
force bool
vaultPass string
}

type Release struct {
Version string `json:"tag_name"`
ZipUrl string `json:"zipball_url"`
}

func NewNewCommand(ui cli.Ui, trellis *trellis.Trellis) *NewCommand {
c := &NewCommand{UI: ui, trellis: trellis}
func NewNewCommand(ui cli.Ui, trellis *trellis.Trellis, version string) *NewCommand {
c := &NewCommand{UI: ui, trellis: trellis, CliVersion: version}
c.init()
return c
}
Expand Down Expand Up @@ -120,11 +121,19 @@ func (c *NewCommand) Run(args []string) int {
// Update default configs
for env, config := range c.trellis.Environments {
c.trellis.UpdateDefaultConfig(config, name, host, env)
c.trellis.WriteConfigYaml(config, filepath.Join("group_vars", env, "wordpress_sites.yml"))
c.trellis.WriteYamlFile(
config,
filepath.Join("group_vars", env, "wordpress_sites.yml"),
c.YamlHeader("https://roots.io/trellis/docs/wordpress-sites/"),
)

stringGenerator := trellis.RandomStringGenerator{Length: 64}
vault := c.trellis.GenerateVaultConfig(name, env, &stringGenerator)
c.trellis.WriteVaultYaml(vault, filepath.Join("group_vars", env, "vault.yml"))
c.trellis.WriteYamlFile(
vault,
filepath.Join("group_vars", env, "vault.yml"),
c.YamlHeader("https://roots.io/trellis/docs/vault/"),
)
}

if err := c.trellis.GenerateVaultPassFile(c.vaultPass); err != nil {
Expand Down Expand Up @@ -181,6 +190,12 @@ Options:
return strings.TrimSpace(helpText)
}

func (c *NewCommand) YamlHeader(doc string) string {
const header = "# Created by trellis-cli v%s\n# Documentation: %s\n\n"

return fmt.Sprintf(header, c.CliVersion, doc)
}

func addTrellisFile(path string) error {
path = filepath.Join(path, ".trellis.yml")
return ioutil.WriteFile(path, []byte{}, 0666)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
c := cli.NewCLI("trellis", "0.1.0")
c := cli.NewCLI("trellis", "0.2.1")
c.Args = os.Args[1:]

ui := &cli.ColoredUi{
Expand Down Expand Up @@ -38,7 +38,7 @@ func main() {
return &cmd.InfoCommand{UI: ui, Trellis: trellis}, nil
},
"new": func() (cli.Command, error) {
return cmd.NewNewCommand(ui, trellis), nil
return cmd.NewNewCommand(ui, trellis, c.Version), nil
},
"provision": func() (cli.Command, error) {
return cmd.NewProvisionCommand(ui, trellis), nil
Expand Down
10 changes: 0 additions & 10 deletions trellis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,3 @@ func (t *Trellis) UpdateDefaultConfig(config *Config, name string, host string,
delete(config.WordPressSites, DefaultSiteName)
t.GenerateSite(config.WordPressSites[name], name, host, env)
}

func (t *Trellis) WriteConfigYaml(config *Config, path string) error {
configYaml, err := yaml.Marshal(config)

if err != nil {
log.Fatal(err)
}

return t.WriteYamlFile(path, configYaml)
}
9 changes: 8 additions & 1 deletion trellis/trellis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trellis
import (
"errors"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -109,8 +110,14 @@ func (t *Trellis) UpdateAnsibleConfig(section string, key string, value string)
return nil
}

func (t *Trellis) WriteYamlFile(path string, data []byte) error {
func (t *Trellis) WriteYamlFile(s interface{}, path string, header string) error {
data, err := yaml.Marshal(s)
if err != nil {
log.Fatal(err)
}

path = filepath.Join(t.Path, path)
data = append([]byte(header), data...)

if err := ioutil.WriteFile(path, data, 0666); err != nil {
log.Fatal(err)
Expand Down
11 changes: 0 additions & 11 deletions trellis/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package trellis
import (
"crypto/rand"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -98,16 +97,6 @@ func (t *Trellis) GenerateVaultPassFile(path string) error {
return ioutil.WriteFile(path, []byte(vaultPass), 0600)
}

func (t *Trellis) WriteVaultYaml(vault *Vault, path string) error {
vaultYaml, err := yaml.Marshal(vault)

if err != nil {
log.Fatal(err)
}

return t.WriteYamlFile(path, vaultYaml)
}

func assertAvailablePRNG() {
buf := make([]byte, 1)

Expand Down

0 comments on commit a7cab6e

Please sign in to comment.