Skip to content

Commit

Permalink
Merge pull request #6 from buty4649/improve-message
Browse files Browse the repository at this point in the history
Improve messages
  • Loading branch information
buty4649 authored Mar 20, 2024
2 parents 18c72b7 + 236ce01 commit 35acf01
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
28 changes: 17 additions & 11 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var applyCmd = &cobra.Command{
if ip.NetnsExists(netns) {
slog.Warn("netns is already exists", "name", netns)
} else {
slog.Info("create netns", "name", netns)
err := ip.AddNetns(netns)
if err != nil {
return err
Expand Down Expand Up @@ -69,24 +70,31 @@ var applyCmd = &cobra.Command{
}

func SetupDevice(name string, addresses []string) error {
err := SetLinkUp(name)
if err != nil {
return err
}

slog.Info("add addresses", "name", name, "addresses", addresses)

for _, address := range addresses {
err := ip.AddAddress(name, address)
if err != nil {
return err
}
}
return nil
}

err := ip.SetLinkUp(name)
if err != nil {
return err
}
func SetLinkUp(name string) error {
slog.Info("link up", "name", name, "netns", ip.Netns())

return nil
return ip.SetLinkUp(name)
}

func SetupLoopback(netns string) error {
return ip.IntoNetns(netns, func() error {
return ip.SetLinkUp("lo")
return SetLinkUp("lo")
})
}

Expand All @@ -107,6 +115,7 @@ func SetupEthernets(netns string, ethernets map[string]config.EthernetConfig) er
func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) error {
for name, values := range devices {
ip.IntoNetns(netns, func() error {
slog.Info("add dummy device", "name", name, "netns", netns)
err := ip.AddDummyDevice(name)
if err != nil {
return err
Expand All @@ -123,6 +132,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig)
peerName := values.Peer.Name
peerNetns := values.Peer.Netns

slog.Info("add veth device", "name", name, "netns", netns, "peer name", peerName, "peer netns", peerNetns)
err := ip.AddVethDevice(name, peerName)
if err != nil {
return err
Expand All @@ -142,11 +152,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig)
return err
}
ip.IntoNetns(netns, func() error {
err = SetupDevice(peerName, values.Peer.Addresses)
if err != nil {
return err
}
return nil
return SetupDevice(peerName, values.Peer.Addresses)
})
} else {
SetupDevice(peerName, values.Peer.Addresses)
Expand Down
6 changes: 3 additions & 3 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (

// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
RunE: func(cmd *cobra.Command, args []string) error {
for n := range cfg.Netns {
if ip.NetnsExists(n) {
Expand Down
36 changes: 25 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ import (
"gitlab.com/greyxor/slogor"
)

var cfgFilePath string
var ipCmdPath string
var debug bool
type Flags struct {
ConfigPath string
IpCmdPath string
Debug, Quiet bool
}

var flags Flags

var cfg *config.Config
var ip *iproute2.Iproute2
Expand All @@ -47,23 +51,30 @@ var rootCmd = &cobra.Command{
Long: "Easily automate Linux netns networks and configurations via YAML",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
logLevel := slog.LevelInfo
if debug {
err := cmd.ValidateFlagGroups()
if err != nil {
return err
}

var logLevel slog.Level
if flags.Debug {
logLevel = slog.LevelDebug
}
if flags.Quiet {
logLevel = slog.LevelError
}
slog.SetDefault(slog.New(slogor.NewHandler(os.Stdout, slogor.Options{
TimeFormat: "",
Level: logLevel,
ShowSource: false,
})))

var err error
cfg, err = config.LoadConfig(cfgFilePath)
cfg, err = config.LoadConfig(flags.ConfigPath)
if err != nil {
return err
}

ip = iproute2.New(ipCmdPath)
ip = iproute2.New(flags.IpCmdPath)
return nil
},
}
Expand All @@ -82,7 +93,10 @@ func Execute() {
}

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFilePath, "config", "./netnsplan.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&ipCmdPath, "cmd", "/bin/ip", "ip command path")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().StringVar(&flags.ConfigPath, "config", "./netnsplan.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&flags.IpCmdPath, "cmd", "/bin/ip", "ip command path")

rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().BoolVarP(&flags.Quiet, "quiet", "q", false, "debug mode")
rootCmd.MarkFlagsMutuallyExclusive("debug", "quiet")
}
7 changes: 7 additions & 0 deletions iproute2/iproute2.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func (i *Iproute2) IntoNetns(netns string, fn func() error) error {
return err
}

func (i *Iproute2) Netns() string {
if i.useNetns {
return i.netns
}
return ""
}

func (i *Iproute2) NetnsExists(name string) bool {
return slices.Contains(i.ListNetns(), name)
}
Expand Down

0 comments on commit 35acf01

Please sign in to comment.