Skip to content

Commit

Permalink
add function: img cmd by graphviz
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Dec 23, 2019
1 parent 69103f4 commit da4e2f5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
68 changes: 68 additions & 0 deletions cmd/img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Package cmd
package cmd

import (
"fmt"
"strings"

"github.com/emicklei/dot"
"github.com/spf13/cobra"
)

// imgCmd represents the img command
var imgCmd = &cobra.Command{
Use: "img",
Short: "Generate topology png file by graphviz",
Run: func(cmd *cobra.Command, args []string) {
g := dot.NewGraph(dot.Directed)
for _, tnnode := range tnconfig.Nodes {
nodeName := tnnode.Name
fromNode := g.Node(nodeName)
fromNode.Label(nodeName)
ifaceInfos := tnnode.Interfaces
for _, ifaceInfo := range ifaceInfos {
var argsName string
if ifaceInfo.Type == "direct" {
argsName = strings.Split(ifaceInfo.Args, "#")[0]
toNode := g.Node(argsName)
findEdges := g.FindEdges(toNode, fromNode)
if len(findEdges) == 0 {
newEdge := g.Edge(fromNode, toNode)
newEdge.Attr("arrowhead", "none")
newEdge.Attr("labelfloat", "true")
newEdge.Attr("headlabel", strings.Split(ifaceInfo.Args, "#")[1])
newEdge.Attr("taillabel", ifaceInfo.Name)
newEdge.Attr("fontsize", "8")
}
} else if ifaceInfo.Type == "bridge" {
argsName = ifaceInfo.Args
toNode := g.Node(argsName)
findEdges := g.FindEdges(toNode, fromNode)
if len(findEdges) == 0 {
newEdge := g.Edge(fromNode, toNode)
newEdge.Attr("arrowhead", "none")
newEdge.Attr("labelfloat", "true")
newEdge.Attr("headlabel", argsName)
newEdge.Attr("taillabel", ifaceInfo.Name)
newEdge.Attr("fontsize", "8")
}
}
}
}
fmt.Println(g.String())
},
}

func init() {
rootCmd.AddCommand(imgCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// imgCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// imgCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
downCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
execCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
initCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
imgCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
printCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
psCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
pullCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./spec.yaml)")
Expand Down Expand Up @@ -99,8 +100,6 @@ func initConfig() {
if err := viper.ReadInConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
} else {
fmt.Println("echo Using config file:", viper.ConfigFileUsed())
}

if err := viper.Unmarshal(&tnconfig); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,24 @@ func Pull(nodes []Node) []string {

// TnTestCmdExec Execute test cmds
func TnTestCmdExec(tests []Test) []string {
var startMessage, doneMessage string
// var startMessage, doneMessage string
var tnTestCmds []string
for _, test := range tests {
if test.Name != "" {
startMessage = fmt.Sprintf("echo %s Test Start", test.Name)
doneMessage = fmt.Sprintf("echo %s Test Done", test.Name)
} else {
startMessage = "echo Test Start"
doneMessage = "echo Test Done"
}
// if test.Name != "" {
// startMessage = fmt.Sprintf("echo %s Test Start", test.Name)
// doneMessage = fmt.Sprintf("echo %s Test Done", test.Name)
// } else {
// startMessage = "echo Test Start"
// doneMessage = "echo Test Done"
// }

tnTestCmds = append(tnTestCmds, startMessage)
// tnTestCmds = append(tnTestCmds, startMessage)

for _, testCmd := range test.Cmds {
tnTestCmds = append(tnTestCmds, testCmd.Cmd)
}

tnTestCmds = append(tnTestCmds, doneMessage)
// tnTestCmds = append(tnTestCmds, doneMessage)
}

return tnTestCmds
Expand Down
4 changes: 2 additions & 2 deletions shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func TestTnTestCmdExec(t *testing.T) {
},
},
},
want: []string{"echo Test Start", "echo slankdev", "echo Test Done"},
want: []string{"echo slankdev"},
},
{
name: "test name set",
Expand All @@ -395,7 +395,7 @@ func TestTnTestCmdExec(t *testing.T) {
},
},
},
want: []string{"echo p2p Test Start", "docker exec R1 echo hello", "docker exec R2 echo world", "echo p2p Test Done"},
want: []string{"docker exec R1 echo hello", "docker exec R2 echo world"},
},
}
for _, tt := range tests {
Expand Down

0 comments on commit da4e2f5

Please sign in to comment.