diff --git a/.gitignore b/.gitignore index 95c3dce..52dd8b2 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ go.work.sum # env file .env +.envrc # Editor/IDE # .idea/ diff --git a/README.md b/README.md index e374e86..b91202b 100644 --- a/README.md +++ b/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 ``` -## 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* - -- --token/-t: Github API Token - -*Environment Variable* +*environment variables* ```bash #!/usr/bin/env bash -export GH_TOKEN= +export IGNR_TOKEN= +export IGNR_HEIGHT=20 ``` ## Commands @@ -38,11 +37,6 @@ export GH_TOKEN= Trigger the selection prompt. -- flags: - - *optional* - - --height: Height of the selection prompt, defaults to 20. - ```console ignr-cli new ``` diff --git a/main.go b/main.go index ae32a55..97dfc3b 100644 --- a/main.go +++ b/main.go @@ -54,11 +54,14 @@ 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.Flags().BoolP("version", "v", false, "Print the version of the CLI") - viper.SetEnvPrefix("GH") + 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. diff --git a/new.go b/new.go index f6aa0e6..5ed32ff 100644 --- a/new.go +++ b/new.go @@ -11,6 +11,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/google/go-github/v72/github" "github.com/spf13/cobra" + "github.com/spf13/viper" ) const gitignoreFileName = ".gitignore" @@ -27,16 +28,14 @@ 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 the selected .gitignore template from GitHub and writes it to the .gitignore file. func runNewCommand(cmd *cobra.Command, _ []string) error { - height, err := cmd.Flags().GetInt("height") - if err != nil { - return fmt.Errorf("error getting height flag: %w", err) + height := viper.GetInt("height") + if height <= 0 { + return errors.New("height must be a positive integer") } client, ok := clientFromContext(cmd.Context())