refactor: do cli.Exit in top level
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
e5e4641264
commit
4c6e251247
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -158,7 +159,7 @@ func createCliApp() *cli.App {
|
|||||||
Action: func(ctx *cli.Context, s string) error {
|
Action: func(ctx *cli.Context, s string) error {
|
||||||
_, err := netip.ParseAddr(s)
|
_, err := netip.ParseAddr(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Exit(fmt.Sprintf("Can not parse %s as a network IP", s), 1)
|
return errors.New(fmt.Sprintf("Can not parse %s as a network IP", s))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -177,7 +178,7 @@ func createCliApp() *cli.App {
|
|||||||
app.Action = func(ctx *cli.Context) error {
|
app.Action = func(ctx *cli.Context) error {
|
||||||
err := setup(ctx)
|
err := setup(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return cli.Exit(err, 1)
|
||||||
}
|
}
|
||||||
run(ctx)
|
run(ctx)
|
||||||
return nil
|
return nil
|
||||||
@ -206,7 +207,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
slog.Info("Creating a private key")
|
slog.Info("Creating a private key")
|
||||||
privateKey, err = wgtypes.GeneratePrivateKey()
|
privateKey, err = wgtypes.GeneratePrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Exit(err, 1)
|
return err
|
||||||
}
|
}
|
||||||
slog.Debug(fmt.Sprintf("new public key: %s", privateKey.PublicKey().String()))
|
slog.Debug(fmt.Sprintf("new public key: %s", privateKey.PublicKey().String()))
|
||||||
return nil
|
return nil
|
||||||
@ -231,7 +232,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
privateKeyStr := make([]byte, 45)
|
privateKeyStr := make([]byte, 45)
|
||||||
n, err := privKeyFile.Read(privateKeyStr)
|
n, err := privKeyFile.Read(privateKeyStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Exit(err, 1)
|
return err
|
||||||
}
|
}
|
||||||
if n != 45 {
|
if n != 45 {
|
||||||
slog.Warn("Private key length did not math the expected 45!")
|
slog.Warn("Private key length did not math the expected 45!")
|
||||||
@ -241,7 +242,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
privateKey, err = wgtypes.ParseKey(string(privateKeyStr))
|
privateKey, err = wgtypes.ParseKey(string(privateKeyStr))
|
||||||
slog.Debug("Keyfile opened for reading")
|
slog.Debug("Keyfile opened for reading")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Exit(err, 1)
|
return err
|
||||||
}
|
}
|
||||||
slog.Debug("Private key parsed and is correct")
|
slog.Debug("Private key parsed and is correct")
|
||||||
}
|
}
|
||||||
@ -256,7 +257,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("Error while initlizing Wireguard netlink and device!")
|
slog.Warn("Error while initlizing Wireguard netlink and device!")
|
||||||
slog.Warn("Ensure to run as root or with CAP_NET_ADMIN")
|
slog.Warn("Ensure to run as root or with CAP_NET_ADMIN")
|
||||||
return cli.Exit(err, 1)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
wgLink = wg
|
wgLink = wg
|
||||||
@ -266,7 +267,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
|
|
||||||
dev, err := wgLink.Device(InterfaceName)
|
dev, err := wgLink.Device(InterfaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cli.Exit(err, 1)
|
return err
|
||||||
}
|
}
|
||||||
slog.Info("Initialized Wireguard device using wgctrl")
|
slog.Info("Initialized Wireguard device using wgctrl")
|
||||||
|
|
||||||
@ -294,7 +295,7 @@ func setup(ctx *cli.Context) error {
|
|||||||
err = ipPool.Free(testVip)
|
err = ipPool.Free(testVip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Could not free test Vip from IPPool!", err)
|
slog.Error("Could not free test Vip from IPPool!", err)
|
||||||
return cli.Exit(err.Error(), 1)
|
return err
|
||||||
}
|
}
|
||||||
slog.Debug("Test IP Freed")
|
slog.Debug("Test IP Freed")
|
||||||
|
|
||||||
@ -343,10 +344,12 @@ func testWgPeerAdd(wgLink *hvpnnode3.WGLink) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
peers, err := wgLink.GetAllPeers()
|
peers, err := wgLink.GetAllPeers()
|
||||||
if len(peers) != 0 {
|
if len(peers) != 0 {
|
||||||
slog.Warn(fmt.Sprintf("Expected 0 peers, got %d", len(peers)))
|
slog.Warn(fmt.Sprintf("Expected 0 peers, got %d", len(peers)))
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("Removed test peer")
|
slog.Debug("Removed test peer")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user