first cli (wip)
This commit is contained in:
commit
4725e096f4
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
.idea/*
|
1483
Cargo.lock
generated
Normal file
1483
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
Normal file
13
Cargo.toml
Normal 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
2
config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[gitlab]
|
||||
token = "glpat-tARsV81umLzsJSkWxyEz"
|
29
src/cli.rs
Normal file
29
src/cli.rs
Normal 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
11
src/config.rs
Normal 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
35
src/main.rs
Normal 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 => {}
|
||||
}
|
||||
}
|
60
src/planning/issue2work.rs
Normal file
60
src/planning/issue2work.rs
Normal 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
1
src/planning/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub(crate) mod issue2work;
|
Loading…
Reference in New Issue
Block a user