mirror of
https://github.com/onyx-and-iris/ignr.git
synced 2025-07-27 06:11:48 +00:00
move api list/get calls into runPrompt
add --height/-H flag to new command add template language to template. This may be useful if combining gitignores.
This commit is contained in:
parent
ad6e3dddf1
commit
d15402bef1
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
||||
# 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:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
|
10
context.go
10
context.go
@ -8,9 +8,15 @@ import (
|
||||
|
||||
type contextKey string
|
||||
|
||||
const clientKey contextKey = "client"
|
||||
var clientKey = contextKey("client")
|
||||
|
||||
func getClientFromContext(ctx context.Context) (*github.Client, bool) {
|
||||
// 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
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -31,7 +31,7 @@ You may also list available templates and generate .gitignore files based on tho
|
||||
} else {
|
||||
client = github.NewClient(nil).WithAuthToken(viper.GetString("token"))
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), clientKey, client)
|
||||
ctx := withClient(context.Background(), client)
|
||||
cmd.SetContext(ctx)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
|
49
new.go
49
new.go
@ -27,32 +27,28 @@ var newCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(newCmd)
|
||||
|
||||
newCmd.Flags().IntP("height", "H", 20, "Height of the selection prompt")
|
||||
}
|
||||
|
||||
// runNewCommand is the handler for the 'new' command.
|
||||
// It retrieves available .gitignore templates from GitHub, prompts the user to select one,
|
||||
// and writes the selected template to a new .gitignore file.
|
||||
// It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file.
|
||||
func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||
client, ok := getClientFromContext(cmd.Context())
|
||||
height, err := cmd.Flags().GetInt("height")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting height flag: %w", err)
|
||||
}
|
||||
|
||||
client, ok := clientFromContext(cmd.Context())
|
||||
if !ok {
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening file '%s': %w", gitignoreFileName, err)
|
||||
@ -67,13 +63,19 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
|
||||
Bold(true).
|
||||
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
|
||||
}
|
||||
|
||||
// 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]
|
||||
for _, template := range templates {
|
||||
options = append(options, huh.NewOption(template, template))
|
||||
@ -82,17 +84,24 @@ func runPrompt(templates []string, selection *string) error {
|
||||
selectionPrompt := huh.NewSelect[string]().
|
||||
Title("Select a .gitignore template").
|
||||
Options(options...).
|
||||
Value(selection)
|
||||
Height(height).
|
||||
Value(&selection)
|
||||
|
||||
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.
|
||||
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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user