From 3851b59bbdc6c734ca0f659824d7a3888a1c051b Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 15 Feb 2023 10:33:35 +0100 Subject: [PATCH] Move code for including command name in user agent (#203) --- cmd/root/user_agent.go | 32 ---------------- cmd/root/user_agent_command.go | 37 +++++++++++++++++++ ...ent_test.go => user_agent_command_test.go} | 0 3 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 cmd/root/user_agent_command.go rename cmd/root/{user_agent_test.go => user_agent_command_test.go} (100%) diff --git a/cmd/root/user_agent.go b/cmd/root/user_agent.go index 80d681aa..38069ed6 100644 --- a/cmd/root/user_agent.go +++ b/cmd/root/user_agent.go @@ -1,42 +1,10 @@ package root import ( - "context" - "strings" - "github.com/databricks/bricks/internal/build" "github.com/databricks/databricks-sdk-go/useragent" - "github.com/spf13/cobra" ) -// commandSeparator joins command names in a command hierachy. -// We enforce no command name contains this character. -// See unit test [main.TestCommandsDontUseUnderscoreInName]. -const commandSeparator = "_" - -// commandString walks up the command hierarchy of the specified -// command to build a string representing this hierarchy. -func commandString(cmd *cobra.Command) string { - reversed := []string{cmd.Name()} - cmd.VisitParents(func(p *cobra.Command) { - if !p.HasParent() { - return - } - reversed = append(reversed, p.Name()) - }) - - ordered := make([]string, 0, len(reversed)) - for i := len(reversed) - 1; i >= 0; i-- { - ordered = append(ordered, reversed[i]) - } - - return strings.Join(ordered, commandSeparator) -} - -func withCommandInUserAgent(ctx context.Context, cmd *cobra.Command) context.Context { - return useragent.InContext(ctx, "cmd", commandString(cmd)) -} - func init() { useragent.WithProduct("bricks", build.GetInfo().Version) } diff --git a/cmd/root/user_agent_command.go b/cmd/root/user_agent_command.go new file mode 100644 index 00000000..306f2d7b --- /dev/null +++ b/cmd/root/user_agent_command.go @@ -0,0 +1,37 @@ +package root + +import ( + "context" + "strings" + + "github.com/databricks/databricks-sdk-go/useragent" + "github.com/spf13/cobra" +) + +// commandSeparator joins command names in a command hierachy. +// We enforce no command name contains this character. +// See unit test [main.TestCommandsDontUseUnderscoreInName]. +const commandSeparator = "_" + +// commandString walks up the command hierarchy of the specified +// command to build a string representing this hierarchy. +func commandString(cmd *cobra.Command) string { + reversed := []string{cmd.Name()} + cmd.VisitParents(func(p *cobra.Command) { + if !p.HasParent() { + return + } + reversed = append(reversed, p.Name()) + }) + + ordered := make([]string, 0, len(reversed)) + for i := len(reversed) - 1; i >= 0; i-- { + ordered = append(ordered, reversed[i]) + } + + return strings.Join(ordered, commandSeparator) +} + +func withCommandInUserAgent(ctx context.Context, cmd *cobra.Command) context.Context { + return useragent.InContext(ctx, "cmd", commandString(cmd)) +} diff --git a/cmd/root/user_agent_test.go b/cmd/root/user_agent_command_test.go similarity index 100% rename from cmd/root/user_agent_test.go rename to cmd/root/user_agent_command_test.go