Skip to content

Commit

Permalink
cmd: Create config setting to control sort order
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
jmooring committed Sep 18, 2023
1 parent ac608a5 commit 640af42
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cmd/hvm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion cmd/hvm/testscripts/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: '
8 changes: 5 additions & 3 deletions cmd/hvm/testscripts/config_with_env_vars.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: '
7 changes: 5 additions & 2 deletions cmd/hvm/testscripts/config_with_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion cmd/hvm/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ func (r *repository) fetchTags() error {
}
}

semver.Sort(tagNames)
if Config.SortAscending {
semver.Sort(tagNames)
}

n := Config.NumTagsToDisplay
if n < 0 {
Expand Down

0 comments on commit 640af42

Please sign in to comment.