Skip to content

Commit

Permalink
feat: add multiple subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Jan 27, 2024
1 parent 9cc7f8a commit 35e4ed8
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 33 deletions.
Empty file removed cmd/cli/add/.gitkeep
Empty file.
46 changes: 46 additions & 0 deletions cmd/cli/add/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package add

import (
"fmt"

"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/utils"
"github.com/bilalcaliskan/split-the-tunnel/internal/logging"
"github.com/spf13/cobra"
)

// AddCmd represents the add command
var AddCmd = &cobra.Command{
Use: "add",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.ErrNoArgs
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
logger := logging.GetLogger()

logger.Info().Any("args", args).Msg("add called")

for _, arg := range args {
req := fmt.Sprintf("%s %s", cmd.Name(), arg)
res, err := utils.SendCommandToDaemon(utils.SocketPath, req)
if err != nil {
logger.Error().Str("command", req).Str("response", res).Err(err).Msg("error sending command to daemon")
continue
}

logger.Info().Str("command", req).Str("response", res).Msg("successfully processed command")
}

return nil
},
}
48 changes: 19 additions & 29 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,32 @@ package main
import (
"os"

"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/add"
"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/list"
"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/remove"
"github.com/bilalcaliskan/split-the-tunnel/internal/version"

"github.com/spf13/cobra"
)

var ver = version.Get()

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "stt-cli",
Short: "",
Long: ``,
Version: ver.GitVersion,
}
var (
ver = version.Get()
cliCmd = &cobra.Command{
Use: "stt-cli",
Short: "",
Long: ``,
Version: ver.GitVersion,
}
)

func main() {
if err := rootCmd.Execute(); err != nil {
if err := cliCmd.Execute(); err != nil {
os.Exit(1)
}
}

//const socketPath = "/tmp/mydaemon.sock"

//func sendCommandToDaemon(command string) (string, error) {
// conn, err := net.Dial("unix", socketPath)
// if err != nil {
// return "", err
// }
// defer conn.Close()
//
// _, err = conn.Write([]byte(command + "\n"))
// if err != nil {
// return "", err
// }
//
// // If you expect a response from the daemon, read it here
// // For example, using bufio.NewReader(conn).ReadString('\n')
//
// return "Command sent successfully", nil
//}
func init() {
cliCmd.AddCommand(add.AddCmd)
cliCmd.AddCommand(list.ListCmd)
cliCmd.AddCommand(remove.RemoveCmd)
}
Empty file removed cmd/cli/list/.gitkeep
Empty file.
46 changes: 46 additions & 0 deletions cmd/cli/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package list

import (
"strings"

"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/utils"
"github.com/bilalcaliskan/split-the-tunnel/internal/logging"

"github.com/spf13/cobra"
)

// ListCmd represents the list command
var ListCmd = &cobra.Command{
Use: "list",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return utils.ErrTooManyArgs
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
logger := logging.GetLogger()

argsStr := strings.Join(args, " ")

logger.Info().Str("args", argsStr).Msg("list called")

req := cmd.Name()
res, err := utils.SendCommandToDaemon(utils.SocketPath, req)
if err != nil {
logger.Error().Err(err).Msg("error sending command to daemon")
return err
}

logger.Info().Str("command", req).Str("response", res).Msg("successfully processed command")
return nil
},
}
Empty file removed cmd/cli/remove/.gitkeep
Empty file.
47 changes: 47 additions & 0 deletions cmd/cli/remove/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package remove

import (
"fmt"
"strings"

"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/utils"
"github.com/bilalcaliskan/split-the-tunnel/internal/logging"

"github.com/spf13/cobra"
)

// RemoveCmd represents the remove command
var RemoveCmd = &cobra.Command{
Use: "remove",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.ErrNoArgs
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
logger := logging.GetLogger()

argsStr := strings.Join(args, " ")

logger.Info().Str("args", argsStr).Msg("remove called")

req := fmt.Sprintf("%s %s", cmd.Name(), argsStr)
res, err := utils.SendCommandToDaemon(utils.SocketPath, req)
if err != nil {
logger.Error().Err(err).Msg("error sending command to daemon")
return err
}

logger.Info().Str("command", req).Str("response", res).Msg("successfully processed command")
return nil
},
}
35 changes: 35 additions & 0 deletions cmd/cli/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package utils

import (
"bufio"
"errors"
"net"
)

const SocketPath = "/tmp/mydaemon.sock"

var (
ErrNoArgs = errors.New("no arguments provided")
ErrTooManyArgs = errors.New("too many arguments provided")
)

func SendCommandToDaemon(socketPath, command string) (string, error) {
conn, err := net.Dial("unix", socketPath)
if err != nil {
return "", err
}
defer conn.Close()

_, err = conn.Write([]byte(command + "\n"))
if err != nil {
return "", err
}

// Read response from daemon
response, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return "", err
}

return response, nil
}
7 changes: 3 additions & 4 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
)

type RouteEntry struct {
Domain string `json:"domain"`
ResolvedIP string `json:"resolvedIP"`
OriginalGateway string `json:"originalGateway"`
// Add other fields as necessary
Domain string `json:"domain"`
ResolvedIP string `json:"resolvedIP"`
Gateway string `json:"gateway"`
}

type State struct {
Expand Down

0 comments on commit 35e4ed8

Please sign in to comment.