Skip to content

Commit

Permalink
Merge pull request #4 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
bilalcaliskan authored Jan 28, 2024
2 parents c3a4a49 + 5145665 commit b34e2e6
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 125 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).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.
42 changes: 42 additions & 0 deletions cmd/cli/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package list

import (
"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()

logger.Info().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
},
}
52 changes: 52 additions & 0 deletions cmd/cli/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package utils

import (
"encoding/json"
"net"

"github.com/pkg/errors"
)

const SocketPath = "/tmp/mydaemon.sock"

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

type DaemonResponse struct {
Success bool `json:"success"`
Response string `json:"response"`
Error string `json:"error"`
}

func SendCommandToDaemon(socketPath, command string) (string, error) {
conn, err := net.Dial("unix", socketPath)
if err != nil {
return "", errors.Wrap(err, "failed to connect to unix domain socket")
}
defer conn.Close()

_, err = conn.Write([]byte(command + "\n"))
if err != nil {
return "", errors.Wrap(err, "failed to write to unix domain socket")
}

buf := make([]byte, 1024)
n, err := conn.Read(buf[:])
if err != nil {
return "", err
}

var response DaemonResponse
if err := json.Unmarshal(buf[:n], &response); err != nil {
return "", err
}

var respErr error
if response.Error != "" {
respErr = errors.New(response.Error)
}

return response.Response, respErr
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bilalcaliskan/split-the-tunnel
go 1.21

require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
Loading

0 comments on commit b34e2e6

Please sign in to comment.