Skip to content

Commit

Permalink
change struct implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1ra24 committed Jan 28, 2020
1 parent 5f98153 commit 9f3b895
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 397 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/go.yaml → .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: golangci
name: test

on: [push, pull_request]
on:
push:
branches:
- "**"
pull_request: {}

jobs:
build:
Expand All @@ -9,7 +13,7 @@ jobs:
strategy:
matrix:
go:
- 1.12.9
- 1.13
steps:
- name: checkout
uses: actions/checkout@v1
Expand Down
3 changes: 1 addition & 2 deletions cmd/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package cmd
import (
"fmt"

"github.com/ak1ra24/tn/internal/pkg/shell"
"github.com/spf13/cobra"
)

Expand All @@ -19,7 +18,7 @@ var confCmd = &cobra.Command{
}

for _, nodeConfig := range tnconfig.NodeConfigs {
execConfCmds := shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
execConfCmds := nodeConfig.ExecConf(nodeinfo[nodeConfig.Name])
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/ak1ra24/tn/internal/pkg/shell"
"github.com/spf13/cobra"
)

Expand All @@ -15,11 +14,11 @@ var downCmd = &cobra.Command{
Short: "Stop and remove containers",
Run: func(cmd *cobra.Command, args []string) {
for _, node := range tnconfig.Nodes {
deleteNode := shell.DeleteNode(node)
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
}
for _, br := range tnconfig.Switches {
delBrCmd := shell.DeleteSwitch(br)
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
}
},
Expand Down
20 changes: 10 additions & 10 deletions cmd/reconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ var reconfCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := shell.DeleteNode(node)
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
}
for _, br := range tnconfig.Switches {
delBrCmd := shell.DeleteSwitch(br)
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
}

Expand All @@ -39,35 +39,35 @@ var reconfCmd = &cobra.Command{
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
createSwitchCmds := shell.CreateSwitch(bridge)
createSwitchCmds := bridge.CreateSwitch()
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := shell.N2nLink(node.Name, inf)
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
s2nLinkCmd := shell.S2nLink(node.Name, inf)
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
v2cLinkCmds := shell.V2cLink(node.Name, inf)
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
p2cLinkCmds := shell.P2cLink(node.Name, inf)
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
Expand All @@ -89,7 +89,7 @@ var reconfCmd = &cobra.Command{
}

for _, nodeConfig := range tnconfig.NodeConfigs {
execConfCmds := shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
execConfCmds := nodeConfig.ExecConf(nodeinfo[nodeConfig.Name])
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/reup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ var reupCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// stop, remove
for _, node := range tnconfig.Nodes {
deleteNode := shell.DeleteNode(node)
deleteNode := node.DeleteNode()
fmt.Println(strings.Join(deleteNode, "\n"))
}
for _, br := range tnconfig.Switches {
delBrCmd := shell.DeleteSwitch(br)
delBrCmd := br.DeleteSwitch()
fmt.Println(delBrCmd)
}

Expand All @@ -39,35 +39,35 @@ var reupCmd = &cobra.Command{
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
createSwitchCmds := shell.CreateSwitch(bridge)
createSwitchCmds := bridge.CreateSwitch()
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := shell.N2nLink(node.Name, inf)
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
s2nLinkCmd := shell.S2nLink(node.Name, inf)
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
v2cLinkCmds := shell.V2cLink(node.Name, inf)
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
p2cLinkCmds := shell.P2cLink(node.Name, inf)
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
Expand Down
14 changes: 7 additions & 7 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ var upCmd = &cobra.Command{
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
createSwitchCmds := shell.CreateSwitch(bridge)
createSwitchCmds := bridge.CreateSwitch()
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := shell.N2nLink(node.Name, inf)
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
s2nLinkCmd := shell.S2nLink(node.Name, inf)
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
v2cLinkCmds := shell.V2cLink(node.Name, inf)
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
p2cLinkCmds := shell.P2cLink(node.Name, inf)
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
Expand Down
17 changes: 9 additions & 8 deletions cmd/upconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ var upconfCmd = &cobra.Command{
}
}
for _, node := range tnconfig.Nodes {
createNodeCmds := shell.CreateNode(node)
createNodeCmds := node.CreateNode()
fmt.Println(strings.Join(createNodeCmds, "\n"))

if node.Type != "netns" {
mountDockerNetnsCmds := shell.Mount_docker_netns(node)
mountDockerNetnsCmds := node.Mount_docker_netns()
fmt.Println(strings.Join(mountDockerNetnsCmds, "\n"))
}
}

if len(tnconfig.Switches) != 0 {
for _, bridge := range tnconfig.Switches {
createSwitchCmds := shell.CreateSwitch(bridge)
createSwitchCmds := bridge.CreateSwitch()
fmt.Println(strings.Join(createSwitchCmds, "\n"))
}
}

for _, node := range tnconfig.Nodes {
for _, inf := range node.Interfaces {
if inf.Type == "direct" {
n2nLinkCmds := shell.N2nLink(node.Name, inf)
n2nLinkCmds := inf.N2nLink(node.Name)
fmt.Println(strings.Join(n2nLinkCmds, "\n"))
} else if inf.Type == "bridge" {
s2nLinkCmd := shell.S2nLink(node.Name, inf)
s2nLinkCmd := inf.S2nLink(node.Name)
fmt.Println(strings.Join(s2nLinkCmd, "\n"))
} else if inf.Type == "veth" {
v2cLinkCmds := shell.V2cLink(node.Name, inf)
v2cLinkCmds := inf.V2cLink(node.Name)
fmt.Println(strings.Join(v2cLinkCmds, "\n"))
} else if inf.Type == "phys" {
p2cLinkCmds := shell.P2cLink(node.Name, inf)
p2cLinkCmds := inf.P2cLink(node.Name)
fmt.Println(strings.Join(p2cLinkCmds, "\n"))
} else {
err := fmt.Errorf("not supported interface type: %s", inf.Type)
Expand All @@ -72,14 +72,15 @@ var upconfCmd = &cobra.Command{
fmt.Println(strings.Join(postExecInitCmds, "\n"))
}
}

// conf
nodeinfo := map[string]string{}
for _, node := range tnconfig.Nodes {
nodeinfo[node.Name] = node.Type
}

for _, nodeConfig := range tnconfig.NodeConfigs {
execConfCmds := shell.ExecConf(nodeinfo[nodeConfig.Name], nodeConfig)
execConfCmds := nodeConfig.ExecConf(nodeinfo[nodeConfig.Name])
for _, execConfCmd := range execConfCmds {
fmt.Println(execConfCmd)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/spf13/cobra"
)

const version = "0.0.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "show the tinet version",
Run: func(cmd *cobra.Command, args []string) {
version := "v0.0.0"
fmt.Printf("Tinet version %s\n", version)
fmt.Printf("Tinet version v%s\n", version)
},
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module github.com/ak1ra24/tn
go 1.12

require (
github.com/Songmu/goxz v0.6.0 // indirect
github.com/emicklei/dot v0.10.1
github.com/go-playground/locales v0.13.0
github.com/heetch/confita v0.9.0
github.com/k0kubun/pp v3.0.1+incompatible
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
github.com/mitchellh/go-homedir v1.1.0
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1
Expand Down
Loading

0 comments on commit 9f3b895

Please sign in to comment.