Commit 647292d
Changed files (6)
cmd/networking.go
@@ -0,0 +1,20 @@
+package cmd
+
+import (
+ "fmt"
+
+ "github.com/spf13/cobra"
+)
+
+var networkingCmd = &cobra.Command{
+ Use: "networking",
+ Short: "Networking tools",
+ Long: `Networking tools`,
+ Run: func(cmd *cobra.Command, args []string) {
+ fmt.Println("Please specify subcommand")
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(networkingCmd)
+}
cmd/networking_get_current_ip.go
@@ -0,0 +1,48 @@
+package cmd
+
+import (
+ "fmt"
+ "log"
+ "net"
+ "strings"
+
+ "github.com/fatih/color"
+ "github.com/spf13/cobra"
+)
+
+func getLocalIP() string {
+ conn, err := net.Dial("udp", "8.8.8.8:80")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ localAddr := conn.LocalAddr().(*net.UDPAddr)
+
+ // sanitize
+ parts := strings.Split(fmt.Sprintf("%v", localAddr), ":")
+
+ localIP := ""
+ if len(parts) > 0 {
+ localIP = parts[0]
+ } else {
+ fmt.Println("Invalid address format")
+ }
+
+ return fmt.Sprintf("%v", localIP)
+}
+
+var getIPCmd = &cobra.Command{
+ Use: "get-ip",
+ Short: "Get IP information",
+ Long: `Get IP information`,
+ Run: func(cmd *cobra.Command, args []string) {
+ color.Green("Networking: get-ip")
+ fmt.Printf("\tLocal IP: %s\n", getLocalIP())
+
+ // public ip
+ },
+}
+
+func init() {
+ networkingCmd.AddCommand(getIPCmd)
+}
cmd/root.go
@@ -0,0 +1,25 @@
+package cmd
+
+import (
+ "os"
+
+ "github.com/spf13/cobra"
+)
+
+var rootCmd = &cobra.Command{
+ Use: "swissknife",
+ Version: "0.1.0",
+ Short: "Various utils",
+ Long: `Various utils`,
+}
+
+func Execute() {
+ err := rootCmd.Execute()
+ if err != nil {
+ os.Exit(1)
+ }
+}
+
+func init() {
+ rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}
go.mod
@@ -0,0 +1,16 @@
+module github.com/kahnwong/swissknife
+
+go 1.20
+
+require (
+ github.com/fatih/color v1.15.0
+ github.com/spf13/cobra v1.7.0
+)
+
+require (
+ github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ github.com/mattn/go-colorable v0.1.13 // indirect
+ github.com/mattn/go-isatty v0.0.17 // indirect
+ github.com/spf13/pflag v1.0.5 // indirect
+ golang.org/x/sys v0.13.0 // indirect
+)
go.sum
@@ -0,0 +1,20 @@
+github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
+github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
+github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
+github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
+github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
+github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
+golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
main.go
@@ -0,0 +1,7 @@
+package main
+
+import "github.com/kahnwong/swissknife/cmd"
+
+func main() {
+ cmd.Execute()
+}