Skip to content

Commit e89a112

Browse files
authored
Self update and script for installing (#23)
1 parent a44f82e commit e89a112

File tree

9 files changed

+432
-2
lines changed

9 files changed

+432
-2
lines changed

Cargo.lock

+286
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ termion = "1.5.6"
1818
rand = "0.8.5"
1919
tabled = "0.5.0"
2020
serde_json = "1.0.79"
21+
self_update = "0.28.0"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Prebuilt binaries exist for **Linux x86_64** and **MacOS arm64** and **x86_64**
1010
```
11-
$ export VERSION=v0.0.2 && curl -LJO https://github.com/allanger/gitlab-user-manager/releases/download/$VERSION/gum-$VERSION-$(arch)-$(uname) && mv gum-$VERSION-$(arch)-$(uname) gum && chmod +x gum
11+
$ curl https://raw.githubusercontent.com/allanger/gitlab-user-manager/main/scripts/download_gum.sh | bash
1212
$ gum -h
1313
```
1414
### Docker
@@ -72,7 +72,7 @@ $ gum users add-team USER_ID team-1 team-2
7272
9. Apply config
7373
```
7474
## to see what's gonna happen
75-
$ gum sync --dry-run true
75+
$ gum sync --dry-run
7676
## to apply
7777
$ gum sync
7878
```

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.0.6

scripts/download_gum.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
case "$(uname)" in
3+
4+
"Darwin")
5+
SYSTEM="apple-darwin"
6+
case $(uname -m) in
7+
"arm64")
8+
TARGET="aarch64-$SYSTEM"
9+
;;
10+
"x68_64")
11+
TARGET="x86_64-$SYSTEM"
12+
;;
13+
*)
14+
echo "Unsuported target"
15+
exit 1
16+
;;
17+
esac
18+
;;
19+
"Linux")
20+
SYSTEM="unknown-linux-gnu"
21+
case $(uname -m) in
22+
"x68_64")
23+
TARGET="x86_64-$SYSTEM"
24+
;;
25+
*)
26+
echo "Unsuported target"
27+
exit 1
28+
;;
29+
esac
30+
;;
31+
*)
32+
echo "Signal number $1 is not processed"
33+
exit 1
34+
;;
35+
esac
36+
LATEST_VERSION=$(curl https://allanger.github.io/gitlab-user-manager/)
37+
echo "Downloading"
38+
RELEASE_NAME=gum-$LATEST_VERSION-$TARGET
39+
RELEASE_URL="https://github.com/allanger/gitlab-user-manager/releases/download/$LATEST_VERSION/$RELEASE_NAME"
40+
echo $RELEASE_URL
41+
curl -LJO $RELEASE_URL
42+
43+
mv $RELEASE_NAME gum
44+
chmod +x gum
45+
46+
echo "Make sure that gum is in your $PATH"
47+
echo 'Try: '
48+
echo ' $ export PATH=$PATH:$PWD'
49+
echo ' $ gum -h'

src/args.rs

+35
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,38 @@ pub(crate) trait Args<'a> {
1919
fn add() -> Arg<'static>;
2020
fn parse<'b>(sub_matches: &'b ArgMatches) -> Result<Self::ArgType>;
2121
}
22+
23+
pub(crate) mod no_confirm {
24+
use clap::{Arg, ArgMatches};
25+
26+
use super::Args;
27+
28+
static ARG: &str = "no-confirm";
29+
pub(crate) struct ArgNoConfirm {
30+
value: bool,
31+
}
32+
33+
impl ArgNoConfirm {
34+
pub(crate) fn value(&self) -> bool {
35+
self.value
36+
}
37+
}
38+
39+
impl Args<'_> for ArgNoConfirm {
40+
type ArgType = ArgNoConfirm;
41+
42+
fn add() -> Arg<'static> {
43+
Arg::new(ARG)
44+
.long(ARG)
45+
.short('y')
46+
.takes_value(false)
47+
.help("Use if the user shouldn't be prompted to confirm an update")
48+
}
49+
50+
fn parse<'b>(sub_matches: &'b ArgMatches) -> std::io::Result<Self::ArgType> {
51+
Ok(ArgNoConfirm {
52+
value: sub_matches.is_present(ARG),
53+
})
54+
}
55+
}
56+
}

src/cmd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub(crate) mod init;
22
pub(crate) mod search;
3+
pub(crate) mod upgrade;
34
pub(crate) mod sync;
45
pub(crate) mod teams;
56
pub(crate) mod users;

src/cmd/upgrade.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::io::{Error, ErrorKind};
2+
3+
use clap::{ArgMatches, Command};
4+
use self_update::{backends::github::Update, cargo_crate_version};
5+
6+
use crate::{
7+
args::{no_confirm::ArgNoConfirm, Args},
8+
cmd::Cmd,
9+
output::OutMessage,
10+
};
11+
12+
/// init cmd should be used to generate an empty gum-config
13+
pub(crate) fn add_upgrade_cmd() -> Command<'static> {
14+
return Command::new("upgrade")
15+
.about("Update current gum with a newer version")
16+
.arg(ArgNoConfirm::add());
17+
}
18+
19+
pub(crate) struct UpgradeCmd {
20+
no_confirm: bool,
21+
}
22+
23+
pub(crate) fn prepare<'a>(sub_matches: &'a ArgMatches) -> Result<impl Cmd<'a>, Error> {
24+
let no_confirm: bool = ArgNoConfirm::parse(sub_matches).unwrap().value();
25+
Ok(UpgradeCmd { no_confirm })
26+
}
27+
28+
impl<'a> Cmd<'a> for UpgradeCmd {
29+
fn exec(&self) -> Result<(), Error> {
30+
let status = match Update::configure()
31+
.repo_owner("allanger")
32+
.repo_name("gitlab-user-manager")
33+
.bin_name("gum")
34+
.show_download_progress(true)
35+
.current_version(cargo_crate_version!())
36+
.no_confirm(self.no_confirm)
37+
.build()
38+
.unwrap()
39+
.update()
40+
{
41+
Ok(s) => s,
42+
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
43+
};
44+
OutMessage::message_empty(format!("Update status: `{}`!", status.version()).as_str());
45+
Ok(())
46+
}
47+
}

src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use cmd::{
1010
search::{self, add_search_cmd},
1111
sync::{self, add_sync_cmd},
1212
teams::{self, add_teams_cmd},
13+
upgrade::{self, add_upgrade_cmd},
1314
users::{self, add_users_cmd},
1415
Cmd,
1516
};
@@ -29,6 +30,7 @@ fn main() {
2930
.subcommand(add_teams_cmd())
3031
.subcommand(add_search_cmd())
3132
.subcommand(add_sync_cmd())
33+
.subcommand(add_upgrade_cmd())
3234
.get_matches();
3335

3436
let result: Result<(), Error>;
@@ -64,8 +66,16 @@ fn main() {
6466
Err(err) => Err(err),
6567
};
6668
}
69+
Some(("upgrade", sub_matches)) => {
70+
result = match upgrade::prepare(sub_matches) {
71+
Ok(cmd) => cmd.exec(),
72+
Err(err) => Err(err),
73+
};
74+
}
75+
6776
_ => result = Err(Error::new(ErrorKind::InvalidInput, "No command provided")),
6877
}
78+
6979
match result {
7080
Err(err) => {
7181
OutSum::sum_failure(&err.to_string());

0 commit comments

Comments
 (0)