From b2fc47ddf47c8feeab2de9fa8c786fbd3b730f07 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 14 Apr 2025 11:15:34 +0100 Subject: [PATCH] remove package level var set gignore client in the context and use it to access the client from subcommands. --- cmd/gignore/context.go | 22 ++++++++++++++++++++++ cmd/gignore/create.go | 7 +++++-- cmd/gignore/list.go | 9 ++++++--- cmd/gignore/main.go | 11 ++++++++--- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 cmd/gignore/context.go diff --git a/cmd/gignore/context.go b/cmd/gignore/context.go new file mode 100644 index 0000000..f230750 --- /dev/null +++ b/cmd/gignore/context.go @@ -0,0 +1,22 @@ +// Package main provides the entry point for the gignore CLI tool, +// including commands like listing available .gitignore templates. +package main + +import ( + "context" + + "github.com/onyx-and-iris/gignore" + log "github.com/sirupsen/logrus" +) + +type contextKey string + +const clientKey contextKey = "client" + +func getClientFromContext(ctx context.Context) *gignore.Client { + client, ok := ctx.Value(clientKey).(*gignore.Client) + if !ok { + log.Fatal("Client not found in context") + } + return client +} diff --git a/cmd/gignore/create.go b/cmd/gignore/create.go index 4561700..5f2830d 100644 --- a/cmd/gignore/create.go +++ b/cmd/gignore/create.go @@ -3,11 +3,13 @@ package main import ( + "context" "fmt" "github.com/spf13/cobra" ) +// createCmd is the command to create a new .gitignore file. var createCmd = &cobra.Command{ Use: "create", Short: "Create a new .gitignore file", @@ -24,7 +26,7 @@ Example: } for _, arg := range args { - err := createTemplate(arg) + err := createTemplate(cmd.Context(), arg) cobra.CheckErr(err) } }, @@ -36,7 +38,8 @@ func init() { } // createTemplate creates a new .gitignore file using the specified template. -func createTemplate(template string) error { +func createTemplate(ctx context.Context, template string) error { + client := getClientFromContext(ctx) err := client.Create(template) if err != nil { return err diff --git a/cmd/gignore/list.go b/cmd/gignore/list.go index 479451a..d309f38 100644 --- a/cmd/gignore/list.go +++ b/cmd/gignore/list.go @@ -3,12 +3,14 @@ package main import ( + "context" "fmt" "strings" "github.com/spf13/cobra" ) +// listCmd is the command to list all .gitignore templates. var listCmd = &cobra.Command{ Use: "list", Short: "List all .gitignore files in the root template repository", @@ -18,8 +20,8 @@ The root template repository can be specified using the --root flag. You can use this command to quickly find all available .gitignore templates. Example: gignore --root= list`, - Run: func(_ *cobra.Command, _ []string) { - err := listTemplates() + Run: func(cmd *cobra.Command, _ []string) { + err := listTemplates(cmd.Context()) cobra.CheckErr(err) }, } @@ -30,7 +32,8 @@ func init() { } // listTemplates retrieves and prints all .gitignore templates available from the gignore client. -func listTemplates() error { +func listTemplates(ctx context.Context) error { + client := getClientFromContext(ctx) templates, err := client.List() if err != nil { return err diff --git a/cmd/gignore/main.go b/cmd/gignore/main.go index 761d10c..9be2b17 100644 --- a/cmd/gignore/main.go +++ b/cmd/gignore/main.go @@ -3,6 +3,7 @@ package main import ( + "context" "os" "github.com/onyx-and-iris/gignore" @@ -10,8 +11,7 @@ import ( "github.com/spf13/cobra" ) -var client *gignore.Client - +// rootCmd is the root command for the gignore CLI tool. var rootCmd = &cobra.Command{ Use: "gignore", Short: "A command line tool to manage .gitignore files", @@ -28,9 +28,14 @@ It supports various programming languages. log.SetLevel(loglevel) // Initialise the gignore client - client = gignore.New( + client := gignore.New( gignore.WithTemplateDirectory(cmd.Flag("root").Value.String()), ) + + // Set the client in the context + // This allows us to access the client in the command handlers + ctx := context.WithValue(context.Background(), clientKey, client) + cmd.SetContext(ctx) }, Run: func(cmd *cobra.Command, _ []string) { cmd.Help()