Script Generator
The application takes a CSV file, processes templates, and outputs text files.
The CSV is a database of domain names, IP addresses, user names, web root paths, git repo details and some flags.
Package https://github.com/gocarina/gocsv is used to parse the CSV.
A Site is a struct to match the fields of the CSV.
ViewData is a slice (aka unindexed array) of Sites used in parsing templates.
type Site struct {
Null bool
Hostname string `csv:"HOSTNAME"`
Domain string `csv:"DOMAIN"`
User string `csv:"USER"`
Port string `csv:"PORT"`
Webroot string `csv:"WEBROOT"`
Repourl string `csv:"REPOURL"`
Host string `csv:"HOST"`
Branch string `csv:"BRANCH"`
Environment string `csv:"ENVIRONMENT"`
Deploy bool `csv:"DEPLOY"`
Update bool `csv:"UPDATE"`
Backups_daily bool `csv:"BACKUPS_DAILY"`
}
type ViewData struct {
Sites []Site
}
The templates are in the https://golang.org/pkg/text/template/ format.
An example of a template for a ssh_config file syntax:
{{range .Sites}}
Host {{.Nick}}
Hostname {{if .Hostname}}{{.Hostname}}{{else}}{{.Domain}}{{end}}
{{if .User}}User {{.User}}{{end}}
{{if .Port}}Port {{.Port}}{{end}}
{{end}}
Build
- [Setup Go] (https://www.digitalocean.com/community/tutorials/how-to-install-go-on-ubuntu-18-04)
- Clone the repo into
$GOPATH/src/gitlab.com/neilscudder/script-generator
- cd into the new dir
- Run
go get
to retrieve dependencies
- 'go build`
[Note: Go can get quirky if you make a symlink as a shortcut into the src path.]
Usage
./script-generator local-data.csv local_templates/
The app takes two paths as arguments, first the CSV_PATH followed by LOCAL_TEMPLATE_PATH.
Both paths are required.
Git will ignore all file and directory names prepended with the string local_
Output is written to ./local-output/
Why?
This application was made in order to maintain configuration and batch files for the management of multiple WordPress websites using:
The CSV format was chosen so that data could be maintained in and exported from Google Sheets.
A local batch script is also made to backup all files and overwrite them with the newly generated versions.
The Future
Managing this data in a spreadsheet is good because:
- universally accessible
- easy to manage access
and bad because:
- no data validation
- mistakes can have serious consequences
- cumbersome data transfer process
My plan is to:
- Configure a lite database with a golang restful API interface (WIP https://gitlab.com/neilscudder/simple-api)
- Import CSV data into the database with validation
- Create a web-based front-end to deprecate the CSV process
I don't know how to achieve step 3 but 1 and 2 are straightforward enough.
This space is left intentionally blank.