Skip to content

Commit

Permalink
add: right to blih repo
Browse files Browse the repository at this point in the history
  • Loading branch information
cchalop1 committed Mar 17, 2020
1 parent 11de860 commit 2f7bdb3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
test.sh
blih.py
16 changes: 12 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Home {
#[derive(Deserialize, Debug)]
pub struct Board {
projets: Vec<Projet>,
activites: Vec<Activites>,
pub activites: Vec<Activites>,
}

#[derive(Deserialize, Debug)]
Expand All @@ -50,7 +50,7 @@ pub struct Projet {

#[derive(Deserialize, Debug)]
pub struct Activites {
title: String,
pub title: String,
module: String,
module_link: String,
module_code: String,
Expand Down Expand Up @@ -228,8 +228,16 @@ impl Board {
]);
table.add_row(row!["Salle: ", activite.salle]);
match &activite.token {
Some(token) => table.add_row(row!["token: ", token]),
None => table.add_row(row!["token: ", "null"]),
Some(token) => {
table.add_row(row!["token: ", &token]);
if &token[..] == "1" {
table.add_row(row!["token: ", token]);
println!("{}", token);
}
},
None => {
table.add_row(row!["token: ", "null"]);
}
};
table.printstd();
}
Expand Down
55 changes: 51 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ enum Opt {
/// Dispaly all repos on blih
///
repo { repo_name: Option<String> },
/// enter one token
token { idx: Option<i32> },
}

fn main() {
Expand All @@ -69,20 +71,31 @@ fn start() {
Opt::activity { idx } => match idx {
Some(idx) => fetch_home(&pass.autologin).print_activity_detail(idx, &pass.autologin),
None => fetch_home(&pass.autologin).print_activity(),
},
},
Opt::notes => fetch_note_modules(&pass.autologin).print_notes(),
Opt::modules => fetch_note_modules(&pass.autologin).print_modules(),
Opt::repo { repo_name } => match repo_name {
Some(repo_name) => fetch_create_repo(&pass, repo_name),
None => fetch_repos(&pass).print_repos(),
},
Opt::token { idx } => match idx {
Some(idx) => println!("{}", idx),
None => fetch_all_token_open(&pass.autologin),
},
};
}

fn json_for_create_repo(name: &String) -> String {
format!("{{\n \"name\": \"{}\",\n \"type\": \"git\"\n}}", name)
}

fn json_for_right_repo(right: &String, user: &String) -> String {
format!(
"{{\n \"acl\": \"{}\",\n \"user\": \"{}\"\n}}",
right, user
)
}

fn fetch_create_repo(pass: &Pass, repo_name: String) {
let data: BlihData = BlihData {
user: format!("{}", &pass.login),
Expand All @@ -105,14 +118,40 @@ fn fetch_create_repo(pass: &Pass, repo_name: String) {
if let Some(error) = resp.error {
println!("{}", error);
}
if let Some(message) = resp.message{
if let Some(message) = resp.message {
println!("{}", message);
println!("git@git.epitech.eu:/{}/{}", pass.login, repo_name);
fetch_right_repo(&pass, repo_name, format!("ramassage-tek"), format!("r"));
}
}

// Todo
// fn fetch_right_repo
fn fetch_right_repo(pass: &Pass, repo_name: String, user: String, user_right: String) {
let data: BlihData = BlihData {
user: format!("{}", &pass.login),
signature: do_hamxc_login_passwd(
&pass.passwd,
&pass.login,
Some(json_for_right_repo(&user_right, &user)),
),
data: json!({"user": &user, "acl": &user_right}),
};
let url_path: &str = &format!("/repository/{}/acls", repo_name)[..];
let url: String = builder_url_blih(&BLIH_URL, url_path);
let resp: BlihResponse = reqwest::Client::new()
.post(&url[..])
.header("Content-Type", "application/json")
.body(serde_json::to_string(&data).unwrap())
.send()
.unwrap()
.json()
.unwrap();
if let Some(error) = resp.error {
println!("{}", error);
}
if let Some(message) = resp.message {
println!("{}", message);
}
}

fn fetch_repos(pass: &Pass) -> Repos {
let data: Blih = Blih {
Expand All @@ -136,6 +175,14 @@ fn fetch_home(autologin_url: &String) -> Board {
home.board
}

fn fetch_all_token_open(autologin_url: &String) {
let url: String = builder_url_autologin(&autologin_url, "");
let home: Home = reqwest::get(&url[..]).unwrap().json().unwrap();
for act in home.board.activites.iter() {
println!("{:?}", act);
}
}

fn fetch_user(autologin_url: &String) -> User {
let url: String = builder_url_autologin(&autologin_url, "/user");
reqwest::get(&url[..]).unwrap().json().unwrap()
Expand Down

0 comments on commit 2f7bdb3

Please sign in to comment.