Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A comprehensive Go CLI tool for managing FreeBSD infrastructure resources.
- OS level
- [Interfaces Management](#network-management)
- [IP Address Management](#ip-address-management)
- [Route Management](#route-management)


## Examples
Expand Down Expand Up @@ -223,6 +224,75 @@ A comprehensive Go CLI tool for managing FreeBSD infrastructure resources.
./fcom ip delete --iface em0 --ip 2001:db8::1 --mask 64 --family inet6
```

### Route Management

Easily manage IPv4 and IPv6 routes, including adding, deleting, and listing routes. The CLI ensures you cannot accidentally remove the last default route, protecting system connectivity.

#### Add a Route

```bash
# Add an IPv4 network route
./fcom route add --family inet --net 10.0.0.0/24 --gw 10.0.0.1

# Add an IPv6 network route
./fcom route add --family inet6 --net 2001:db8::/64 --gw 2001:db8::1

# Add an IPv4 network route via a specific interface
./fcom route add --family inet --net 10.0.0.0/24 --gw 10.0.0.1 --iface em0

# Add an IPv6 network route via a specific interface
./fcom route add --family inet6 --net 2001:db8::/64 --gw 2001:db8::1 --iface em1

# Add a default IPv4 route
./fcom route add --family inet --net default --gw 192.168.1.1

# Add a host route (single IP)
./fcom route add --family inet --net 192.168.1.50/32 --gw 10.0.0.1
```

> **Note:** The `--iface` flag sets the outgoing interface for the route (uses FreeBSD's `-ifp` option). It is optional.

#### Delete a Route

```bash
# Delete an IPv4 network route
./fcom route del --family inet --net 10.0.0.0/24

# Delete an IPv6 network route
./fcom route del --family inet6 --net 2001:db8::/64

# Delete a default route (will fail if it's the last default route)
./fcom route del --family inet --net default
# Output: cannot delete the last default route
```

#### List Routes

```bash
# List all IPv4 routes
./fcom route list --family inet

# List IPv6 routes
./fcom route list --family inet6


# List all routes
./fcom route list
```

#### Example: Safe Default Route Handling

```bash
# Attempting to delete the last default route will fail:
./fcom route del --family inet --net default
# Output: cannot delete the last default route
```

#### Troubleshooting
- Ensure you specify the correct `--family` (either `inet` for IPv4 or `inet6` for IPv6).
- The `--net` flag accepts both network prefixes (e.g., `10.0.0.0/24`) and `default`.
- You cannot delete the last default route for a family; this is a safety feature.

### Version Information

```bash
Expand Down
2 changes: 2 additions & 0 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,7 @@ func init() { //nolint
os.Exit(1)
}

networkCmd.AddCommand(routeCmd)

cmd.AddCommand(networkCmd)
}
86 changes: 86 additions & 0 deletions cmd/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cmd

import (
"FreeBSD-Command-manager/internal/network/bareos"
"fmt"
"os"

"github.com/spf13/cobra"
)

var (
routeFamily string
routeNet string
routeGW string
routeIface string // new flag for interface
)

var routeCmd = &cobra.Command{
Use: "route",
Short: "Manage routes (add, del, list)",
}

var routeAddCmd = &cobra.Command{
Use: "add",
Short: "Add a route",
Run: func(_ *cobra.Command, _ []string) {
if routeNet == "" || routeGW == "" {
fmt.Fprintln(os.Stderr, "--net and --gw are required")
os.Exit(1)
}
err := bareos.AddRouteWithIface(routeFamily, routeNet, routeGW, routeIface)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Route added successfully")
},
}

var routeDelCmd = &cobra.Command{
Use: "del",
Short: "Delete a route",
Run: func(cmd *cobra.Command, args []string) { //nolint:revive // cmd is required by cobra interface
if routeNet == "" {
fmt.Fprintln(os.Stderr, "--net is required")
os.Exit(1)
}
err := bareos.DelRoute(routeFamily, routeNet)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Route deleted successfully")
},
}

var routeListCmd = &cobra.Command{
Use: "list",
Short: "List routes",
Run: func(cmd *cobra.Command, args []string) { //nolint:revive // cmd is required by cobra interface
out, err := bareos.ListAllRoutes(routeFamily)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(out)
},
}

func init() { //nolint
routeAddCmd.Flags().StringVar(&routeFamily, "family", "inet", "Address family (inet or inet6)")
routeAddCmd.Flags().StringVar(&routeNet, "net", "", "Network or destination (required)")
routeAddCmd.Flags().StringVar(&routeGW, "gw", "", "Gateway (required)")
routeAddCmd.Flags().StringVar(&routeIface, "iface", "", "Outgoing interface (optional)")

routeDelCmd.Flags().StringVar(&routeFamily, "family", "inet", "Address family (inet or inet6)")
routeDelCmd.Flags().StringVar(&routeNet, "net", "", "Network or destination (required)")

routeListCmd.Flags().StringVar(&routeFamily, "family", "", "Address family (inet, inet6, or empty for all)")

routeCmd.AddCommand(routeAddCmd)
routeCmd.AddCommand(routeDelCmd)
routeCmd.AddCommand(routeListCmd)

cmd.AddCommand(routeCmd)
}
Loading
Loading