mirror of
https://github.com/onyx-and-iris/ignr.git
synced 2026-04-18 07:13:33 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf93198462 | |||
| e1875fb894 | |||
| e1b879f54c | |||
| f51302a945 | |||
| 11fe797f3f | |||
| 5b389aa9dc | |||
| bb535f296d | |||
| 5eccfc939c | |||
| 00638a6bf4 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
# Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli
|
||||
# Generated by ignr: github.com/onyx-and-iris/ignr
|
||||
|
||||
## Go ##
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
@@ -36,4 +36,4 @@ go.work.sum
|
||||
# .idea/
|
||||
# .vscode/
|
||||
|
||||
# End of ignr-cli
|
||||
# End of ignr
|
||||
|
||||
15
README.md
15
README.md
@@ -2,7 +2,7 @@
|
||||

|
||||

|
||||
|
||||
# ignr-cli
|
||||
# ignr
|
||||
|
||||
Simple no-frills .gitignore generator backed by the Github API.
|
||||
|
||||
@@ -11,7 +11,7 @@ Simple no-frills .gitignore generator backed by the Github API.
|
||||
## Install
|
||||
|
||||
```console
|
||||
go install github.com/onyx-and-iris/ignr-cli@latest
|
||||
go install github.com/onyx-and-iris/ignr@latest
|
||||
```
|
||||
|
||||
## Configuration
|
||||
@@ -20,9 +20,10 @@ go install github.com/onyx-and-iris/ignr-cli@latest
|
||||
|
||||
- --token/-t: GitHub authentication token
|
||||
- note, this tool can be used **without** authenticating but rate limiting will be stricter.
|
||||
- --height/-H: Height of the selection prompt (default 20)
|
||||
- --height/-H: Height of the selection prompt (default 10)
|
||||
- --filter/-f: Type of filter to apply to the list of templates (default startswith)
|
||||
- may be one of (startswith, contains)
|
||||
- --start-search/-s: Start the prompt in search mode (default false)
|
||||
|
||||
*environment variables*
|
||||
|
||||
@@ -30,8 +31,9 @@ go install github.com/onyx-and-iris/ignr-cli@latest
|
||||
#!/usr/bin/env bash
|
||||
|
||||
export IGNR_TOKEN=<API Token>
|
||||
export IGNR_HEIGHT=20
|
||||
export IGNR_HEIGHT=10
|
||||
export IGNR_FILTER=startswith
|
||||
export IGNR_START_SEARCH=false
|
||||
```
|
||||
|
||||
## Commands
|
||||
@@ -41,7 +43,7 @@ export IGNR_FILTER=startswith
|
||||
Trigger the selection prompt.
|
||||
|
||||
```console
|
||||
ignr-cli new
|
||||
ignr new
|
||||
```
|
||||
|
||||
The prompt filter can be activated by pressing `/`:
|
||||
@@ -50,4 +52,5 @@ The prompt filter can be activated by pressing `/`:
|
||||
|
||||
## Special Thanks
|
||||
|
||||
- [Charm](https://github.com/charmbracelet) for their awesome CLI packages.
|
||||
- [spf13](https://github.com/spf13) for the [cobra](https://github.com/spf13/cobra) and [viper](https://github.com/spf13/viper) packages.
|
||||
- [Manifold](https://github.com/manifoldco) for the [promptui](https://github.com/manifoldco/promptui) package.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
PROGRAM: ignr-cli
|
||||
PROGRAM: ignr
|
||||
SHELL: '{{if eq .OS "Windows_NT"}}powershell{{end}}'
|
||||
BIN_DIR: bin
|
||||
VERSION:
|
||||
@@ -12,12 +12,12 @@ vars:
|
||||
|
||||
tasks:
|
||||
default:
|
||||
desc: Build the ignr-cli project
|
||||
desc: Build the ignr project
|
||||
cmds:
|
||||
- task: build
|
||||
|
||||
build:
|
||||
desc: Build the ignr-cli project
|
||||
desc: Build the ignr project
|
||||
deps: [vet]
|
||||
cmds:
|
||||
- task: build-windows
|
||||
@@ -35,13 +35,13 @@ tasks:
|
||||
- go fmt ./...
|
||||
|
||||
build-windows:
|
||||
desc: Build the ignr-cli project for Windows
|
||||
desc: Build the ignr project for Windows
|
||||
cmds:
|
||||
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}"
|
||||
internal: true
|
||||
|
||||
build-linux:
|
||||
desc: Build the ignr-cli project for Linux
|
||||
desc: Build the ignr project for Linux
|
||||
cmds:
|
||||
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}"
|
||||
internal: true
|
||||
|
||||
@@ -2,13 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// filterFunc returns a function that filters templates based on the specified filter type.
|
||||
func filterFunc(templates []string) func(input string, index int) bool {
|
||||
switch viper.GetString("filter") {
|
||||
func filterFunc(templates []string, filterType string) func(input string, index int) bool {
|
||||
switch filterType {
|
||||
case "contains":
|
||||
return func(input string, index int) bool {
|
||||
return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input))
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
||||
module github.com/onyx-and-iris/ignr-cli
|
||||
module github.com/onyx-and-iris/ignr
|
||||
|
||||
go 1.24.3
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 10 KiB |
11
main.go
11
main.go
@@ -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.
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "ignr-cli",
|
||||
Use: "ignr",
|
||||
Short: "A command-line interface for generating .gitignore files",
|
||||
Long: `ignr-cli is a command-line interface for generating .gitignore files.
|
||||
Long: `ignr 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.
|
||||
You may also list available templates and generate .gitignore files based on those templates.`,
|
||||
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]
|
||||
}
|
||||
fmt.Printf("ignr-cli version: %s\n", version)
|
||||
fmt.Printf("ignr version: %s\n", version)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -54,17 +54,20 @@ You may also list available templates and generate .gitignore files based on tho
|
||||
// init initialises the root command and its flags.
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringP("token", "t", "", "GitHub authentication token")
|
||||
rootCmd.PersistentFlags().IntP("height", "H", 20, "Height of the selection prompt")
|
||||
rootCmd.PersistentFlags().IntP("height", "H", 10, "Height of the selection prompt")
|
||||
rootCmd.PersistentFlags().
|
||||
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")
|
||||
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||
viper.SetEnvPrefix("IGNR")
|
||||
viper.AutomaticEnv()
|
||||
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
|
||||
viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height"))
|
||||
viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter"))
|
||||
viper.BindPFlag("start-search", rootCmd.PersistentFlags().Lookup("start-search"))
|
||||
}
|
||||
|
||||
// main is the entry point of the application.
|
||||
|
||||
35
new.go
35
new.go
@@ -29,11 +29,21 @@ func init() {
|
||||
rootCmd.AddCommand(newCmd)
|
||||
}
|
||||
|
||||
type promptConfig struct {
|
||||
Height int
|
||||
StartSearch bool
|
||||
FilterType string
|
||||
}
|
||||
|
||||
// runNewCommand is the handler for the 'new' command.
|
||||
// It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file.
|
||||
func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||
height := viper.GetInt("height")
|
||||
if height <= 0 {
|
||||
pc := promptConfig{
|
||||
Height: viper.GetInt("height"),
|
||||
StartSearch: viper.GetBool("start-search"),
|
||||
FilterType: viper.GetString("filter"),
|
||||
}
|
||||
if pc.Height <= 0 {
|
||||
return errors.New("height must be a positive integer")
|
||||
}
|
||||
|
||||
@@ -42,7 +52,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||
return errors.New("failed to get GitHub client from context")
|
||||
}
|
||||
|
||||
content, err := runPrompt(client, height)
|
||||
content, err := runPrompt(client, &pc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error running selection prompt: %w", err)
|
||||
}
|
||||
@@ -61,13 +71,13 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||
}
|
||||
|
||||
// runPrompt is a helper function to run the selection prompt for .gitignore templates.
|
||||
func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
|
||||
func runPrompt(client *github.Client, pc *promptConfig) (*github.Gitignore, error) {
|
||||
templates, _, err := client.Gitignores.List(context.Background())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error retrieving gitignore template list: %w", err)
|
||||
}
|
||||
|
||||
selectTemmplates := &promptui.SelectTemplates{
|
||||
selectTemplates := &promptui.SelectTemplates{
|
||||
Label: ` {{ "\U0000007C" | faint }} {{ . | magenta | bold }}`,
|
||||
Active: `{{ "\U0000007C" | faint }} {{ "🌶" | red }} {{ . | cyan | italic }}`,
|
||||
Inactive: `{{ "\U0000007C" | faint }} {{ . | faint }}`,
|
||||
@@ -75,11 +85,12 @@ func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
|
||||
}
|
||||
|
||||
prompt := promptui.Select{
|
||||
Label: "Select a .gitignore template",
|
||||
Items: templates,
|
||||
Templates: selectTemmplates,
|
||||
Size: height,
|
||||
Searcher: filterFunc(templates),
|
||||
Label: "Select a .gitignore template",
|
||||
Items: templates,
|
||||
Templates: selectTemplates,
|
||||
Size: pc.Height,
|
||||
Searcher: filterFunc(templates, pc.FilterType),
|
||||
StartInSearchMode: pc.StartSearch,
|
||||
}
|
||||
|
||||
i, _, err := prompt.Run()
|
||||
@@ -97,7 +108,7 @@ func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
|
||||
|
||||
// commitGitignore writes the content of the selected gitignore template to the .gitignore file.
|
||||
func commitGitignore(content *github.Gitignore, w io.Writer) error {
|
||||
if _, err := fmt.Fprintf(w, "# Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli\n\n## %s ##\n", content.GetName()); err != nil {
|
||||
if _, err := fmt.Fprintf(w, "# Generated by ignr: github.com/onyx-and-iris/ignr\n\n## %s ##\n", content.GetName()); err != nil {
|
||||
return fmt.Errorf("error writing header to file '%s': %w", gitignoreFileName, err)
|
||||
}
|
||||
|
||||
@@ -105,7 +116,7 @@ func commitGitignore(content *github.Gitignore, w io.Writer) error {
|
||||
return fmt.Errorf("error writing to file '%s': %w", gitignoreFileName, err)
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintf(w, "\n# End of ignr-cli\n"); err != nil {
|
||||
if _, err := fmt.Fprintf(w, "\n# End of ignr\n"); err != nil {
|
||||
return fmt.Errorf("error writing footer to file '%s': %w", gitignoreFileName, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user