Compare commits

..

No commits in common. "a6fb3c69c7eeef96db84aaefce6df47d9a7f4f99" and "5b389aa9dc253d18fc1b8f5b5df4be6e2f1ae285" have entirely different histories.

7 changed files with 25 additions and 39 deletions

4
.gitignore vendored
View File

@ -1,4 +1,4 @@
# Generated by ignr: github.com/onyx-and-iris/ignr # Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli
## Go ## ## Go ##
# If you prefer the allow list template instead of the deny list, see community template: # If you prefer the allow list template instead of the deny list, see community template:
@ -36,4 +36,4 @@ go.work.sum
# .idea/ # .idea/
# .vscode/ # .vscode/
# End of ignr # End of ignr-cli

View File

@ -20,10 +20,9 @@ go install github.com/onyx-and-iris/ignr@latest
- --token/-t: GitHub authentication token - --token/-t: GitHub authentication token
- note, this tool can be used **without** authenticating but rate limiting will be stricter. - note, this tool can be used **without** authenticating but rate limiting will be stricter.
- --height/-H: Height of the selection prompt (default 10) - --height/-H: Height of the selection prompt (default 20)
- --filter/-f: Type of filter to apply to the list of templates (default startswith) - --filter/-f: Type of filter to apply to the list of templates (default startswith)
- may be one of (startswith, contains) - may be one of (startswith, contains)
- --start-search/-s: Start the prompt in search mode (default false)
*environment variables* *environment variables*
@ -31,9 +30,8 @@ go install github.com/onyx-and-iris/ignr@latest
#!/usr/bin/env bash #!/usr/bin/env bash
export IGNR_TOKEN=<API Token> export IGNR_TOKEN=<API Token>
export IGNR_HEIGHT=10 export IGNR_HEIGHT=20
export IGNR_FILTER=startswith export IGNR_FILTER=startswith
export IGNR_START_SEARCH=false
``` ```
## Commands ## Commands
@ -46,7 +44,7 @@ Trigger the selection prompt.
ignr new ignr new
``` ```
Search mode can be activated by pressing `/`: The prompt filter can be activated by pressing `/`:
![Prompt Filter](./img/promptfilter.png) ![Prompt Filter](./img/promptfilter.png)

View File

@ -2,11 +2,13 @@ package main
import ( import (
"strings" "strings"
"github.com/spf13/viper"
) )
// filterFunc returns a function that filters templates based on the specified filter type. // filterFunc returns a function that filters templates based on the specified filter type.
func filterFunc(templates []string, filterType string) func(input string, index int) bool { func filterFunc(templates []string) func(input string, index int) bool {
switch filterType { switch viper.GetString("filter") {
case "contains": case "contains":
return func(input string, index int) bool { return func(input string, index int) bool {
return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input)) return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input))

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/onyx-and-iris/ignr module github.com/onyx-and-iris/ignr-cli
go 1.24.3 go 1.24.3

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 11 KiB

11
main.go
View File

@ -18,9 +18,9 @@ var version string // Version of the CLI, set during build time
// rootCmd represents the base command when called without any subcommands. // rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "ignr", Use: "ignr-cli",
Short: "A command-line interface for generating .gitignore files", Short: "A command-line interface for generating .gitignore files",
Long: `ignr is a command-line interface for generating .gitignore files. Long: `ignr-cli is a command-line interface for generating .gitignore files.
It allows users to easily create and manage .gitignore files for various programming languages and frameworks. It allows users to easily create and manage .gitignore files for various programming languages and frameworks.
You may also list available templates and generate .gitignore files based on those templates.`, You may also list available templates and generate .gitignore files based on those templates.`,
SilenceUsage: true, SilenceUsage: true,
@ -43,7 +43,7 @@ You may also list available templates and generate .gitignore files based on tho
} }
version = strings.Split(info.Main.Version, "-")[0] version = strings.Split(info.Main.Version, "-")[0]
} }
fmt.Printf("ignr version: %s\n", version) fmt.Printf("ignr-cli version: %s\n", version)
return nil return nil
} }
@ -54,20 +54,17 @@ You may also list available templates and generate .gitignore files based on tho
// init initialises the root command and its flags. // init initialises the root command and its flags.
func init() { func init() {
rootCmd.PersistentFlags().StringP("token", "t", "", "GitHub authentication token") rootCmd.PersistentFlags().StringP("token", "t", "", "GitHub authentication token")
rootCmd.PersistentFlags().IntP("height", "H", 10, "Height of the selection prompt") rootCmd.PersistentFlags().IntP("height", "H", 20, "Height of the selection prompt")
rootCmd.PersistentFlags(). rootCmd.PersistentFlags().
StringP("filter", "f", "startswith", "Type of filter to apply to the list of templates (e.g., 'startswith', 'contains')") StringP("filter", "f", "startswith", "Type of filter to apply to the list of templates (e.g., 'startswith', 'contains')")
rootCmd.PersistentFlags().BoolP("start-search", "s", false, "Start the prompt in search mode")
rootCmd.Flags().BoolP("version", "v", false, "Print the version of the CLI") rootCmd.Flags().BoolP("version", "v", false, "Print the version of the CLI")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetEnvPrefix("IGNR") viper.SetEnvPrefix("IGNR")
viper.AutomaticEnv() viper.AutomaticEnv()
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")) viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height")) viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height"))
viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter")) viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter"))
viper.BindPFlag("start-search", rootCmd.PersistentFlags().Lookup("start-search"))
} }
// main is the entry point of the application. // main is the entry point of the application.

27
new.go
View File

@ -29,21 +29,11 @@ func init() {
rootCmd.AddCommand(newCmd) rootCmd.AddCommand(newCmd)
} }
type promptConfig struct {
Height int
StartSearch bool
FilterType string
}
// runNewCommand is the handler for the 'new' command. // runNewCommand is the handler for the 'new' command.
// It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file. // It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file.
func runNewCommand(cmd *cobra.Command, _ []string) error { func runNewCommand(cmd *cobra.Command, _ []string) error {
pc := promptConfig{ height := viper.GetInt("height")
Height: viper.GetInt("height"), if height <= 0 {
StartSearch: viper.GetBool("start-search"),
FilterType: viper.GetString("filter"),
}
if pc.Height <= 0 {
return errors.New("height must be a positive integer") return errors.New("height must be a positive integer")
} }
@ -52,7 +42,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
return errors.New("failed to get GitHub client from context") return errors.New("failed to get GitHub client from context")
} }
content, err := runPrompt(client, &pc) content, err := runPrompt(client, height)
if err != nil { if err != nil {
return fmt.Errorf("error running selection prompt: %w", err) return fmt.Errorf("error running selection prompt: %w", err)
} }
@ -71,7 +61,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
} }
// runPrompt is a helper function to run the selection prompt for .gitignore templates. // runPrompt is a helper function to run the selection prompt for .gitignore templates.
func runPrompt(client *github.Client, pc *promptConfig) (*github.Gitignore, error) { func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
templates, _, err := client.Gitignores.List(context.Background()) templates, _, err := client.Gitignores.List(context.Background())
if err != nil { if err != nil {
return nil, fmt.Errorf("error retrieving gitignore template list: %w", err) return nil, fmt.Errorf("error retrieving gitignore template list: %w", err)
@ -88,9 +78,8 @@ func runPrompt(client *github.Client, pc *promptConfig) (*github.Gitignore, erro
Label: "Select a .gitignore template", Label: "Select a .gitignore template",
Items: templates, Items: templates,
Templates: selectTemplates, Templates: selectTemplates,
Size: pc.Height, Size: height,
Searcher: filterFunc(templates, pc.FilterType), Searcher: filterFunc(templates),
StartInSearchMode: pc.StartSearch,
} }
i, _, err := prompt.Run() i, _, err := prompt.Run()
@ -108,7 +97,7 @@ func runPrompt(client *github.Client, pc *promptConfig) (*github.Gitignore, erro
// commitGitignore writes the content of the selected gitignore template to the .gitignore file. // commitGitignore writes the content of the selected gitignore template to the .gitignore file.
func commitGitignore(content *github.Gitignore, w io.Writer) error { func commitGitignore(content *github.Gitignore, w io.Writer) error {
if _, err := fmt.Fprintf(w, "# Generated by ignr: github.com/onyx-and-iris/ignr\n\n## %s ##\n", content.GetName()); err != nil { if _, err := fmt.Fprintf(w, "# Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli\n\n## %s ##\n", content.GetName()); err != nil {
return fmt.Errorf("error writing header to file '%s': %w", gitignoreFileName, err) return fmt.Errorf("error writing header to file '%s': %w", gitignoreFileName, err)
} }
@ -116,7 +105,7 @@ func commitGitignore(content *github.Gitignore, w io.Writer) error {
return fmt.Errorf("error writing to file '%s': %w", gitignoreFileName, err) return fmt.Errorf("error writing to file '%s': %w", gitignoreFileName, err)
} }
if _, err := fmt.Fprintf(w, "\n# End of ignr\n"); err != nil { if _, err := fmt.Fprintf(w, "\n# End of ignr-cli\n"); err != nil {
return fmt.Errorf("error writing footer to file '%s': %w", gitignoreFileName, err) return fmt.Errorf("error writing footer to file '%s': %w", gitignoreFileName, err)
} }