Compare commits

..

6 Commits
v0.5.0 ... main

Author SHA1 Message Date
b2fc47ddf4 remove package level var
set gignore client in the context and use it to access the client from subcommands.
2025-04-14 11:15:34 +01:00
f5f89d8515 upd docstring 2025-04-14 09:03:32 +01:00
7a964a031c return err from createTemplate and check it 2025-04-13 17:41:18 +01:00
00641cdc85 move getEnv into main.go 2025-04-13 17:03:57 +01:00
478a172588 upd usage in readme 2025-04-13 14:49:18 +01:00
6adea84322 no need to pass client around 2025-04-13 14:35:43 +01:00
6 changed files with 63 additions and 28 deletions

View File

@ -31,11 +31,11 @@ Available Commands:
completion Generate the autocompletion script for the specified shell completion Generate the autocompletion script for the specified shell
create Create a new .gitignore file create Create a new .gitignore file
help Help about any command help Help about any command
list List all .gitignore files in the current directory list List all .gitignore files in the root template repository
Flags: Flags:
-h, --help help for gignore -h, --help help for gignore
-l, --loglevel string Log level (trace, debug, info, warn, error, fatal, panic) (default "info") -l, --loglevel string Log level (trace, debug, info, warn, error, fatal, panic) (default "warn")
-r, --root string Root directory to search for .gitignore files (default "gitignoreio") -r, --root string Root directory to search for .gitignore files (default "gitignoreio")
Use "gignore [command] --help" for more information about a command. Use "gignore [command] --help" for more information about a command.

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,12 +3,13 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/onyx-and-iris/gignore"
"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",
@ -25,7 +26,8 @@ Example:
} }
for _, arg := range args { for _, arg := range args {
createTemplate(client, arg) err := createTemplate(cmd.Context(), arg)
cobra.CheckErr(err)
} }
}, },
} }
@ -36,8 +38,13 @@ func init() {
} }
// createTemplate creates a new .gitignore file using the specified template. // createTemplate creates a new .gitignore file using the specified template.
func createTemplate(client *gignore.Client, template string) { func createTemplate(ctx context.Context, template string) error {
client := getClientFromContext(ctx)
err := client.Create(template) err := client.Create(template)
cobra.CheckErr(err) if err != nil {
return err
}
fmt.Printf("√ created %s .gitignore file\n", template) fmt.Printf("√ created %s .gitignore file\n", template)
return nil
} }

View File

@ -3,13 +3,14 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"github.com/onyx-and-iris/gignore"
"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",
@ -19,10 +20,9 @@ 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) {
if err := listTemplates(client); err != nil { err := listTemplates(cmd.Context())
cobra.CheckErr(err) cobra.CheckErr(err)
}
}, },
} }
@ -32,16 +32,18 @@ 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.
// It takes a gignore.Client as a parameter and returns an error if the operation fails. func listTemplates(ctx context.Context) error {
func listTemplates(client *gignore.Client) error { client := getClientFromContext(ctx)
templates, err := client.List() templates, err := client.List()
if err != nil { if err != nil {
return err return err
} }
var output strings.Builder var output strings.Builder
for _, template := range templates { for _, template := range templates {
output.WriteString(template + "\n") output.WriteString(template + "\n")
} }
fmt.Print(output.String()) fmt.Print(output.String())
return nil return nil
} }

View File

@ -3,13 +3,15 @@
package main package main
import ( import (
"context"
"os"
"github.com/onyx-and-iris/gignore" "github.com/onyx-and-iris/gignore"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"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",
@ -26,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()
@ -37,6 +44,14 @@ It supports various programming languages.
// init initialises the root command and adds global flags. // init initialises the root command and adds global flags.
func init() { func init() {
getEnv := func(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
rootCmd.PersistentFlags(). rootCmd.PersistentFlags().
StringP("root", "r", getEnv("GIGNORE_TEMPLATE_ROOT", gignore.DefaultTemplateDirectory), "Root directory to search for .gitignore files") StringP("root", "r", getEnv("GIGNORE_TEMPLATE_ROOT", gignore.DefaultTemplateDirectory), "Root directory to search for .gitignore files")
rootCmd.PersistentFlags(). rootCmd.PersistentFlags().
@ -45,6 +60,6 @@ func init() {
func main() { func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
panic(err) log.Fatal(err)
} }
} }

View File

@ -1,11 +0,0 @@
package main
import "os"
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}