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 | |
|---|---|---|---|
| 39f762c0e6 | |||
| 4b756ee2fd | |||
| 04e6b0ca49 | |||
| efc8511f26 | |||
| fac0150fcd | |||
| 0c8092528a | |||
| d15402bef1 | |||
| ad6e3dddf1 | |||
| 3a8a846bdb |
30
.github/workflows/update-go-modules.yml
vendored
Normal file
30
.github/workflows/update-go-modules.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
name: Auto-Update Go Modules
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: '0 0 * * 1' # Runs every Monday at midnight
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
update-go-modules:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: stable
|
||||||
|
|
||||||
|
- name: Update Dependencies
|
||||||
|
run: |
|
||||||
|
go get -u ./...
|
||||||
|
go mod tidy
|
||||||
|
git config user.name "github-actions[bot]"
|
||||||
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
git add go.mod go.sum
|
||||||
|
git commit -m "chore: auto-update Go modules"
|
||||||
|
git push
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,6 @@
|
|||||||
# Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli
|
# Generated by ignr-cli: github.com/onyx-and-iris/ignr-cli
|
||||||
|
|
||||||
|
## 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:
|
||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
#
|
#
|
||||||
@@ -28,6 +30,7 @@ go.work.sum
|
|||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
.envrc
|
||||||
|
|
||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
|
|||||||
19
README.md
19
README.md
@@ -14,22 +14,21 @@ Simple no-frills .gitignore generator backed by the Github API.
|
|||||||
go install github.com/onyx-and-iris/ignr-cli@latest
|
go install github.com/onyx-and-iris/ignr-cli@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Authentication
|
## Configuration
|
||||||
|
|
||||||
You can run this tool without authenticating but requests will have a stricter rate limiting.
|
*flags*
|
||||||
|
|
||||||
If you prefer to authenticate you can pass a token in the following ways:
|
- --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)
|
||||||
|
|
||||||
*Flag*
|
*environment variables*
|
||||||
|
|
||||||
- --token/-t: Github API Token
|
|
||||||
|
|
||||||
*Environment Variable*
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
export GH_TOKEN=<API Token>
|
export IGNR_TOKEN=<API Token>
|
||||||
|
export IGNR_HEIGHT=20
|
||||||
```
|
```
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
@@ -42,7 +41,7 @@ Trigger the selection prompt.
|
|||||||
ignr-cli new
|
ignr-cli new
|
||||||
```
|
```
|
||||||
|
|
||||||
The prompt filter can activated by pressing `/`:
|
The prompt filter can be activated by pressing `/`:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/google/go-github/v72/github"
|
|
||||||
)
|
|
||||||
|
|
||||||
type contextKey string
|
|
||||||
|
|
||||||
const clientKey contextKey = "client"
|
|
||||||
|
|
||||||
func getClientFromContext(ctx context.Context) (*github.Client, bool) {
|
|
||||||
client, ok := ctx.Value(clientKey).(*github.Client)
|
|
||||||
return client, ok
|
|
||||||
}
|
|
||||||
68
cmd/root.go
68
cmd/root.go
@@ -1,68 +0,0 @@
|
|||||||
// Package cmd provides a command-line interface for generating .gitignore files.
|
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"runtime/debug"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/google/go-github/v72/github"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
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",
|
|
||||||
Short: "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.
|
|
||||||
You may also list available templates and generate .gitignore files based on those templates.`,
|
|
||||||
SilenceUsage: true,
|
|
||||||
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
||||||
var client *github.Client
|
|
||||||
if !viper.IsSet("token") || viper.GetString("token") == "" {
|
|
||||||
client = github.NewClient(nil)
|
|
||||||
} else {
|
|
||||||
client = github.NewClient(nil).WithAuthToken(viper.GetString("token"))
|
|
||||||
}
|
|
||||||
ctx := context.WithValue(context.Background(), clientKey, client)
|
|
||||||
cmd.SetContext(ctx)
|
|
||||||
},
|
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
||||||
if cmd.Flags().Lookup("version").Changed {
|
|
||||||
if version == "" {
|
|
||||||
info, ok := debug.ReadBuildInfo()
|
|
||||||
if !ok {
|
|
||||||
return errors.New("unable to retrieve build information")
|
|
||||||
}
|
|
||||||
version = strings.Split(info.Main.Version, "-")[0]
|
|
||||||
}
|
|
||||||
fmt.Printf("ignr-cli version: %s\n", version)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd.Help()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.PersistentFlags().StringP("token", "t", "", "GitHub authentication token")
|
|
||||||
rootCmd.Flags().BoolP("version", "v", false, "Print the version of the CLI")
|
|
||||||
|
|
||||||
viper.SetEnvPrefix("GH")
|
|
||||||
viper.AutomaticEnv()
|
|
||||||
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
||||||
func Execute() error {
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
22
context.go
Normal file
22
context.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/google/go-github/v72/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
var clientKey = contextKey("client")
|
||||||
|
|
||||||
|
// withClient returns a new context with the GitHub client set.
|
||||||
|
func withClient(ctx context.Context, client *github.Client) context.Context {
|
||||||
|
return context.WithValue(ctx, clientKey, client)
|
||||||
|
}
|
||||||
|
|
||||||
|
// clientFromContext retrieves the GitHub client from the context.
|
||||||
|
func clientFromContext(ctx context.Context) (*github.Client, bool) {
|
||||||
|
client, ok := ctx.Value(clientKey).(*github.Client)
|
||||||
|
return client, ok
|
||||||
|
}
|
||||||
94
main.go
94
main.go
@@ -1,28 +1,74 @@
|
|||||||
/*
|
// Package cmd provides a command-line interface for generating .gitignore files.
|
||||||
Copyright © 2025 onyx-and-iris <code@onyxandiris.online>
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/onyx-and-iris/ignr-cli/cmd"
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
|
|
||||||
func main() {
|
"github.com/google/go-github/v72/github"
|
||||||
cmd.Execute()
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
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",
|
||||||
|
Short: "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.
|
||||||
|
You may also list available templates and generate .gitignore files based on those templates.`,
|
||||||
|
SilenceUsage: true,
|
||||||
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
||||||
|
var client *github.Client
|
||||||
|
if !viper.IsSet("token") || viper.GetString("token") == "" {
|
||||||
|
client = github.NewClient(nil)
|
||||||
|
} else {
|
||||||
|
client = github.NewClient(nil).WithAuthToken(viper.GetString("token"))
|
||||||
|
}
|
||||||
|
ctx := withClient(context.Background(), client)
|
||||||
|
cmd.SetContext(ctx)
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
if cmd.Flags().Lookup("version").Changed {
|
||||||
|
if version == "" {
|
||||||
|
info, ok := debug.ReadBuildInfo()
|
||||||
|
if !ok {
|
||||||
|
return errors.New("unable to retrieve build information")
|
||||||
|
}
|
||||||
|
version = strings.Split(info.Main.Version, "-")[0]
|
||||||
|
}
|
||||||
|
fmt.Printf("ignr-cli version: %s\n", version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.Flags().BoolP("version", "v", false, "Print the version of the CLI")
|
||||||
|
|
||||||
|
viper.SetEnvPrefix("IGNR")
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
|
||||||
|
viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// main is the entry point of the application.
|
||||||
|
// It executes the root command and handles any errors.
|
||||||
|
func main() {
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package cmd
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/google/go-github/v72/github"
|
"github.com/google/go-github/v72/github"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const gitignoreFileName = ".gitignore"
|
const gitignoreFileName = ".gitignore"
|
||||||
@@ -30,29 +31,23 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runNewCommand is the handler for the 'new' command.
|
// runNewCommand is the handler for the 'new' command.
|
||||||
// It retrieves available .gitignore templates from GitHub, prompts the user to select one,
|
// It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file.
|
||||||
// and writes the selected template to a new .gitignore file.
|
|
||||||
func runNewCommand(cmd *cobra.Command, _ []string) error {
|
func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||||
client, ok := getClientFromContext(cmd.Context())
|
height := viper.GetInt("height")
|
||||||
|
if height <= 0 {
|
||||||
|
return errors.New("height must be a positive integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, ok := clientFromContext(cmd.Context())
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("failed to get GitHub client from context")
|
return errors.New("failed to get GitHub client from context")
|
||||||
}
|
}
|
||||||
|
|
||||||
templates, _, err := client.Gitignores.List(context.Background())
|
content, err := runPrompt(client, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error listing gitignore templates: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var selection string
|
|
||||||
if err := runPrompt(templates, &selection); err != nil {
|
|
||||||
return fmt.Errorf("error running selection prompt: %w", err)
|
return fmt.Errorf("error running selection prompt: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
content, _, err := client.Gitignores.Get(context.Background(), selection)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error retrieving gitignore template '%s': %w", selection, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.OpenFile(gitignoreFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
|
f, err := os.OpenFile(gitignoreFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error opening file '%s': %w", gitignoreFileName, err)
|
return fmt.Errorf("error opening file '%s': %w", gitignoreFileName, err)
|
||||||
@@ -67,13 +62,19 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
|
|||||||
Bold(true).
|
Bold(true).
|
||||||
Foreground(lipgloss.Color("#7D56F4")) // nolint: misspell
|
Foreground(lipgloss.Color("#7D56F4")) // nolint: misspell
|
||||||
|
|
||||||
fmt.Println(style.Render("Created"), selection, style.Render(".gitignore file ✓"))
|
fmt.Println(style.Render("Created"), content.GetName(), style.Render(".gitignore file ✓"))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(templates []string, selection *string) error {
|
func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
|
||||||
|
var selection string
|
||||||
|
|
||||||
|
templates, _, err := client.Gitignores.List(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error retrieving gitignore template list: %w", err)
|
||||||
|
}
|
||||||
var options []huh.Option[string]
|
var options []huh.Option[string]
|
||||||
for _, template := range templates {
|
for _, template := range templates {
|
||||||
options = append(options, huh.NewOption(template, template))
|
options = append(options, huh.NewOption(template, template))
|
||||||
@@ -82,17 +83,24 @@ func runPrompt(templates []string, selection *string) error {
|
|||||||
selectionPrompt := huh.NewSelect[string]().
|
selectionPrompt := huh.NewSelect[string]().
|
||||||
Title("Select a .gitignore template").
|
Title("Select a .gitignore template").
|
||||||
Options(options...).
|
Options(options...).
|
||||||
Value(selection)
|
Height(height).
|
||||||
|
Value(&selection)
|
||||||
|
|
||||||
if err := selectionPrompt.Run(); err != nil {
|
if err := selectionPrompt.Run(); err != nil {
|
||||||
return fmt.Errorf("error running selection prompt: %w", err)
|
return nil, fmt.Errorf("error running selection prompt: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
content, _, err := client.Gitignores.Get(context.Background(), selection)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error retrieving gitignore template '%s': %w", selection, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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-cli: github.com/onyx-and-iris/ignr-cli\n"); 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user