first cli (wip)

This commit is contained in:
Julien Fastré 2023-03-27 17:53:41 +02:00
commit 4725e096f4
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
9 changed files with 1636 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.idea/*

1483
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "cl-cli"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.1.13", features = ["derive"] }
gitlab = "0.1510.0"
serde = { version = "1.0.158", features = ["derive"] }
toml = "0.7.3"
url = "2.3.1"

2
config.toml Normal file
View File

@ -0,0 +1,2 @@
[gitlab]
token = "glpat-tARsV81umLzsJSkWxyEz"

29
src/cli.rs Normal file
View File

@ -0,0 +1,29 @@
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
pub(crate) struct Cli {
#[arg(short, long, value_name = "FILE")]
pub config: Option<PathBuf>,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
pub(crate) enum Commands {
#[command(subcommand)]
Planning(Planning)
}
#[derive(Subcommand)]
pub(crate) enum Planning {
I2work(Issue2Work)
}
#[derive(Args, Debug)]
pub(crate) struct Issue2Work {
pub issue_url: String
}

11
src/config.rs Normal file
View File

@ -0,0 +1,11 @@
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub(crate) struct Config {
pub gitlab: GitlabConfig,
}
#[derive(Deserialize, Debug)]
pub(crate) struct GitlabConfig {
pub token: String,
}

35
src/main.rs Normal file
View File

@ -0,0 +1,35 @@
extern crate serde;
extern crate clap;
mod cli;
mod config;
mod planning;
use std::fs;
use std::path::PathBuf;
use clap::Parser;
use cli::Cli;
use config::Config;
use crate::cli::Commands::Planning;
use crate::cli::Planning::I2work;
fn main() {
let cli = Cli::parse();
let default_config_path = PathBuf::from("/etc/config.toml");
let config_path = match cli.config.as_deref() {
Some(p) => p,
None => &default_config_path
};
let config_path_content = fs::read_to_string(config_path)
.expect("Could not read config file");
let config: Config = toml::from_str(&*config_path_content).expect("Could not parse config");
match cli.command {
Some(Planning(I2work(args))) => {
planning::issue2work::issue2work(config, &args);
},
None => {}
}
}

View File

@ -0,0 +1,60 @@
use crate::cli::Issue2Work;
use crate::config::Config;
use gitlab::{Gitlab, Issue};
use gitlab::api::{Query, issues};
use url::Url;
#[derive(Debug)]
struct IssueInfo {
project: String,
iid: u64,
}
pub(crate) fn issue2work(config: Config, args: &Issue2Work) {
let url = Url::parse(&*args.issue_url)
.expect("issue_url is not valid");
let data = extract_issue_info(&url).unwrap();
let client = Gitlab::new("gitlab.com", config.gitlab.token).unwrap();
let endpoint = issues::ProjectIssues::builder()
.iid(data.iid)
.project(String::from(data.project))
.build()
.unwrap();
let issue: Vec<Issue> = endpoint.query(&client).unwrap();
println!("{:?}", issue.first());
}
fn extract_issue_info(url: &Url) -> Option<IssueInfo> {
let parts = url.path_segments().expect("Could not parse path segment of given url");
let mut project_url: Vec<String> = Vec::with_capacity(3);
let mut iid: Option<String> = None;
let mut project_found = false;
for el in parts {
if el == "-" {
project_found = true;
continue;
}
if el == "issues" {
continue
}
if !project_found {
project_url.push(String::from(el));
} else {
// must be the id
iid = Some(String::from(el));
break;
}
}
Some(IssueInfo {
project: project_url.join("/"),
iid: iid.expect("iid of the issue not found")
.parse().expect("could not transform issue id to u64")
})
}

1
src/planning/mod.rs Normal file
View File

@ -0,0 +1 @@
pub(crate) mod issue2work;