diff --git a/src/cli/cli.go b/src/cli/cli.go index f06354f0b..b67663d06 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -15,6 +15,7 @@ import ( "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/cli/repo" "github.com/alcionai/corso/src/cli/restore" + "github.com/alcionai/corso/src/cli/utils" "github.com/alcionai/corso/src/internal/observe" "github.com/alcionai/corso/src/internal/version" "github.com/alcionai/corso/src/pkg/logger" @@ -31,7 +32,27 @@ var corsoCmd = &cobra.Command{ Short: "Free, Secure, Open-Source Backup for M365.", Long: `Free, Secure, and Open-Source Backup for Microsoft 365.`, RunE: handleCorsoCmd, - PersistentPreRunE: config.InitFunc(), + PersistentPreRunE: preRun, +} + +func preRun(cc *cobra.Command, args []string) error { + if err := config.InitFunc(cc, args); err != nil { + return err + } + + log := logger.Ctx(cc.Context()) + + flags := utils.GetPopulatedFlags(cc) + flagSl := make([]string, 0, len(flags)) + + // currently only tracking flag names to avoid pii leakage. + for f := range flags { + flagSl = append(flagSl, f) + } + + log.Infow("cli command", "command", cc.CommandPath(), "flags", flagSl, "version", version.CurrentVersion()) + + return nil } // Handler for flat calls to `corso`. @@ -39,7 +60,7 @@ var corsoCmd = &cobra.Command{ func handleCorsoCmd(cmd *cobra.Command, args []string) error { v, _ := cmd.Flags().GetBool("version") if v { - print.Outf(cmd.Context(), "Corso version: "+version.Version) + print.Outf(cmd.Context(), "Corso version: "+version.CurrentVersion()) return nil } @@ -62,7 +83,7 @@ func BuildCommandTree(cmd *cobra.Command) { cmd.PersistentFlags().SortFlags = false cmd.Flags().BoolP("version", "v", false, "current version info") - cmd.PersistentPostRunE = config.InitFunc() + cmd.PersistentPreRunE = preRun config.AddConfigFlags(cmd) logger.AddLoggingFlags(cmd) observe.AddProgressBarFlags(cmd) @@ -85,6 +106,7 @@ func BuildCommandTree(cmd *cobra.Command) { // Handle builds and executes the cli processor. func Handle() { + //nolint:forbidigo ctx := config.Seed(context.Background()) ctx = print.SetRootCmd(ctx, corsoCmd) observe.SeedWriter(ctx, print.StderrWriter(ctx), observe.PreloadFlags()) diff --git a/src/cli/config/config.go b/src/cli/config/config.go index dbcd21422..8f532abb6 100644 --- a/src/cli/config/config.go +++ b/src/cli/config/config.go @@ -77,20 +77,18 @@ func AddConfigFlags(cmd *cobra.Command) { // InitFunc provides a func that lazily initializes viper and // verifies that the configuration was able to read a file. -func InitFunc() func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - fp := configFilePathFlag - if len(fp) == 0 || fp == displayDefaultFP { - fp = configFilePath - } - - err := initWithViper(GetViper(cmd.Context()), fp) - if err != nil { - return err - } - - return Read(cmd.Context()) +func InitFunc(cmd *cobra.Command, args []string) error { + fp := configFilePathFlag + if len(fp) == 0 || fp == displayDefaultFP { + fp = configFilePath } + + err := initWithViper(GetViper(cmd.Context()), fp) + if err != nil { + return err + } + + return Read(cmd.Context()) } // initWithViper implements InitConfig, but takes in a viper diff --git a/src/go.mod b/src/go.mod index 1b05ec06b..a8054ab0e 100644 --- a/src/go.mod +++ b/src/go.mod @@ -13,6 +13,7 @@ require ( github.com/microsoft/kiota-abstractions-go v0.16.0 github.com/microsoft/kiota-authentication-azure-go v0.6.0 github.com/microsoft/kiota-http-go v0.13.0 + github.com/microsoft/kiota-serialization-form-go v0.2.0 github.com/microsoft/kiota-serialization-json-go v0.7.2 github.com/microsoftgraph/msgraph-sdk-go v0.53.0 github.com/microsoftgraph/msgraph-sdk-go-core v0.33.0 @@ -40,7 +41,6 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/magiconair/properties v1.8.7 // indirect - github.com/microsoft/kiota-serialization-form-go v0.2.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/spf13/afero v1.9.3 // indirect @@ -84,7 +84,7 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect - github.com/microsoft/kiota-serialization-text-go v0.6.0 // indirect + github.com/microsoft/kiota-serialization-text-go v0.6.0 github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/minio-go/v7 v7.0.45 // indirect github.com/minio/sha256-simd v1.0.0 // indirect diff --git a/src/internal/version/version.go b/src/internal/version/version.go index 4a07d07ba..6b1859a80 100644 --- a/src/internal/version/version.go +++ b/src/internal/version/version.go @@ -1,3 +1,29 @@ package version +import ( + "os/exec" + "strings" +) + var Version = "dev" + +func CurrentVersion() string { + if len(Version) == 0 || Version == "dev" { + c, b := exec.Command("git", "describe", "--tag"), new(strings.Builder) + c.Stdout = b + + if err := c.Run(); err != nil { + return "dev" + } + + s := strings.TrimRight(b.String(), "\n") + + if len(s) != 0 { + return "dev-" + s + } + + return "dev" + } + + return Version +} diff --git a/src/pkg/logger/logger.go b/src/pkg/logger/logger.go index f64febe2e..0d5ffe250 100644 --- a/src/pkg/logger/logger.go +++ b/src/pkg/logger/logger.go @@ -6,6 +6,7 @@ import ( "path/filepath" "time" + "github.com/alcionai/clues" "github.com/spf13/cobra" "github.com/spf13/pflag" "go.uber.org/zap" @@ -264,7 +265,7 @@ func Ctx(ctx context.Context) *zap.SugaredLogger { return singleton(levelOf(llFlag), defaultLogLocation()) } - return l.(*zap.SugaredLogger) + return l.(*zap.SugaredLogger).With(clues.Slice(ctx)...) } // transforms the llevel flag value to a logLevel enum