From 140ad2dbe350a8e07020b0debb456eff149cee1e Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sat, 9 Mar 2024 17:27:08 +0300 Subject: [PATCH 1/3] feat: config reading from file --- cmd/daemon/daemon.go | 2 +- cmd/daemon/options/options.go | 23 +++++++++- cmd/daemon/options/options_test.go | 2 +- go.mod | 25 ++++++++--- go.sum | 67 +++++++++++++++++++++++++----- internal/config/config.go | 32 ++++++++++++++ internal/config/types.go | 9 ++++ 7 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 internal/config/config.go create mode 100644 internal/config/types.go diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index f79b178..68266a2 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -26,7 +26,7 @@ var ( func init() { opts = options.GetRootOptions() - opts.InitFlags(daemonCmd) + panic(opts.InitFlags(daemonCmd)) } // daemonCmd represents the base command when called without any subcommands diff --git a/cmd/daemon/options/options.go b/cmd/daemon/options/options.go index 38fcaf6..a956b40 100644 --- a/cmd/daemon/options/options.go +++ b/cmd/daemon/options/options.go @@ -1,11 +1,19 @@ package options -import "github.com/spf13/cobra" +import ( + "os" + "path/filepath" + + "github.com/pkg/errors" + "github.com/spf13/cobra" +) var rootOptions = &RootOptions{} // RootOptions contains frequent command line and application options. type RootOptions struct { + Workspace string + ConfigFile string DnsServers string CheckIntervalMin int Verbose bool @@ -16,8 +24,19 @@ func GetRootOptions() *RootOptions { return rootOptions } -func (opts *RootOptions) InitFlags(cmd *cobra.Command) { +func (opts *RootOptions) InitFlags(cmd *cobra.Command) error { + homeDir, err := os.UserHomeDir() + if err != nil { + return errors.Wrap(err, "failed to get user home directory") + } + + workspace := filepath.Join(homeDir, ".split-the-tunnel") + + cmd.Flags().StringVarP(&opts.Workspace, "workspace", "w", workspace, "workspace directory path") + cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", filepath.Join(workspace, "config.toml"), "config file path") cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "", false, "verbose logging output") cmd.Flags().StringVarP(&opts.DnsServers, "dns-servers", "", "", "comma separated dns servers to be used for DNS resolving") cmd.Flags().IntVarP(&opts.CheckIntervalMin, "check-interval-min", "", 5, "routing table check interval with collected state, in minutes") + + return nil } diff --git a/cmd/daemon/options/options_test.go b/cmd/daemon/options/options_test.go index 0ca9b60..f14f5db 100644 --- a/cmd/daemon/options/options_test.go +++ b/cmd/daemon/options/options_test.go @@ -15,5 +15,5 @@ func TestGetRootOptions(t *testing.T) { func TestRootOptions_InitFlags(t *testing.T) { cmd := cobra.Command{} opts := GetRootOptions() - opts.InitFlags(&cmd) + assert.NoError(t, opts.InitFlags(&cmd)) } diff --git a/go.mod b/go.mod index 10d9bc8..4ec1029 100644 --- a/go.mod +++ b/go.mod @@ -7,21 +7,34 @@ require ( github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.32.0 github.com/spf13/cobra v1.8.0 + github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 ) require ( - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/kr/pretty v0.1.0 // indirect - github.com/kr/text v0.2.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/sys v0.17.0 // indirect - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 30efa75..1fb3025 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,26 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= 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= @@ -21,32 +30,68 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= 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= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ= +golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..a4ffaf4 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,32 @@ +package config + +import ( + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func ReadConfig(cmd *cobra.Command, path string) (conf *Config, err error) { + // set config file name and path + viper.SetConfigName(path) + viper.SetConfigType("toml") + viper.AddConfigPath(".") + + // read config file + if err := viper.ReadInConfig(); err != nil { + return nil, errors.Wrap(err, "an error occurred while reading config file") + } + + // unmarshal config file into Config struct + if err := viper.Unmarshal(&conf); err != nil { + return nil, errors.Wrap(err, "an error occurred while unmarshaling config file") + } + + // if a flag is set, it overrides the value in config file + if cmd.Flags().Changed("verbose") { + verbose, _ := cmd.Flags().GetBool("verbose") + conf.Verbose = verbose + } + + return conf, nil +} diff --git a/internal/config/types.go b/internal/config/types.go new file mode 100644 index 0000000..a3f871f --- /dev/null +++ b/internal/config/types.go @@ -0,0 +1,9 @@ +package config + +// Config struct represents the config file +type Config struct { + CheckIntervalMin int `toml:"check-interval-min"` + DnsServers []string `toml:"dns-servers"` + Verbose bool `toml:"verbose"` + SocketPath string `toml:"socket-path"` +} From 26191c6249d8db83e7cb87575ab6a7cfd8e65346 Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sun, 10 Mar 2024 12:34:11 +0300 Subject: [PATCH 2/3] chore: local sync --- cmd/daemon/daemon.go | 9 +++++- cmd/daemon/options/options.go | 57 ++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 68266a2..3c04d54 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -1,11 +1,14 @@ package main import ( + "fmt" "os" "os/signal" "syscall" "time" + "github.com/pkg/errors" + "github.com/bilalcaliskan/split-the-tunnel/internal/state" "github.com/bilalcaliskan/split-the-tunnel/internal/constants" @@ -26,7 +29,9 @@ var ( func init() { opts = options.GetRootOptions() - panic(opts.InitFlags(daemonCmd)) + if err := opts.InitFlags(daemonCmd); err != nil { + panic(errors.Wrap(err, "failed to initialize flags")) + } } // daemonCmd represents the base command when called without any subcommands @@ -36,6 +41,8 @@ var daemonCmd = &cobra.Command{ Long: ``, Version: ver.GitVersion, RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(opts) + logger := logging.GetLogger().With().Str("job", "main").Logger() logger.Info().Str("appVersion", ver.GitVersion).Str("goVersion", ver.GoVersion).Str("goOS", ver.GoOs). Str("goArch", ver.GoArch).Str("gitCommit", ver.GitCommit).Str("buildDate", ver.BuildDate). diff --git a/cmd/daemon/options/options.go b/cmd/daemon/options/options.go index a956b40..8ec9709 100644 --- a/cmd/daemon/options/options.go +++ b/cmd/daemon/options/options.go @@ -1,9 +1,12 @@ package options import ( + "log" "os" "path/filepath" + "github.com/spf13/viper" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -12,11 +15,11 @@ var rootOptions = &RootOptions{} // RootOptions contains frequent command line and application options. type RootOptions struct { - Workspace string - ConfigFile string - DnsServers string - CheckIntervalMin int - Verbose bool + Workspace string // This field will be managed by the command line argument + ConfigFile string // This field will be managed by the command line argument + DnsServers string `toml:"dns-servers"` // This field will be managed by the config file + CheckIntervalMin int `toml:"check-interval-min"` // This field will be managed by the config file + Verbose bool `toml:"verbose"` // This field will be managed by the config file } // GetRootOptions returns the pointer of RootOptions @@ -25,18 +28,56 @@ func GetRootOptions() *RootOptions { } func (opts *RootOptions) InitFlags(cmd *cobra.Command) error { + if err := opts.setWorkspace(); err != nil { + return err + } + + opts.setFlags(cmd) + viper.BindPFlags(cmd.Flags()) + viper.SetConfigFile(opts.ConfigFile) + viper.SetConfigType("toml") + + if err := opts.readConfig(); err != nil { + return err + } + + return nil +} + +func (opts *RootOptions) setWorkspace() error { homeDir, err := os.UserHomeDir() if err != nil { return errors.Wrap(err, "failed to get user home directory") } - workspace := filepath.Join(homeDir, ".split-the-tunnel") + opts.Workspace = filepath.Join(homeDir, ".split-the-tunnel") + opts.ConfigFile = filepath.Join(opts.Workspace, "config.toml") + + return nil +} - cmd.Flags().StringVarP(&opts.Workspace, "workspace", "w", workspace, "workspace directory path") - cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", filepath.Join(workspace, "config.toml"), "config file path") +func (opts *RootOptions) setFlags(cmd *cobra.Command) { + cmd.Flags().StringVarP(&opts.Workspace, "workspace", "w", opts.Workspace, "workspace directory path") + cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", opts.ConfigFile, "config file path") cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "", false, "verbose logging output") cmd.Flags().StringVarP(&opts.DnsServers, "dns-servers", "", "", "comma separated dns servers to be used for DNS resolving") cmd.Flags().IntVarP(&opts.CheckIntervalMin, "check-interval-min", "", 5, "routing table check interval with collected state, in minutes") +} + +func (opts *RootOptions) readConfig() error { + if _, err := os.Stat(opts.ConfigFile); os.IsNotExist(err) { + log.Println("Warning: Config file not found. Using default values.") + } else if err != nil { + return errors.Wrap(err, "an error occurred while accessing config file") + } else { + if err := viper.ReadInConfig(); err != nil { + return errors.Wrap(err, "an error occurred while reading config file") + } + + if err := viper.Unmarshal(opts); err != nil { + return errors.Wrap(err, "an error occurred while unmarshaling config file") + } + } return nil } From a53500adebaf300e5abec49ec746ce4a0d14d75d Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sun, 10 Mar 2024 15:50:56 +0300 Subject: [PATCH 3/3] feat: more configuration of both daaemon and cli --- Makefile | 6 +++ cmd/cli/add/add.go | 3 +- cmd/cli/cli.go | 21 ++++++++-- cmd/cli/list/list.go | 3 +- cmd/cli/purge/purge.go | 3 +- cmd/cli/remove/remove.go | 3 +- cmd/cli/utils/utils.go | 2 - cmd/daemon/daemon.go | 18 ++++++--- cmd/daemon/options/options.go | 72 ++++++++++++++++++++--------------- internal/constants/others.go | 5 ++- internal/ipc/ipc.go | 15 ++++---- 11 files changed, 96 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index 1e8d601..eff1d6b 100644 --- a/Makefile +++ b/Makefile @@ -111,6 +111,12 @@ run-daemon: build-daemon chmod +x ./.bin/split-the-tunnel sudo ./.bin/split-the-tunnel $(DEFAULT_GO_RUN_ARGS) +.PHONY: run-cli +run-cli: build-cli + $(info running cli...) + chmod +x ./.bin/stt-cli + sudo ./.bin/stt-cli $(DEFAULT_GO_RUN_ARGS) + .PHONY: prepare-initial-project GITHUB_USERNAME ?= $(shell read -p "Your Github username(ex: bilalcaliskan): " github_username; echo $$github_username) PROJECT_NAME ?= $(shell read -p "'Kebab-cased' Project Name(ex: golang-cli-template): " project_name; echo $$project_name) diff --git a/cmd/cli/add/add.go b/cmd/cli/add/add.go index 426c950..5c1245a 100644 --- a/cmd/cli/add/add.go +++ b/cmd/cli/add/add.go @@ -30,6 +30,7 @@ to quickly create a Cobra application.`, }, 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()). @@ -38,7 +39,7 @@ to quickly create a Cobra application.`, for _, arg := range args { req := fmt.Sprintf("%s %s", cmd.Name(), arg) - res, err := utils.SendCommandToDaemon(utils.SocketPath, req) + res, err := utils.SendCommandToDaemon(socketPath, req) if err != nil { logger.Error().Str("command", req).Err(err).Msg(constants.FailedToProcessCommand) continue diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index be7fe5a..1ba716c 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -2,13 +2,14 @@ package main import ( "context" - "errors" "os" + "path/filepath" "github.com/bilalcaliskan/split-the-tunnel/cmd/cli/purge" "github.com/bilalcaliskan/split-the-tunnel/internal/constants" "github.com/bilalcaliskan/split-the-tunnel/internal/logging" + "github.com/pkg/errors" "github.com/bilalcaliskan/split-the-tunnel/cmd/cli/add" "github.com/bilalcaliskan/split-the-tunnel/cmd/cli/list" @@ -20,9 +21,11 @@ import ( ) var ( - verbose bool - ver = version.Get() - cliCmd = &cobra.Command{ + verbose bool + socketFile string + workspace string + ver = version.Get() + cliCmd = &cobra.Command{ Use: "stt-cli", Short: "", Long: ``, @@ -38,6 +41,9 @@ var ( logger.Debug().Str("foo", "bar").Msg("this is a dummy log") } + socketPath := filepath.Join(workspace, socketFile) + + cmd.SetContext(context.WithValue(cmd.Context(), constants.SocketPathKey{}, socketPath)) cmd.SetContext(context.WithValue(cmd.Context(), constants.LoggerKey{}, logger)) }, } @@ -54,6 +60,13 @@ func main() { } func init() { + homeDir, err := os.UserHomeDir() + if err != nil { + panic(errors.Wrap(err, "failed to get user home directory")) + } + + cliCmd.PersistentFlags().StringVarP(&workspace, "workspace", "w", filepath.Join(homeDir, ".split-the-tunnel"), "workspace directory path") + cliCmd.PersistentFlags().StringVarP(&socketFile, "socket-file", "s", "ipc.sock", "unix domain socket file in workspace") cliCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose mode") cliCmd.AddCommand(add.AddCmd) diff --git a/cmd/cli/list/list.go b/cmd/cli/list/list.go index 327b400..d824057 100644 --- a/cmd/cli/list/list.go +++ b/cmd/cli/list/list.go @@ -32,12 +32,13 @@ to quickly create a Cobra application.`, }, 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(utils.SocketPath, cmd.Name()) + res, err := utils.SendCommandToDaemon(socketPath, cmd.Name()) if err != nil { logger.Error().Str("command", cmd.Name()).Err(err).Msg(constants.FailedToProcessCommand) diff --git a/cmd/cli/purge/purge.go b/cmd/cli/purge/purge.go index da7d09b..edc6d57 100644 --- a/cmd/cli/purge/purge.go +++ b/cmd/cli/purge/purge.go @@ -30,12 +30,13 @@ to quickly create a Cobra application.`, }, 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(utils.SocketPath, cmd.Name()) + res, err := utils.SendCommandToDaemon(socketPath, cmd.Name()) if err != nil { logger.Error().Str("command", cmd.Name()).Err(err).Msg(constants.FailedToProcessCommand) diff --git a/cmd/cli/remove/remove.go b/cmd/cli/remove/remove.go index 3b14e71..feb1406 100644 --- a/cmd/cli/remove/remove.go +++ b/cmd/cli/remove/remove.go @@ -30,6 +30,7 @@ to quickly create a Cobra application.`, }, 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()). @@ -38,7 +39,7 @@ to quickly create a Cobra application.`, for _, arg := range args { req := fmt.Sprintf("%s %s", cmd.Name(), arg) - res, err := utils.SendCommandToDaemon(utils.SocketPath, req) + res, err := utils.SendCommandToDaemon(socketPath, req) if err != nil { logger.Error(). Str("command", req). diff --git a/cmd/cli/utils/utils.go b/cmd/cli/utils/utils.go index 22d562e..7b49396 100644 --- a/cmd/cli/utils/utils.go +++ b/cmd/cli/utils/utils.go @@ -9,8 +9,6 @@ import ( "github.com/pkg/errors" ) -const SocketPath = "/tmp/mydaemon.sock" - var ( ErrNoArgs = errors.New("no arguments provided") ErrTooManyArgs = errors.New("too many arguments provided") diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index 3c04d54..7b7e585 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -20,8 +20,6 @@ import ( "github.com/spf13/cobra" ) -const socketPath = "/tmp/mydaemon.sock" - var ( opts *options.RootOptions ver = version.Get() @@ -41,8 +39,16 @@ var daemonCmd = &cobra.Command{ Long: ``, Version: ver.GitVersion, RunE: func(cmd *cobra.Command, args []string) error { + if err := opts.ReadConfig(); err != nil { + return errors.Wrap(err, "failed to read config") + } + fmt.Println(opts) + if err := os.MkdirAll(opts.Workspace, 0755); err != nil { + return errors.Wrap(err, "failed to create workspace directory") + } + logger := logging.GetLogger().With().Str("job", "main").Logger() logger.Info().Str("appVersion", ver.GitVersion).Str("goVersion", ver.GoVersion).Str("goOS", ver.GoOs). Str("goArch", ver.GoArch).Str("gitCommit", ver.GitCommit).Str("buildDate", ver.BuildDate). @@ -55,20 +61,20 @@ var daemonCmd = &cobra.Command{ st := state.NewState(logger) // Initialize IPC mechanism - if err := ipc.InitIPC(st, socketPath, logger); err != nil { + if err := ipc.InitIPC(st, opts.SocketPath, logger); err != nil { logger.Error().Err(err).Msg(constants.FailedToInitializeIPC) return err } - logger.Info().Str("socket", socketPath).Msg(constants.IPCInitialized) + logger.Info().Str("socket", opts.SocketPath).Msg(constants.IPCInitialized) defer func() { - if err := ipc.Cleanup(socketPath); err != nil { + if err := ipc.Cleanup(opts.SocketPath); err != nil { logger.Error().Err(err).Msg(constants.FailedToCleanupIPC) } }() - logger.Info().Str("socket", socketPath).Msg(constants.DaemonRunning) + logger.Info().Str("socket", opts.SocketPath).Msg(constants.DaemonRunning) go func() { // Create a ticker that fires every 5 minutes diff --git a/cmd/daemon/options/options.go b/cmd/daemon/options/options.go index 8ec9709..e369b62 100644 --- a/cmd/daemon/options/options.go +++ b/cmd/daemon/options/options.go @@ -19,6 +19,7 @@ type RootOptions struct { ConfigFile string // This field will be managed by the command line argument DnsServers string `toml:"dns-servers"` // This field will be managed by the config file CheckIntervalMin int `toml:"check-interval-min"` // This field will be managed by the config file + SocketPath string `toml:"socket-path"` // This field will be managed by the config file Verbose bool `toml:"verbose"` // This field will be managed by the config file } @@ -28,55 +29,66 @@ func GetRootOptions() *RootOptions { } func (opts *RootOptions) InitFlags(cmd *cobra.Command) error { - if err := opts.setWorkspace(); err != nil { - return err + if err := opts.setFlags(cmd); err != nil { + return errors.Wrap(err, "failed to set flags") } - opts.setFlags(cmd) - viper.BindPFlags(cmd.Flags()) - viper.SetConfigFile(opts.ConfigFile) - viper.SetConfigType("toml") - - if err := opts.readConfig(); err != nil { - return err + if err := viper.BindPFlags(cmd.Flags()); err != nil { + return errors.Wrap(err, "failed to bind flags") } return nil } -func (opts *RootOptions) setWorkspace() error { +//func (opts *RootOptions) setDefaultWorkspace() error { +// homeDir, err := os.UserHomeDir() +// if err != nil { +// return errors.Wrap(err, "failed to get user home directory") +// } +// +// ws := filepath.Join(homeDir, ".split-the-tunnel") +// opts.Workspace = ws +// opts.ConfigFile = filepath.Join(ws, "config.toml") +// +// return nil +//} + +func (opts *RootOptions) setFlags(cmd *cobra.Command) error { homeDir, err := os.UserHomeDir() if err != nil { return errors.Wrap(err, "failed to get user home directory") } - opts.Workspace = filepath.Join(homeDir, ".split-the-tunnel") - opts.ConfigFile = filepath.Join(opts.Workspace, "config.toml") - - return nil -} - -func (opts *RootOptions) setFlags(cmd *cobra.Command) { - cmd.Flags().StringVarP(&opts.Workspace, "workspace", "w", opts.Workspace, "workspace directory path") - cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", opts.ConfigFile, "config file path") + cmd.Flags().StringVarP(&opts.Workspace, "workspace", "w", filepath.Join(homeDir, ".split-the-tunnel"), "workspace directory path") + cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", "config.toml", "config file path, will search in workspace") + cmd.Flags().StringVarP(&opts.SocketPath, "socket-path", "", "ipc.sock", "unix domain socket path in workspace") cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "", false, "verbose logging output") cmd.Flags().StringVarP(&opts.DnsServers, "dns-servers", "", "", "comma separated dns servers to be used for DNS resolving") cmd.Flags().IntVarP(&opts.CheckIntervalMin, "check-interval-min", "", 5, "routing table check interval with collected state, in minutes") + + return nil } -func (opts *RootOptions) readConfig() error { +func (opts *RootOptions) ReadConfig() error { + viper.SetConfigFile(opts.ConfigFile) + viper.SetConfigType("toml") + + opts.SocketPath = filepath.Join(opts.Workspace, opts.SocketPath) + opts.ConfigFile = filepath.Join(opts.Workspace, opts.ConfigFile) + if _, err := os.Stat(opts.ConfigFile); os.IsNotExist(err) { - log.Println("Warning: Config file not found. Using default values.") + log.Printf("config file not found in %s, will use default values\n", opts.ConfigFile) + return nil } else if err != nil { - return errors.Wrap(err, "an error occurred while accessing config file") - } else { - if err := viper.ReadInConfig(); err != nil { - return errors.Wrap(err, "an error occurred while reading config file") - } - - if err := viper.Unmarshal(opts); err != nil { - return errors.Wrap(err, "an error occurred while unmarshaling config file") - } + return errors.Wrap(err, "failed to access config file") + } + + if err := viper.ReadInConfig(); err != nil { + return errors.Wrap(err, "failed to read config file") + } + + if err := viper.Unmarshal(opts); err != nil { + return errors.Wrap(err, "failed to unmarshal config file") } return nil diff --git a/internal/constants/others.go b/internal/constants/others.go index fcc7d8b..515672e 100644 --- a/internal/constants/others.go +++ b/internal/constants/others.go @@ -4,4 +4,7 @@ const ( StateFilePath = "/tmp/state.json" ) -type LoggerKey struct{} +type ( + LoggerKey struct{} + SocketPathKey struct{} +) diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go index d6f0222..c70478a 100644 --- a/internal/ipc/ipc.go +++ b/internal/ipc/ipc.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net" - "os" "strings" "github.com/bilalcaliskan/split-the-tunnel/internal/constants" @@ -19,18 +18,18 @@ import ( ) // InitIPC initializes the IPC setup and continuously listens on the given path for incoming connections -func InitIPC(st *state.State, path string, logger zerolog.Logger) error { +func InitIPC(st *state.State, socketPath string, logger zerolog.Logger) error { logger = logger.With().Str("job", "ipc").Logger() // Check and remove the socket file if it already exists - if _, err := os.Stat(path); err == nil { - if err := os.Remove(path); err != nil { - return err - } - } + //if _, err := os.Stat(opts.SocketPath); err == nil { + // if err := os.Remove(opts.SocketPath); err != nil { + // return err + // } + //} // Listen on the UNIX domain socket - listener, err := net.Listen("unix", path) + listener, err := net.Listen("unix", socketPath) if err != nil { return err }