From 798e7dc73852aed8a7a9f8f59bdfc2228def96e6 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 20 Oct 2023 16:32:23 +0300 Subject: [PATCH 1/3] integration of std lib flag package support for glog&err2, version cmd --- README.md | 2 +- cmd/root.go | 21 ++++++++++++--------- cmd/version.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 cmd/version.go diff --git a/README.md b/README.md index 37b429d1..7e732bee 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ setup environment and especially install `libindy`. 2. Clone [findy-agent](https://github.com/findy-network/findy-agent) (this repo) 3. Install needed Go packages: `make deps`. 4. Install the command line application: `make install` -5. Verify the installation: `findy-agent -version` +5. Verify the installation: `findy-agent version` It should output similar to: `findy-agent version 0.xx.xx` diff --git a/cmd/root.go b/cmd/root.go index 04130f9c..c7b59703 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,16 +1,14 @@ package cmd import ( + goflag "flag" "fmt" "log" "os" "strings" - "github.com/findy-network/findy-agent/agent/utils" - "github.com/findy-network/findy-agent/cmds/agency" "github.com/golang/glog" "github.com/lainio/err2" - "github.com/lainio/err2/assert" "github.com/lainio/err2/try" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -22,19 +20,22 @@ const envPrefix = "FCLI" // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ SilenceUsage: true, - Version: utils.Version, Use: "findy-agent", Short: "Findy agent cli tool", Long: ` Findy agent cli tool `, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - assert.SetDefault(assert.Production) - err2.SetPanicTracer(os.Stderr) - agency.ParseLoggingArgs(rootFlags.logging) + PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { + defer err2.Handle(&err) + + // NOTE! Very important. Adds support for std flag pkg users: glog, err2 + goflag.Parse() + + try.To(goflag.Set("logtostderr", "true")) handleViperFlags(cmd) glog.CopyStandardLogTo("ERROR") // for err2 - aCmd.PreRun() + //aCmd.PreRun() + return nil }, } @@ -105,6 +106,8 @@ func init() { try.To(BindEnvs(rootEnvs, "")) + // NOTE! Very important. Adds support for std flag pkg users: glog, err2 + pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) } func initConfig() { diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..1b1b2221 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "fmt" + + "github.com/findy-network/findy-agent/agent/utils" + "github.com/lainio/err2" + "github.com/lainio/err2/try" + "github.com/spf13/cobra" +) + +var versionDoc = `` + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Prints the version and build information of the CLI tool", + Long: versionDoc, + RunE: func(c *cobra.Command, args []string) (err error) { + defer err2.Handle(&err) + + try.To1(fmt.Println(utils.Version)) + return nil + }, +} + +func init() { + defer err2.Catch(err2.Err(func(err error) { + fmt.Println(err) + })) + + rootCmd.AddCommand(versionCmd) +} From cfdfcac1fa897e355b83f16f821823a92afd6a51 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 27 Oct 2023 12:44:17 +0300 Subject: [PATCH 2/3] err2 v0.9.51 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 33cb6dc6..d47c6f2b 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/hyperledger/aries-framework-go v0.3.2 github.com/hyperledger/aries-framework-go/component/models v0.0.0-20230901120639-e17eddd3ad3e github.com/hyperledger/aries-framework-go/spi v0.0.0-20230901120639-e17eddd3ad3e - github.com/lainio/err2 v0.9.41 + github.com/lainio/err2 v0.9.51 github.com/mr-tron/base58 v1.2.0 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 8421fa88..73884a6d 100644 --- a/go.sum +++ b/go.sum @@ -467,6 +467,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lainio/err2 v0.9.41 h1:aTJOSUvq7WIZJjVkDvefLakW734buyLhM2Bkj8K4AVM= github.com/lainio/err2 v0.9.41/go.mod h1:glTVV2qNFbBy6WzZFDP2G5BqMiZI58cudp588cEgCuM= +github.com/lainio/err2 v0.9.51 h1:R9lzMUxvP0T2U1C5DnpQG0FMMYooqGjUypP6aVmw+eY= +github.com/lainio/err2 v0.9.51/go.mod h1:glTVV2qNFbBy6WzZFDP2G5BqMiZI58cudp588cEgCuM= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= From f80069a5e4e5089b190dbf04abf134d1cc571424 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 27 Oct 2023 16:06:22 +0300 Subject: [PATCH 3/3] fix using FCLI_LOGGING --- cmd/root.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index c7b59703..0b152c3f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( "os" "strings" + "github.com/findy-network/findy-common-go/utils" "github.com/golang/glog" "github.com/lainio/err2" "github.com/lainio/err2/try" @@ -31,6 +32,8 @@ Findy agent cli tool // NOTE! Very important. Adds support for std flag pkg users: glog, err2 goflag.Parse() + // let's support our old logging env + utils.ParseLoggingArgs(rootFlags.logging) try.To(goflag.Set("logtostderr", "true")) handleViperFlags(cmd) glog.CopyStandardLogTo("ERROR") // for err2