remove package level var

set gignore client in the context and use it to access the client from subcommands.
This commit is contained in:
onyx-and-iris 2025-04-14 11:15:34 +01:00
parent f5f89d8515
commit b2fc47ddf4
4 changed files with 41 additions and 8 deletions

22
cmd/gignore/context.go Normal file
View File

@ -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
}

View File

@ -3,11 +3,13 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// createCmd is the command to create a new .gitignore file.
var createCmd = &cobra.Command{ var createCmd = &cobra.Command{
Use: "create", Use: "create",
Short: "Create a new .gitignore file", Short: "Create a new .gitignore file",
@ -24,7 +26,7 @@ Example:
} }
for _, arg := range args { for _, arg := range args {
err := createTemplate(arg) err := createTemplate(cmd.Context(), arg)
cobra.CheckErr(err) cobra.CheckErr(err)
} }
}, },
@ -36,7 +38,8 @@ func init() {
} }
// createTemplate creates a new .gitignore file using the specified template. // 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) err := client.Create(template)
if err != nil { if err != nil {
return err return err

View File

@ -3,12 +3,14 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// listCmd is the command to list all .gitignore templates.
var listCmd = &cobra.Command{ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List all .gitignore files in the root template repository", 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. You can use this command to quickly find all available .gitignore templates.
Example: Example:
gignore --root=<path> list`, gignore --root=<path> list`,
Run: func(_ *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
err := listTemplates() err := listTemplates(cmd.Context())
cobra.CheckErr(err) cobra.CheckErr(err)
}, },
} }
@ -30,7 +32,8 @@ func init() {
} }
// listTemplates retrieves and prints all .gitignore templates available from the gignore client. // 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() templates, err := client.List()
if err != nil { if err != nil {
return err return err

View File

@ -3,6 +3,7 @@
package main package main
import ( import (
"context"
"os" "os"
"github.com/onyx-and-iris/gignore" "github.com/onyx-and-iris/gignore"
@ -10,8 +11,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var client *gignore.Client // rootCmd is the root command for the gignore CLI tool.
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "gignore", Use: "gignore",
Short: "A command line tool to manage .gitignore files", Short: "A command line tool to manage .gitignore files",
@ -28,9 +28,14 @@ It supports various programming languages.
log.SetLevel(loglevel) log.SetLevel(loglevel)
// Initialise the gignore client // Initialise the gignore client
client = gignore.New( client := gignore.New(
gignore.WithTemplateDirectory(cmd.Flag("root").Value.String()), 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) { Run: func(cmd *cobra.Command, _ []string) {
cmd.Help() cmd.Help()