Skip to content

Commit

Permalink
add check comand
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed May 5, 2023
1 parent 70a8f95 commit 75f7c39
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 30 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

## v1.1.0

- A new command [`check`](https://github.com/zekroTJA/goup/blob/main/docs/commands.md#check) has been added which can be used to check for new upstream versions compared to the currently used one.
![](https://user-images.githubusercontent.com/16734205/236545310-b6aa6956-93c8-4b6a-b50e-27332dd52104.gif)

- Remote versions are now fetched via the GitHub REST API and `git ls-remote --tags` is only used as fallback. This should improve the performance significantly. [#1]

- A warning is now printed using the commands `ls`, `current` and `use` when the required environment variables are not set.

- A better about description has been added to the `env` command when displaying the long help using `help env` or `env --help`.


## v1.0.0

- Initial release.
Binary file added assets/check-command-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions assets/check-command-demo.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Set FontSize 30
Set Padding 30
Set WindowBar Colorful

Type "goup ls"
Enter
Sleep 1s
Type "goup use 1.19.0"
Enter
Sleep 1s
Type "goup check"
Enter
Sleep 1s
Type "goup use 1.20.0"
Enter
Sleep 6s
Type "goup check"
Enter
Sleep 1s
Type "goup use"
Enter
Sleep 1s
Type "goup check"
Enter
Sleep 5s
48 changes: 34 additions & 14 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@
```
goup helps to install, update and switch between Go SDK versions in an as easy as possible way.
Simply use `goup env -p && source ~/profile` to add the required environment variables.
After that, download the latest version of Go using `goup use`.
Simply use `goup env -p && source ~/profile` to add the required environment variables. After that, download the latest version of Go using `goup use`.
Usage: goup <COMMAND>
Commands:
check
Check for updates
clean
Remove all installed SDKs [aliases: purge, prune]
current
Display the currently selected version of Go [aliases: c]
drop
Drop an installed SDK [aliases: delete, remove]
env
Print env variables required to use goup
use
Install a version of Go [aliases: u, select, install]
lsr
List all upstream versions [aliases: ls-remote, list-remote]
ls
Display currently installed SDKs [aliases: list]
drop
Drop an installed SDK [aliases: delete, remove]
clean
Remove all installed SDKs [aliases: purge, prune]
lsr
List all upstream versions [aliases: ls-remote, list-remote]
use
Install a version of Go [aliases: u, select, install]
help
Print this message or the help of the given subcommand(s)
Expand All @@ -41,17 +42,31 @@ Options:
## Index


- [check](#check): `Check for updates`
- [clean](#clean): `Remove all installed SDKs`
- [current](#current): `Display the currently selected version of Go`
- [drop](#drop): `Drop an installed SDK`
- [env](#env): `Print env variables required to use goup`
- [env](#env): `This command prints all necessary environment variables and values required to use goup.`
- [ls](#ls): `Display currently installed SDKs`
- [lsr](#lsr): `List all upstream versions`
- [use](#use): `Install a version of Go`

## Details


### check

> $ goup help check
```
Check for updates
Usage: goup check
Options:
-h, --help Print help
```

### clean

> $ goup help clean
Expand Down Expand Up @@ -99,13 +114,18 @@ Options:
> $ goup help env
```
Print env variables required to use goup
This command prints all necessary environment variables and values required to use goup.
Using `goup env -p` appends the variables to your `.profile` file in your $HOME directory. After that, you can apply the changes to your current terminal session using `source ~/.profile`.
If you only want to play around with goup, you can temporarily apply the environment variables to your current terminal session using the command`eval "$(goup env)"`.
Usage: goup env [OPTIONS]
Options:
-p, --profile Apply the environment variables to your .profile
-h, --help Print help
-p, --profile
Apply the environment variables to your .profile
-h, --help
Print help (see a summary with '-h')
```

### ls
Expand Down
66 changes: 66 additions & 0 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::Command;
use crate::{
env::*,
success,
versions::{get_upstream_versions, Version, VersionPart},
warning,
};
use clap::Args;
use console::style;

/// Check for updates.
#[derive(Args)]
pub struct Check {}

impl Command for Check {
fn run(&self) -> anyhow::Result<()> {
check_env_applied()?;

let Some(current) = get_current_version()? else {
warning!("No version has been selected.\n\
Use `goup use` to select an SDK version.");
return Ok(());
};

let upstream_versions = get_upstream_versions()?;

let new_minor = upstream_versions
.iter()
.rev()
.find(|v| v.minor > current.minor && current.strip_after(VersionPart::Major).covers(v));

let new_patch = upstream_versions
.iter()
.rev()
.find(|v| v.patch > current.patch && current.strip_after(VersionPart::Minor).covers(v));

let mut new_pre = None;
if !current.is_stable() {
new_pre = upstream_versions
.iter()
.rev()
.find(|v| v.pre > current.pre && current.strip_after(VersionPart::Patch).covers(v));
}

checkprint("pre-release", &current, new_pre);
checkprint("minor", &current, new_minor);
checkprint("patch", &current, new_patch);

if new_pre.is_none() && new_minor.is_none() && new_patch.is_none() {
success!("You are up to date with the latest upstream version!");
}

Ok(())
}
}

fn checkprint(typ: &str, current: &Version, v: Option<&Version>) {
if let Some(new) = v {
success!("New {typ} version is available!");
println!(
"{} → {}",
style(current.to_string()).dim(),
style(new).cyan()
);
}
}
4 changes: 2 additions & 2 deletions src/commands/lsr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Command;
use crate::versions::get_versions;
use crate::versions::get_upstream_versions;
use clap::{Args, ValueEnum};

#[derive(ValueEnum, Clone)]
Expand All @@ -20,7 +20,7 @@ pub struct Lsr {

impl Command for Lsr {
fn run(&self) -> anyhow::Result<()> {
let tags = get_versions()?;
let tags = get_upstream_versions()?;
let mut tags: Box<dyn Iterator<Item = _>> = Box::new(tags.iter());

match self.filter {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
crate::prelude!(current, env, ls, lsr, r#use, drop, clean);
crate::prelude!(current, env, ls, lsr, r#use, drop, clean, check);

use anyhow::Result;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Command for Use {
let version: Version = match version.to_lowercase().as_str() {
"stable" => get_latest_upstream_version(false)?,
"unstable" => get_latest_upstream_version(true)?,
v => find_version(&v.parse()?)?,
v => find_upstream_version(&v.parse()?)?,
};

let install_dir = get_version_installation_dir(&version)?;
Expand Down
2 changes: 1 addition & 1 deletion src/env/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn check_env_applied() -> Result<()> {
warning!(
"Seems like necessary environment variables have not been applied. \
This results in the selected SDK version not being available in the terminal.\n\
Please see 'goup help env' to setup required environment variables.\n"
Please see `goup help env` to setup required environment variables.\n"
);
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct App {
command: Commands,
}

register_commands!(Current, Env, Use, Lsr, Ls, Drop, Clean);
register_commands!(Check, Clean, Current, Drop, Env, Ls, Lsr, Use,);

fn main() {
let app = App::parse();
Expand Down
4 changes: 2 additions & 2 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn print_success(v: &str) {
pub fn print_error(v: &str) {
Term::stdout().clear_line();
println!(
"{}: {}",
"{} {}",
style("error:").red().bold(),
style(v).red().bright()
);
Expand All @@ -65,7 +65,7 @@ pub fn print_error(v: &str) {
pub fn print_warning(v: &str) {
Term::stdout().clear_line();
println!(
"{}: {}",
"{} {}",
style("warning:").yellow().bold(),
style(v).yellow().bright()
);
Expand Down
16 changes: 8 additions & 8 deletions src/versions/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ const GOLANG_TAGS_ENDPOINT: &str =
/// The tags are first tried to be fetched via the GitHub API.
/// If this fails, a warning message is printed and
/// `git ls-remote --tags` is used as fallback.
pub fn get_versions() -> Result<Vec<Version>> {
let mut tags = get_versions_api().or_else(|err| {
pub fn get_upstream_versions() -> Result<Vec<Version>> {
let mut tags = get_upstream_versions_api().or_else(|err| {
warning!(
"Listing remote versions via GitHub API failed, falling back to using git ls-remote.\n\
Error was: {err}"
);
get_versions_git()
get_upstream_versions_git()
})?;

tags.sort();
Expand All @@ -37,7 +37,7 @@ pub fn get_versions() -> Result<Vec<Version>> {

/// Fetches a list of versions from the Go remote repository on
/// GitHub using `git ls-remote --tags`.
fn get_versions_git() -> Result<Vec<Version>> {
fn get_upstream_versions_git() -> Result<Vec<Version>> {
let res = exec(&["git", "ls-remote", "--tags", GOLANG_REPO]);

let res = match res {
Expand Down Expand Up @@ -65,7 +65,7 @@ struct Ref {

/// Fetches a list of versions from the Go remote repository on
/// GitHub using the GitHub REST API.
fn get_versions_api() -> Result<Vec<Version>> {
fn get_upstream_versions_api() -> Result<Vec<Version>> {
let refs: Vec<Ref> = Client::builder()
.build()?
.get(GOLANG_TAGS_ENDPOINT)
Expand All @@ -90,7 +90,7 @@ fn get_versions_api() -> Result<Vec<Version>> {
/// If no version has been found, an error of type [`anyhow::Error`]
/// is returned with a message containing more details.
pub fn get_latest_upstream_version(include_unstable: bool) -> Result<Version> {
get_versions()?
get_upstream_versions()?
.iter()
.rev()
.find(|v| include_unstable || v.is_stable())
Expand All @@ -109,8 +109,8 @@ pub fn get_latest_upstream_version(include_unstable: bool) -> Result<Version> {
/// # Errors
/// If no version has been found, an error of type [`anyhow::Error`]
/// is returned with a message containing more details.
pub fn find_version(s: &Version) -> Result<Version> {
get_versions()?
pub fn find_upstream_version(s: &Version) -> Result<Version> {
get_upstream_versions()?
.iter()
.rev()
.filter(|v| v.is_stable() || !s.is_stable())
Expand Down
Loading

0 comments on commit 75f7c39

Please sign in to comment.