From ca2c70ec05da102902444e8e3a8f1d19bc986bb1 Mon Sep 17 00:00:00 2001 From: Bruno Schaatsbergen Date: Thu, 8 Sep 2022 13:03:11 +0200 Subject: [PATCH] Add flagstore and utils --- cmd/root.go | 51 +++++++++++++++++++--------------------------- go.mod | 7 ++++++- go.sum | 14 +++++++++++++ main.go | 4 ---- model/flagstore.go | 5 +++++ utils/utils.go | 33 ++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 model/flagstore.go create mode 100644 utils/utils.go diff --git a/cmd/root.go b/cmd/root.go index 2dcc80d..1619ef7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,43 +1,34 @@ package cmd import ( - "os" - + "github.com/bschaatsbergen/cidr/model" + "github.com/bschaatsbergen/cidr/utils" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "cidr", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: +var ( + version string -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.`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, -} + FlagStore model.FlagStore -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - err := rootCmd.Execute() - if err != nil { - os.Exit(1) + rootCmd = &cobra.Command{ + Use: "cidr", + Short: "cidr - cross platform cli to perform various operations on a cidr range", + Version: version, // The version is set during the build by making using of `go build -ldflags` + Run: func(cmd *cobra.Command, args []string) { + utils.ConfigureLogLevel(FlagStore.Debug) + cmd.Help() + }, } -} +) func init() { - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cidr.yaml)") + rootCmd.PersistentFlags().BoolVarP(&FlagStore.Debug, "debug", "d", false, "set log level to debug") +} - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +func Execute() { + if err := rootCmd.Execute(); err != nil { + logrus.Fatal(err) + } } diff --git a/go.mod b/go.mod index 448a057..3de02e7 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,13 @@ module github.com/bschaatsbergen/cidr go 1.19 +require ( + github.com/sirupsen/logrus v1.9.0 + github.com/spf13/cobra v1.5.0 +) + require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/spf13/cobra v1.5.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) diff --git a/go.sum b/go.sum index 0d85248..ea3a43c 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,24 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +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/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index ce2dd1e..66c4201 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,3 @@ -/* -Copyright © 2022 NAME HERE - -*/ package main import "github.com/bschaatsbergen/cidr/cmd" diff --git a/model/flagstore.go b/model/flagstore.go new file mode 100644 index 0000000..0d4a97e --- /dev/null +++ b/model/flagstore.go @@ -0,0 +1,5 @@ +package model + +type FlagStore struct { + Debug bool +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..920640f --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,33 @@ +package utils + +import ( + "os" + + "github.com/sirupsen/logrus" +) + +// Contains returns true if the given string is contained in the given slice of strings. +func Contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + +// configureLogLevel If an existing log level environment variable is present, re-use that to configure logrus. +func ConfigureLogLevel(debugLogsEnabled bool) { + logLevelStr, ok := os.LookupEnv("LOG_LEVEL") + if !ok { + logLevelStr = "info" + } + if debugLogsEnabled { + logLevelStr = "debug" + } + logLevel, err := logrus.ParseLevel(logLevelStr) + if err != nil { + logLevel = logrus.InfoLevel + } + logrus.SetLevel(logLevel) +}