Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: template function #53

Merged
merged 3 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions command_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func CmdUp(c *cli.Context) error {
if node.Type == "docker" || node.Type == "" {
delNsCmd := node.DelNsCmd()
utils.PrintCmd(os.Stdout, delNsCmd, verbose)
mountTmplCmd, err := node.MountTmpl()
utils.PrintCmds(os.Stdout, mountTmplCmd, verbose)
if err != nil {
return err
}
}
}

Expand Down
75 changes: 61 additions & 14 deletions internal/pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package shell

import (
"fmt"
"os"
"strings"
"text/template"

l "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -48,20 +50,22 @@ type PostFini struct {

// Node
type Node struct {
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
BuildFile string `yaml:"buildfile" mapstructure:"buildfile"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
DNS []string `yaml:"dns,flow" mapstructure:"dns,flow"`
DNSSearches []string `yaml:"dns_search,flow" mapstructure:"dns_search,flow"`
HostNameIgnore bool `yaml:"hostname_ignore" mapstructure:"hostname_ignore"`
EntryPoint string `yaml:"entrypoint" mapstructure:"entrypoint"`
ExtraArgs string `yaml:"docker_run_extra_args" mapstructure:"docker_run_extra_args"`
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
BuildFile string `yaml:"buildfile" mapstructure:"buildfile"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
DNS []string `yaml:"dns,flow" mapstructure:"dns,flow"`
DNSSearches []string `yaml:"dns_search,flow" mapstructure:"dns_search,flow"`
HostNameIgnore bool `yaml:"hostname_ignore" mapstructure:"hostname_ignore"`
EntryPoint string `yaml:"entrypoint" mapstructure:"entrypoint"`
ExtraArgs string `yaml:"docker_run_extra_args" mapstructure:"docker_run_extra_args"`
Vars map[string]interface{} `yaml:"vars" mapstructure:"vars"`
slankdev marked this conversation as resolved.
Show resolved Hide resolved
Templates []Template `yaml:"templates" mapstructure:"templates"`
}

// Interface
Expand All @@ -77,6 +81,12 @@ type Sysctl struct {
Sysctl string `yaml:"string"`
}

type Template struct {
Src string `yaml:"src"`
Dst string `yaml:"dst"`
Content string `yaml:"content"`
}
Comment on lines +84 to +88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これもだだの今後の議論ポイントとして.

もしかすると, 以下のようでもよかったかもしれないね.
両方(content, src) が両方選択されると, 片方は無視されるだけだけど, 指定されたかどうかとかは, mapとしてはpointer使ってその辺を整理出来るようにする方法もあると思う.

今は十分だと思うからこれでいいけど. ポイントとして.

type Template struct {
  Src *string
  Dst *string
  Content *string
}


// Switch
type Switch struct {
Name string `yaml:"name"`
Expand Down Expand Up @@ -599,3 +609,40 @@ func (node *Node) DelNsCmd() (delNsCmd string) {

return delNsCmd
}

// MountTmpl
func (node *Node) MountTmpl() (mountTmplCmd []string, err error) {
for _, tmpl := range node.Templates {
dest := strings.Split(tmpl.Dst, "/")
destfile := dest[len(dest)-1]
f, err := os.Create(destfile)
slankdev marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
defer f.Close()

if len(tmpl.Src) > 0 {
tpl := template.Must(template.ParseFiles(tmpl.Src))
slankdev marked this conversation as resolved.
Show resolved Hide resolved
// err := tpl.Execute(os.Stdout, node.Vars)
err := tpl.Execute(f, node.Vars)
if err != nil {
return nil, err
}
} else if len(tmpl.Content) > 0 {
tpl, err := template.New("").Parse(tmpl.Content)
if err != nil {
return nil, err
}
err = tpl.Execute(f, node.Vars)
if err != nil {
return nil, err
}
}
tmplCmd := fmt.Sprintf("docker cp %s %s:%s", destfile, node.Name, tmpl.Dst)
mountTmplCmd = append(mountTmplCmd, tmplCmd)
removeFileCmd := fmt.Sprintf("rm -rf %s", destfile)
mountTmplCmd = append(mountTmplCmd, removeFileCmd)
}

return mountTmplCmd, nil
}