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

View File

@ -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=<path> 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

View File

@ -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()