From 23ec3b85c14615ef2417734a9f51d23caa06ad46 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 13 Apr 2025 14:23:48 +0100 Subject: [PATCH] migrate to cobra --- cmd/gignore/create.go | 43 +++++++++++++++ cmd/gignore/list.go | 47 +++++++++++++++++ cmd/gignore/main.go | 119 ++++++++++++++---------------------------- gignore.go | 7 ++- 4 files changed, 133 insertions(+), 83 deletions(-) create mode 100644 cmd/gignore/create.go create mode 100644 cmd/gignore/list.go diff --git a/cmd/gignore/create.go b/cmd/gignore/create.go new file mode 100644 index 0000000..03233da --- /dev/null +++ b/cmd/gignore/create.go @@ -0,0 +1,43 @@ +// Package main provides the entry point for the gignore CLI tool, +// including commands like listing available .gitignore templates. +package main + +import ( + "fmt" + + "github.com/onyx-and-iris/gignore" + "github.com/spf13/cobra" +) + +var createCmd = &cobra.Command{ + Use: "create", + Short: "Create a new .gitignore file", + Long: `Create a new .gitignore file in the current directory. +At least one template must be specified. +Multiple templates can be specified, and they will be combined into a single .gitignore file. +Example: + gignore create python + gignore create python go`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + cmd.Help() + return + } + + for _, arg := range args { + createTemplate(client, arg) + } + }, +} + +// init initialises the create command and adds it to the root command. +func init() { + rootCmd.AddCommand(createCmd) +} + +// createTemplate creates a new .gitignore file using the specified template. +func createTemplate(client *gignore.Client, template string) { + err := client.Create(template) + cobra.CheckErr(err) + fmt.Printf("√ created %s .gitignore file\n", template) +} diff --git a/cmd/gignore/list.go b/cmd/gignore/list.go new file mode 100644 index 0000000..9c073d9 --- /dev/null +++ b/cmd/gignore/list.go @@ -0,0 +1,47 @@ +// Package main provides the entry point for the gignore CLI tool, +// including commands like listing available .gitignore templates. +package main + +import ( + "fmt" + "strings" + + "github.com/onyx-and-iris/gignore" + "github.com/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "List all .gitignore files in the root template repository", + Long: `List all .gitignore files in the root template repository. +This command will search the root template repository for .gitignore files and print their paths to the console. +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) { + if err := listTemplates(client); err != nil { + cobra.CheckErr(err) + } + }, +} + +// init initialises the list command and adds it to the root command. +func init() { + rootCmd.AddCommand(listCmd) +} + +// 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(client *gignore.Client) error { + templates, err := client.List() + if err != nil { + return err + } + var output strings.Builder + for _, template := range templates { + output.WriteString(template + "\n") + } + fmt.Print(output.String()) + return nil +} diff --git a/cmd/gignore/main.go b/cmd/gignore/main.go index 8d4a752..c02ad82 100644 --- a/cmd/gignore/main.go +++ b/cmd/gignore/main.go @@ -1,93 +1,50 @@ -// Package main provides the entry point for the gignore command-line tool, -// which generates .gitignore files based on specified templates. +// Package main provides the entry point for the gignore CLI tool, +// including commands like listing available .gitignore templates. package main import ( - "flag" - "fmt" - "os" - "github.com/onyx-and-iris/gignore" log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) -func exit(err error) { - _, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err) - os.Exit(1) +var client *gignore.Client + +var rootCmd = &cobra.Command{ + Use: "gignore", + Short: "A command line tool to manage .gitignore files", + Long: `gignore is a command line tool that helps you manage your .gitignore files. +You can use it to list available templates and create new .gitignore files. +It supports various programming languages. + Example: + gignore list + gignore create python`, + PersistentPreRun: func(cmd *cobra.Command, _ []string) { + // Initialise the logger + loglevel, err := log.ParseLevel(cmd.Flag("loglevel").Value.String()) + cobra.CheckErr(err) + log.SetLevel(loglevel) + + // Initialise the gignore client + client = gignore.New( + gignore.WithTemplateDirectory(cmd.Flag("root").Value.String()), + ) + }, + Run: func(cmd *cobra.Command, _ []string) { + cmd.Help() + }, +} + +// init initialises the root command and adds global flags. +func init() { + rootCmd.PersistentFlags(). + StringP("root", "r", getEnv("GIGNORE_TEMPLATE_ROOT", gignore.DefaultTemplateDirectory), "Root directory to search for .gitignore files") + rootCmd.PersistentFlags(). + StringP("loglevel", "l", getEnv("GIGNORE_LOGLEVEL", "warn"), "Log level (trace, debug, info, warn, error, fatal, panic)") } func main() { - flag.Usage = func() { - w := flag.CommandLine.Output() - - fmt.Fprint(w, "Usage of gignore:\n") - fmt.Fprint(w, " gignore [flags]