generated from bilalcaliskan/golang-cli-template
/
list.go
73 lines (57 loc) · 2.21 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package list
import (
"os"
"strings"
"github.com/bilalcaliskan/split-the-tunnel/internal/state"
"github.com/olekukonko/tablewriter"
"github.com/bilalcaliskan/split-the-tunnel/cmd/cli/utils"
"github.com/bilalcaliskan/split-the-tunnel/internal/constants"
"github.com/rs/zerolog"
"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 := cmd.Context().Value(constants.LoggerKey{}).(zerolog.Logger)
socketPath := cmd.Context().Value(constants.SocketPathKey{}).(string)
logger.Info().
Str("operation", cmd.Name()).
Msg(constants.ProcessCommand)
res, err := utils.SendCommandToDaemon(socketPath, cmd.Name())
if err != nil {
logger.Error().Str("command", cmd.Name()).Err(err).Msg(constants.FailedToProcessCommand)
return &utils.CommandError{Err: err, Code: 10}
}
logger.Info().Str("command", cmd.Name()).Msg(constants.SuccessfullyProcessed)
domains, err := state.FromStringSlice(res)
if err != nil {
logger.Error().Err(err).Msg("failed to parse response")
return &utils.CommandError{Err: err, Code: 11}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Domain", "Gateway", "IPs"})
// Set the Alignment for each column to center
table.SetColumnAlignment([]int{tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
table.SetBorder(true) // Set to false if you do not want borders
table.SetRowLine(true) // Enable row line for more clarity
table.SetAlignment(tablewriter.ALIGN_CENTER)
for _, info := range domains {
table.Append([]string{info.Domain, info.Gateway, strings.Join(info.ResolvedIPs, "\n")})
}
table.Render() // Send output
return nil
},
}