zyedidia / eget

Easily install prebuilt binaries from GitHub.
MIT License
954 stars 39 forks source link

Add config file support #53

Closed patinthehat closed 1 year ago

patinthehat commented 1 year ago

This PR adds support for using a configuration file with Eget.

Summary

This adds supports for a TOML configuration file located at ~/.eget.toml, or one located in the same directory as the eget binary. Note that using command line flags will always override any configuration file settings that have been configured.

Implementation

Both global settings can be configured, as well as setting on a per-repository basis.

Sections can be named either global or "owner/repo", where owner and repo are the owner and repository name of the target repository (not that the owner/repo format is quoted).

Sections can be named either global or "owner/repo", where owner and repo are the owner and repository name of the target repository (not that the owner/repo format is quoted).

For example, the following configuration file will set the --to flag to ~/bin for all repositories, and will set the --to flag to ~/.local/bin for the zyedidia/micro repository.

[global]
    target = "~/bin"

["zyedidia/micro"]
    target = "~/.local/bin"

Example configuration

[global]
    github_token = "ghp_1234567890"
    quiet = false
    show_hash = false
    upgrade_only = true
    target = "./test"

["zyedidia/micro"]
    upgrade_only = false
    show_hash = true
    asset_filters = [ "static", ".tar.gz" ]
    target = "~/.local/bin/micro"

By using the configuration above, you could run the following command to download the latest release of micro:

eget zyedidia/micro

Without the configuration, you would need to run the following command instead:

export EGET_GITHUB_TOKEN=ghp_1234567890 &&\
eget zyedidia/micro --to ~/.local/bin/micro --sha256 --asset static --asset .tar.gz

Available settings

Setting Related Flag Description Default
github_token N/A GitHub API token to use for requests ""
all --all Whether to extract all candidate files. false
asset_filters --asset An array of partial asset names to filter the available assets for download. []
download_only --download-only Whether to stop after downloading the asset (no extraction). false
download_source --source Download only the source code false
file --file The glob to select files for extraction. *
quiet --quiet Whether to only print essential output. false
show_hash --sha256 Whether to show the SHA-256 hash of the downloaded asset. false
system --system The target system to download for. all
target --to The directory to move the downloaded file to after extraction. .
upgrade_only --upgrade-only Whether to only download if release is more recent than current version. false
zyedidia commented 1 year ago

Wow, this is great! I think this is a really nice proposal for what the config file should support. I'll look at the code more in a bit, but my initial thought is that it would be nice to use a toml library directly (https://github.com/pelletier/go-toml), rather than depending on Viper, which seems to pull huge number of dependencies into go.sum, and generally seems a bit overkill. Thanks for the contribution!

patinthehat commented 1 year ago

@zyedidia Thanks! I put a lot of thought into the best way to configure eget using a config file.

which seems to pull huge number of dependencies into go.sum, and generally seems a bit overkill.

The reason I chose viper was for a few reasons:

I feel that viper gives us a lot of flexibility for the future. If you do decide that viper isn't what you want to use, I'll refactor and use the go-toml library. Please Let me know.

patinthehat commented 1 year ago

@zyedidia Are there any changes you'd like to see to this PR for a merge?

larsks commented 1 year ago

I wanted to do pretty much the same thing that this pull request attempts to enable, but I handled it by putting together a simple shell-script wrapper that reads from a configuration file and drives multiple calls to eget. You can see the script here. I've been using this to maintain a list of 40+ packages on a couple of different machines and it's been working great so far.

zyedidia commented 1 year ago

Sorry for the delay. I think it would be best to stick with a custom and simpler toml solution rather than using Viper and bringing in all the dependencies and complexity it has (850 additions to go.sum is a bit concerning for a project of this size). In general I would like to err on the side of keeping eget small, though I think a config file of this sort would be useful and acceptable. Alternatively, maybe the eget-all script from @larsks provides enough functionality to cover your use-case?

patinthehat commented 1 year ago

@zyedidia I've refactored to use a smaller, toml-specific library. What do you think?

zyedidia commented 1 year ago

Great, thanks! Initially it looks good, will take a closer look shortly.

zyedidia commented 1 year ago

Looks great, thanks!