This is a high level question, as I'm not sure how to approach this problem:
I'm building a CLI that sets up code projects for users automatically.
I want to create an update
feature that will allow the user to update to the latest CLI's template versions but still keep the original variables they used to create the project (i.e. project name, env variables, etc...)
I know how to create new templates, but I don't know how I could update and still keep the variables passed by the user.
It would be something similar to a git merge, so merge the user's local project with the updated CLI template.
In the code below you can see an example of how the files are templated with var deploymentYamlData
as string variables
I have a function that organizes all these file templates into the proper directory structure for the project.
How would I go about merging directory templates with the user's local directory, while being able to persist the original template variables?
Apologies ahead of time if I need to clarify more.
package main
import (
"text/template"
"os"
)
var deploymentYamlData = `---
# do not edit commented lines
# CLI-Version: {{ .GitTagVersion}}
app: {{ .BotName }}
type: web
team: {{ .TeamName }}
docker_image: {{ .DockerImageName }}
docker_tag: {{ .TagVersion }}
internal: false
replicas: {{ .Replicas }}
revisionHistoryLimit: 5
container_port: {{ .DockerImagePort }}
healthcheck:
path: /{{ .DockerImageHealthCheck }}
dynamodb:
enable_deleteitem: true
tables:
- name: {{ .BotName }}_conversation_data
`
type DigitalAssistant struct {
BotName string
TeamName string
DockerImageName string
TagVersion string
Replicas int
DockerImagePort int
DockerImageHealthCheck string
GitTageVersion string
}
func main() {
bot := DigitalAssistant{"bobisyouruncle", "teamAwesome", "awesomebotimagename", "0.1.0", 1, 8000, "health"}
bmap, err := template.New("captain.tmpl").Parse(deploymentYamlData)
if err != nil { panic(err) }
err = bmap.Execute(os.Stdout, bot)
if err != nil { panic(err) }
}
There are several ways to solve this, but this roundtrip will be easier if you generate and read YAML using a module like https://github.com/go-yaml/yaml rather than using templates.
Also, this tool will make it easy to make the struct type by example: https://mengzhuo.github.io/yaml-to-go/
A different parsing approach is done in https://github.com/spf13/viper which might be more useful if you allow fields you don´t know.