Skip to content

Commit

Permalink
Try to rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
rikonaka committed Sep 10, 2024
1 parent 8aa541d commit 7835d89
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.x86_64-unknown-linux-gnu]
runner = 'sudo -E'

[target.aarch64-unknown-linux-gnu]
runner = 'sudo -E'
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[package]
name = "aoit"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "^1"
clap = { version = "^4", features = ["derive"] }
glob = "^0"
serde = { version = "^1", features = ["derive"] }
serde_json = "^1"
sevenz-rust={version="^0", features=["compress"]}
sha2 = "^0"
log = "^0"
env_logger = "^0"
20 changes: 2 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;

mod install;
Expand Down Expand Up @@ -28,23 +29,6 @@ pub struct SerdeConfig {
data: Vec<HashMap<String, String>>,
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
#[test]
fn append() {
let mut a = vec![0, 1, 2];
let mut b = vec![];
a.append(&mut b);
// println!("{:?}", a);
assert_eq!(a, vec![0, 1, 2]);
}
}

fn main() {
let args = Args::parse();
if args.pack != "null" {
Expand Down
85 changes: 82 additions & 3 deletions src/pack.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,73 @@
use anyhow::Result;
use glob::glob;
use log::debug;
use log::error;
use log::info;
use log::warn;
use serde::Deserialize;
use serde::Serialize;
use sevenz_rust;
use std::{collections::HashMap, process::Command};
use std::collections::HashMap;
use std::process::Command;

use crate::utils;
use crate::SerdeConfig;
use crate::DEFAULT_CONFIG_NAME;
use crate::DEFAULT_PACKAGE_SUFFIX;
use crate::DEFAULT_SHA256_SUFFIX;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AptDepends {
pub name: String,
pub depends: Vec<AptDepends>,
}

impl AptDepends {
pub fn new(name: &str, depends: &[AptDepends]) -> AptDepends {
AptDepends {
name: name.to_string(),
depends: depends.to_vec(),
}
}
}

fn apt_cache_depends_parser(package_name: &str) -> Result<Vec<AptDepends>> {
debug!("apt cache depends parser: {package_name}");
println!("apt cache depends parser: {package_name}");
let command = Command::new("apt-cache")
.arg("depends")
.arg(package_name)
.output()?;

let mut ret = Vec::new();
let command_output = String::from_utf8_lossy(&command.stdout);
for line in command_output.lines() {
if line.contains("Depends:") && !line.contains("|") {
let line_split: Vec<&str> = line
.split(":")
.map(|x| x.trim())
.filter(|x| x.len() > 0)
.collect();

if line_split.len() == 2 {
let depends_package_name = line_split[1];
let depends = apt_cache_depends_parser(depends_package_name)?;
let apt_depends = AptDepends::new(depends_package_name, &depends);
ret.push(apt_depends);
}
}
}

Ok(ret)
}

fn resolve_depends_new(package_name: &str) -> Result<AptDepends> {
println!("apt cache depends parser: {package_name}");
let root_depends = apt_cache_depends_parser(package_name)?;
let root = AptDepends::new(&package_name, &root_depends);
Ok(root)
}

fn resolve_depends(package_name: &str) -> Option<Vec<String>> {
let mut depend_vec: Vec<String> = Vec::new();
let command = Command::new("apt-cache")
Expand Down Expand Up @@ -74,9 +134,9 @@ pub fn pack_deb(package_name: &str) {
// let target_dir = format!("./{}", package_name);
let target_dir = package_name;
match utils::create_dir(&target_dir) {
true => println!("Create tmp dir success..."),
true => println!("Create tmp dir success!"),
false => {
println!("Create tmp dir failed...");
println!("Create tmp dir failed!");
return;
}
}
Expand Down Expand Up @@ -139,3 +199,22 @@ pub fn pack_deb(package_name: &str) {
utils::remove_dir(target_dir);
println!("Done");
}

#[cfg(test)]
mod tests {
use super::*;
use env_logger;
#[test]
fn test_resolve_depends() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.is_test(true)
.try_init()
.unwrap();

println!("Test");
let package_name = "postgresql"; // for test
let ret = resolve_depends_new(package_name).unwrap();
println!("{:?}", ret);
}
}

0 comments on commit 7835d89

Please sign in to comment.