Skip to content

Commit

Permalink
add: compatible with Precmd v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Dec 13, 2019
1 parent bd60dc4 commit 36111b2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 46 deletions.
24 changes: 15 additions & 9 deletions cmd/reconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ var reconfCmd = &cobra.Command{
}

// create, start and config
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
}
}
if len(tnconfig.PreInit.Cmds) != 0 {
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
}
}
if len(tnconfig.PostInit.Cmds) != 0 {
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
Expand Down
24 changes: 15 additions & 9 deletions cmd/reup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ var reupCmd = &cobra.Command{
}

// create, start
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
}
}
if len(tnconfig.PreInit.Cmds) != 0 {
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
}
}
if len(tnconfig.PostInit.Cmds) != 0 {
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ func initConfig() {
fmt.Println("echo Using config file:", viper.ConfigFileUsed())
}

fmt.Println(viper.Get("precmd"))
fmt.Println(viper.Get("nodes"))
if err := viper.Unmarshal(&tnconfig); err != nil {
fmt.Println(err)
os.Exit(1)
panic(err)
// fmt.Println(err)
// os.Exit(1)
}
}
24 changes: 15 additions & 9 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ var upCmd = &cobra.Command{
Use: "up",
Short: "Create and start containers",
Run: func(cmd *cobra.Command, args []string) {
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
}
}
if len(tnconfig.PreInit.Cmds) != 0 {
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
}
}
if len(tnconfig.PostInit.Cmds) != 0 {
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
Expand Down
24 changes: 15 additions & 9 deletions cmd/upconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ var upconfCmd = &cobra.Command{
Short: "Create, start and config",
Run: func(cmd *cobra.Command, args []string) {
// up
if len(tnconfig.PreCmd.Cmds) != 0 {
preCmds := shell.ExecCmd(tnconfig.PreCmd.Cmds)
fmt.Println(strings.Join(preCmds, "\n"))
if len(tnconfig.PreCmd) != 0 {
for _, preCmds := range tnconfig.PreCmd {
preExecCmds := shell.ExecCmd(preCmds.Cmds)
fmt.Println(strings.Join(preExecCmds, "\n"))
}
}
if len(tnconfig.PreInit.Cmds) != 0 {
preInitCmds := shell.ExecCmd(tnconfig.PreInit.Cmds)
fmt.Println(strings.Join(preInitCmds, "\n"))
if len(tnconfig.PreInit) != 0 {
for _, preInitCmds := range tnconfig.PreInit {
preExecInitCmds := shell.ExecCmd(preInitCmds.Cmds)
fmt.Println(strings.Join(preExecInitCmds, "\n"))
}
}
if len(tnconfig.PostInit.Cmds) != 0 {
postInitCmds := shell.ExecCmd(tnconfig.PostInit.Cmds)
fmt.Println(strings.Join(postInitCmds, "\n"))
if len(tnconfig.PostInit) != 0 {
for _, postInitCmds := range tnconfig.PostInit {
postExecInitCmds := shell.ExecCmd(postInitCmds.Cmds)
fmt.Println(strings.Join(postExecInitCmds, "\n"))
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
Expand Down
17 changes: 9 additions & 8 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

// Tn tinet config
type Tn struct {
PreCmd PreCmd `yaml:"precmd"`
PreInit PreInit `yaml:"preinit"`
PostInit PostInit `yaml:"postinit"`
PostFini PostFini `yaml:"postfini"`
PreCmd []PreCmd `yaml:"precmd"`
PreInit []PreInit `yaml:"preinit"`
PostInit []PostInit `yaml:"postinit"`
PostFini []PostFini `yaml:"postfini"`
Nodes []Node `yaml:"nodes" mapstructure:"nodes"`
Switches []Switch `yaml:"switches" mapstructure:"switches"`
NodeConfigs []NodeConfig `yaml:"node_configs" mapstructure:"node_configs"`
Expand All @@ -24,6 +24,7 @@ type Tn struct {

// PreCmd
type PreCmd struct {
// Cmds []Cmd `yaml:"cmds"`
Cmds []Cmd `yaml:"cmds" mapstructure:"cmds"`
}

Expand Down Expand Up @@ -250,10 +251,10 @@ func (tnconfig *Tn) GenerateFile(cfgFile string) error {
}

tnconfig = &Tn{
PreCmd: precmd,
PreInit: preinit,
PostInit: postinit,
PostFini: postfini,
PreCmd: []PreCmd{precmd},
PreInit: []PreInit{preinit},
PostInit: []PostInit{postinit},
PostFini: []PostFini{postfini},
Nodes: []Node{nodes},
Switches: []Switch{switches},
NodeConfigs: []NodeConfig{nodeConfig},
Expand Down

0 comments on commit 36111b2

Please sign in to comment.