mirror of
https://github.com/onyx-and-iris/gignore.git
synced 2025-04-16 02:23:53 +01:00
migrate to cobra
This commit is contained in:
parent
2c1d2ed99c
commit
23ec3b85c1
43
cmd/gignore/create.go
Normal file
43
cmd/gignore/create.go
Normal file
@ -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)
|
||||||
|
}
|
47
cmd/gignore/list.go
Normal file
47
cmd/gignore/list.go
Normal file
@ -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=<path> 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
|
||||||
|
}
|
@ -1,93 +1,50 @@
|
|||||||
// Package main provides the entry point for the gignore command-line tool,
|
// Package main provides the entry point for the gignore CLI tool,
|
||||||
// which generates .gitignore files based on specified templates.
|
// including commands like listing available .gitignore templates.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
func exit(err error) {
|
var client *gignore.Client
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
|
||||||
os.Exit(1)
|
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() {
|
func main() {
|
||||||
flag.Usage = func() {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
w := flag.CommandLine.Output()
|
panic(err)
|
||||||
|
|
||||||
fmt.Fprint(w, "Usage of gignore:\n")
|
|
||||||
fmt.Fprint(w, " gignore [flags] <template>\n")
|
|
||||||
fmt.Fprint(w, "\n")
|
|
||||||
|
|
||||||
fmt.Fprint(w, "Flags:\n")
|
|
||||||
flag.PrintDefaults()
|
|
||||||
|
|
||||||
fmt.Fprint(w, "\n")
|
|
||||||
fmt.Fprint(w, "Example:\n")
|
|
||||||
fmt.Fprint(w, " gignore go\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
list bool
|
|
||||||
templateDir string
|
|
||||||
loglevel string
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.BoolVar(&list, "list", false, "list available templates")
|
|
||||||
flag.BoolVar(&list, "ls", false, "list available templates (shorthand)")
|
|
||||||
flag.StringVar(
|
|
||||||
&templateDir,
|
|
||||||
"dir",
|
|
||||||
getEnv("GIGNORE_TEMPLATE_DIR", "gitignoreio"),
|
|
||||||
"directory containing .gitignore templates",
|
|
||||||
)
|
|
||||||
flag.StringVar(&loglevel, "loglevel", "warn", "log level")
|
|
||||||
flag.StringVar(&loglevel, "l", "warn", "log level (shorthand)")
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
level, err := log.ParseLevel(loglevel)
|
|
||||||
if err != nil {
|
|
||||||
exit(fmt.Errorf("invalid log level: %s", loglevel))
|
|
||||||
}
|
|
||||||
log.SetLevel(level)
|
|
||||||
|
|
||||||
client := gignore.New(gignore.WithTemplateDirectory(templateDir))
|
|
||||||
|
|
||||||
if list {
|
|
||||||
if err := listTemplates(client); err != nil {
|
|
||||||
exit(fmt.Errorf("failed to list templates: %v", err))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
args := flag.Args()
|
|
||||||
if len(args) == 0 {
|
|
||||||
flag.Usage()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, arg := range args {
|
|
||||||
err := client.Create(arg)
|
|
||||||
if err != nil {
|
|
||||||
exit(fmt.Errorf("failed to create .gitignore file: %v", err))
|
|
||||||
}
|
|
||||||
fmt.Printf("√ created %s .gitignore file\n", arg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listTemplates(client *gignore.Client) error {
|
|
||||||
templates, err := client.List()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, template := range templates {
|
|
||||||
fmt.Println(template)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -10,6 +10,9 @@ import (
|
|||||||
|
|
||||||
//go:generate go run cmd/gen/main.go
|
//go:generate go run cmd/gen/main.go
|
||||||
|
|
||||||
|
// DefaultTemplateDirectory is the default directory for .gitignore templates.
|
||||||
|
const DefaultTemplateDirectory = "gitignoreio"
|
||||||
|
|
||||||
// Client is a client for managing .gitignore templates.
|
// Client is a client for managing .gitignore templates.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
registry *registry.TemplateRegistry
|
registry *registry.TemplateRegistry
|
||||||
@ -43,11 +46,11 @@ func (c *Client) Create(template string) error {
|
|||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
templateNotFoundErr := &templateNotFoundError{template, []string{c.registry.Directory}}
|
templateNotFoundErr := &templateNotFoundError{template, []string{c.registry.Directory}}
|
||||||
if c.registry.Directory == "gitignoreio" {
|
if c.registry.Directory == DefaultTemplateDirectory {
|
||||||
return templateNotFoundErr
|
return templateNotFoundErr
|
||||||
}
|
}
|
||||||
|
|
||||||
c.registry.Directory = "gitignoreio"
|
c.registry.Directory = DefaultTemplateDirectory
|
||||||
ok, err = c.registry.Contains(template)
|
ok, err = c.registry.Contains(template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user