diff --git a/README.md b/README.md index dfdd493..4023bd7 100644 --- a/README.md +++ b/README.md @@ -148,25 +148,29 @@ Use "hvm [command] --help" for more information about a command. ## Configuration -### Number of releases to display +To locate the configuration file, run the `hvm config` command. This will print the path to the configuration file to the console. Keys in the configuration file are case-insensitive. -By default, the `hvm use` and `hvm install` commands display the 30 most recent releases. To change the number of releases displayed, you can do one of the following: +To set configuration values with an environment variable, create an environment variable prefixed with `HVM_`. For example, to set the `sortAscending` configuration value to false: -- Set the `numtagstodisplay` option in the hvm configuration file. You can find the path to the configuration file by running the `hvm config` command. -- Set the `HVM_NUMTAGSTODISPLAY` environment variable. +```text +export HVM_SORTASCENDING=false +``` -To display all releases since v0.54.0, set the value to `-1`. Releases before v0.54.0 were not semantically versioned. +An environment variable takes precedence over the values set in the configuration file. This means that if you set a configuration value with both an environment variable and in the configuration file, the value in the environment variable will be used. -### GitHub personal access token +**gitHubToken** (string) GitHub limits the number of requests that can be made to its API per hour to 60 for unauthenticated clients. If you exceed this limit, hvm will display a message indicating when the limit will be reset. This is typically within minutes. -If you regularly exceed this limit, you can create a GitHub personal access token with read-only public repository access. Then, you can do either of the following: +If you regularly exceed this limit, you can create a GitHub personal access token with read-only public repository access. With a personal access token, GitHub limits API requests to 5,000 per hour. + +**numTagsToDisplay** (int, default `30`) + +By default, the `hvm use` and `hvm install` commands display the 30 most recent releases. To display all releases since v0.54.0, set the value to `-1`. Releases before v0.54.0 were not semantically versioned. -- Set the `githubtoken` option in the hvm configuration file. You can find the path to the configuration file by running the hvm config command. -- Set the `HVM_GITHUBTOKEN` environment variable. +**sortAscending** (bool, default `true`) -With a personal access token, GitHub limits API requests to 5,000 per hour. +By default, the `hvm use` and `hvm install` commands display the list of recent releases in ascending order. To display the list in descending order, set this value to `false`. [go]: https://go.dev/doc/install [hugo]: https://github.com/gohugoio/hugo/#readme diff --git a/cmd/hvm/cmd.go b/cmd/hvm/cmd.go index bf13511..df31742 100644 --- a/cmd/hvm/cmd.go +++ b/cmd/hvm/cmd.go @@ -45,8 +45,9 @@ type application struct { // A configuration contains the current configuration parameters from environment // variables, the configuration file, or default values, in that order. type configuration struct { - GithubToken string // a GitHub personal access token - NumTagsToDisplay int // number of tags to display when using the "use" and "install" commands + GithubToken string `toml:"githubToken"` // a GitHub personal access token + NumTagsToDisplay int `toml:"numTagsToDisplay"` // number of tags to display when using the "use" and "install" commands + SortAscending bool `toml:"sortAscending"` // displays tags list in ascending order } var App application = application{ @@ -98,8 +99,9 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { // Set default values. - viper.SetDefault("numTagsToDisplay", 30) viper.SetDefault("githubToken", "") + viper.SetDefault("numTagsToDisplay", 30) + viper.SetDefault("sortAscending", true) // Create config directory. userConfigDir, err := os.UserConfigDir() diff --git a/cmd/hvm/testscripts/config.txt b/cmd/hvm/testscripts/config.txt index 81856f4..651cff6 100644 --- a/cmd/hvm/testscripts/config.txt +++ b/cmd/hvm/testscripts/config.txt @@ -9,5 +9,7 @@ # Test exec hvm config -stdout 'NumTagsToDisplay = 30\n' +stdout 'githubToken = ''''\n' +stdout 'numTagsToDisplay = 30\n' +stdout 'sortAscending = true\n' stdout 'Configuration file: ' diff --git a/cmd/hvm/testscripts/config_with_env_vars.txt b/cmd/hvm/testscripts/config_with_env_vars.txt index 30f2d3e..b055820 100644 --- a/cmd/hvm/testscripts/config_with_env_vars.txt +++ b/cmd/hvm/testscripts/config_with_env_vars.txt @@ -7,11 +7,13 @@ [windows] env LocalAppData=cache [windows] env AppData=config -env HVM_NUMTAGSTODISPLAY=42 env HVM_GITHUBTOKEN=my-token +env HVM_NUMTAGSTODISPLAY=42 +env HVM_SORTASCENDING=true # Test exec hvm config -stdout 'GithubToken = ''my-token''\n' -stdout 'NumTagsToDisplay = 42\n' +stdout 'githubToken = ''my-token''\n' +stdout 'numTagsToDisplay = 42\n' +stdout 'sortAscending = true\n' stdout 'Configuration file: ' diff --git a/cmd/hvm/testscripts/config_with_file.txt b/cmd/hvm/testscripts/config_with_file.txt index 7204b10..71fd6d0 100644 --- a/cmd/hvm/testscripts/config_with_file.txt +++ b/cmd/hvm/testscripts/config_with_file.txt @@ -9,16 +9,19 @@ # Test exec hvm config -stdout 'GithubToken = ''my-token''\n' -stdout 'NumTagsToDisplay = 42\n' +stdout 'githubToken = ''my-token''\n' +stdout 'numTagsToDisplay = 42\n' +stdout 'sortAscending = false' stdout 'Configuration file: ' # Files for darwin -- home/Library/Application Support/hvm/config.toml -- githubtoken = "my-token" numtagstodisplay = 42 +sortascending = false # Files for linux and windows -- config/hvm/config.toml -- githubtoken = "my-token" numtagstodisplay = 42 +sortascending = false diff --git a/cmd/hvm/use.go b/cmd/hvm/use.go index 01d1627..31d5b4a 100644 --- a/cmd/hvm/use.go +++ b/cmd/hvm/use.go @@ -200,7 +200,9 @@ func (r *repository) fetchTags() error { } } - semver.Sort(tagNames) + if Config.SortAscending { + semver.Sort(tagNames) + } n := Config.NumTagsToDisplay if n < 0 {