Skip to content

Commit

Permalink
thingies
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Aug 9, 2024
1 parent 21c789f commit 47dae95
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 17 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ ratatui = "0.27.0"
tokio-tungstenite = "0.23.1"
log = "0.4.22"
env_logger = "0.11.3"
toml_edit = "0.22"
Empty file.
15 changes: 15 additions & 0 deletions src/api/models/addon/addon_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ pub enum AddonType {
filename: String,
},
}

impl ToString for AddonType {
fn to_string(&self) -> String {
match self {
AddonType::Url { url } => format!("Url [{url}]"),
AddonType::Modrinth { id, version } => format!("Modrinth/{id} [{version}]"),
AddonType::Curseforge { id, version } => format!("Curseforge/{id} [{version}]"),
AddonType::Spigot { id, version } => format!("Spigot/{id} [{version}]"),
AddonType::Hangar { id, version } => format!("Hangar/{id} [{version}]"),
AddonType::GithubRelease { repo, version, filename } => format!("Github/{repo} [{version}; {filename}]"),
AddonType::Jenkins { url, job, build, artifact } => format!("Jenkins/{job} [{build}; {artifact}]"),

Check warning on line 70 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `url`

warning: unused variable: `url` --> src/api/models/addon/addon_type.rs:70:34 | 70 | AddonType::Jenkins { url, job, build, artifact } => format!("Jenkins/{job} [{build}; {artifact}]"), | ^^^ help: try ignoring the field: `url: _`
AddonType::MavenArtifact { url, group, artifact, version, filename } => format!("Maven/{group}.{artifact} [{version}; {filename}]"),

Check warning on line 71 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `url`

warning: unused variable: `url` --> src/api/models/addon/addon_type.rs:71:40 | 71 | ... AddonType::MavenArtifact { url, group, artifact, version, filename } => format!("Maven/{group}.{artifact} [{version}; {filename}]"), | ^^^ help: try ignoring the field: `url: _`
}
}
}

Check failure on line 74 in src/api/models/addon/addon_type.rs

View workflow job for this annotation

GitHub Actions / clippy

direct implementation of `ToString`

error: direct implementation of `ToString` --> src/api/models/addon/addon_type.rs:61:1 | 61 | / impl ToString for AddonType { 62 | | fn to_string(&self) -> String { 63 | | match self { 64 | | AddonType::Url { url } => format!("Url [{url}]"), ... | 73 | | } 74 | | } | |_^ | = help: prefer implementing `Display` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_trait_impl
6 changes: 4 additions & 2 deletions src/api/models/modpack_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::api::utils::accessor::Accessor;
pub enum ModpackSource {
Local {
path: String,
#[serde(default)]
can_update: bool,
},

Remote {
Expand All @@ -25,15 +27,15 @@ impl FromStr for ModpackSource {
if s.starts_with("http") {
Ok(ModpackSource::Remote { url: s.into() })
} else {
Ok(ModpackSource::Local { path: s.into() })
Ok(ModpackSource::Local { path: s.into(), can_update: false })
}
}
}

impl ModpackSource {
pub fn accessor(&self, base: &Path) -> Result<Accessor> {
let str = match self {
Self::Local { path } => &base.join(path)
Self::Local { path, .. } => &base.join(path)
.canonicalize()
.with_context(|| format!("Resolving path: {:?}", base.join(path)))?
.to_string_lossy()
Expand Down
2 changes: 1 addition & 1 deletion src/api/models/mrpack/mrpack_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::api::{models::Environment, utils::hashing::HashFormat};
use crate::api::utils::hashing::HashFormat;

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down
10 changes: 5 additions & 5 deletions src/api/models/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum SourceType {

Modpack {
#[serde(flatten)]
modpack: ModpackSource,
modpack_source: ModpackSource,
modpack_type: ModpackType,
},
}
Expand All @@ -39,8 +39,8 @@ impl FromStr for SourceType {

Ok(match ty {
"file" | "f" => SourceType::File { path: val.into() },
"packwiz" | "pw" => SourceType::Modpack { modpack: ModpackSource::from_str(val)?, modpack_type: ModpackType::Packwiz },
"mrpack" => SourceType::Modpack { modpack: ModpackSource::from_str(val)?, modpack_type: ModpackType::MRPack },
"packwiz" | "pw" => SourceType::Modpack { modpack_source: ModpackSource::from_str(val)?, modpack_type: ModpackType::Packwiz },
"mrpack" => SourceType::Modpack { modpack_source: ModpackSource::from_str(val)?, modpack_type: ModpackType::MRPack },
_ => bail!("Unknown source identifier type: {ty}"),
})
}
Expand All @@ -61,7 +61,7 @@ impl Source {
.with_context(|| format!("Resolving path: {:?}", relative_to.join(path)))?)),
SourceType::Folder { path } => Ok(Accessor::Local(relative_to.join(path).canonicalize()
.with_context(|| format!("Resolving path: {:?}", relative_to.join(path)))?)),
SourceType::Modpack { modpack, .. } => modpack.accessor(relative_to)
SourceType::Modpack { modpack_source: modpack, .. } => modpack.accessor(relative_to)
.with_context(|| "Getting Modpack Accessor"),
}
}
Expand All @@ -84,7 +84,7 @@ impl Source {

SourceType::Folder { .. } => Ok(vec![]),

SourceType::Modpack { modpack, modpack_type } => {
SourceType::Modpack { modpack_source: modpack, modpack_type } => {
let accessor = modpack.accessor(relative_to)?;
match modpack_type {
ModpackType::MRPack => resolve_mrpack_addons(app, accessor).await,
Expand Down
1 change: 1 addition & 0 deletions src/api/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod zip;
pub mod toml;
pub mod fs;
pub mod logger;
pub mod update_writer;
45 changes: 45 additions & 0 deletions src/api/utils/update_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Result};

Check warning on line 3 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `anyhow`

warning: unused import: `anyhow` --> src/api/utils/update_writer.rs:3:14 | 3 | use anyhow::{anyhow, Result}; | ^^^^^^
use toml_edit::DocumentMut;
use zip::ZipArchive;

use crate::api::{app::App, models::{mrpack::MRPackIndex, packwiz::{PackwizPack, PackwizPackIndex}, ModpackSource, ModpackType, Source, SourceType}};

Check warning on line 7 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `ModpackSource`, `ModpackType`, `SourceType`, and `app::App`

warning: unused imports: `ModpackSource`, `ModpackType`, `SourceType`, and `app::App` --> src/api/utils/update_writer.rs:7:18 | 7 | ...::{app::App, models::{mrpack::MRPackIndex, packwiz::{PackwizPack, PackwizPackIndex}, ModpackSource, ModpackType, Source, SourceType}}; | ^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^

use super::toml::read_toml;

Check warning on line 9 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `super::toml::read_toml`

warning: unused import: `super::toml::read_toml` --> src/api/utils/update_writer.rs:9:5 | 9 | use super::toml::read_toml; | ^^^^^^^^^^^^^^^^^^^^^^

/// An `Accessor` allows for filesystem, remote or zip file access.
pub enum UpdateWriter {

Check warning on line 12 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

enum `UpdateWriter` is never used

warning: enum `UpdateWriter` is never used --> src/api/utils/update_writer.rs:12:10 | 12 | pub enum UpdateWriter { | ^^^^^^^^^^^^
File(PathBuf, DocumentMut),
MRPack(PathBuf, ZipArchive<std::fs::File>, MRPackIndex),
Packwiz(PathBuf, PackwizPack, PackwizPackIndex),
}

impl UpdateWriter {
pub fn from(source: &Source, relative_to: &Path) -> Result<Self> {

Check warning on line 19 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

associated function `from` is never used

warning: associated function `from` is never used --> src/api/utils/update_writer.rs:19:12 | 18 | impl UpdateWriter { | ----------------- associated function in this implementation 19 | pub fn from(source: &Source, relative_to: &Path) -> Result<Self> { | ^^^^

Check warning on line 19 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `relative_to`

warning: unused variable: `relative_to` --> src/api/utils/update_writer.rs:19:34 | 19 | pub fn from(source: &Source, relative_to: &Path) -> Result<Self> { | ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_relative_to`

Check warning on line 19 in src/api/utils/update_writer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `source`

warning: unused variable: `source` --> src/api/utils/update_writer.rs:19:17 | 19 | pub fn from(source: &Source, relative_to: &Path) -> Result<Self> { | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_source`
todo!();

/* match &source.source_type {
SourceType::File { path } => Ok(Self::File(path, read_toml(relative_to.join(path).to_path_buf())?)),
SourceType::Folder { path } => unimplemented!(),
SourceType::Modpack {
modpack_source: ModpackSource::Local { path, can_update: true },
modpack_type
} => match modpack_type {
ModpackType::MRPack => Ok(Self::MRPack(
relative_to.join(path).to_path_buf(),
ZipArchive::new(std::fs::File::open(relative_to.join(path))?)?,
todo!()
)),
ModpackType::Packwiz => Ok(Self::Packwiz(
relative_to.join(path),
todo!(),
todo!(),
)),
ModpackType::Unsup => unimplemented!(),
},
_ => Err(anyhow!("Can't make an UpdateWriter")),
} */
}

}
21 changes: 21 additions & 0 deletions src/commands/export/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::sync::Arc;

use anyhow::Result;

use crate::api::app::App;

pub mod mrpack;
pub mod packwiz;

#[derive(clap::Subcommand)]
pub enum Commands {
MRPack(mrpack::Args),
Packwiz(packwiz::Args),
}

pub async fn run(app: Arc<App>, args: Commands) -> Result<()> {
match args {
Commands::MRPack(args) => mrpack::run(app, args).await,
Commands::Packwiz(args) => packwiz::run(app, args).await,
}
}
16 changes: 16 additions & 0 deletions src/commands/export/mrpack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::sync::Arc;

use anyhow::Result;

use crate::api::app::App;

#[derive(clap::Args)]
pub struct Args {
filename: Option<String>,
}

pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 12 in src/commands/export/mrpack.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/export/mrpack.rs:12:33 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`

Check warning on line 12 in src/commands/export/mrpack.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/commands/export/mrpack.rs:12:18 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^ help: if this is intentional, prefix it with an underscore: `_app`


Ok(())
}

Check warning on line 16 in src/commands/export/mrpack.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `async` for function with no await statements

warning: unused `async` for function with no await statements --> src/commands/export/mrpack.rs:12:1 | 12 | / pub async fn run(app: Arc<App>, args: Args) -> Result<()> { 13 | | 14 | | 15 | | Ok(()) 16 | | } | |_^ | = help: consider removing the `async` from this function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
16 changes: 16 additions & 0 deletions src/commands/export/packwiz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::sync::Arc;

use anyhow::Result;

use crate::api::app::App;

#[derive(clap::Args)]
pub struct Args {

}

pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 12 in src/commands/export/packwiz.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/export/packwiz.rs:12:33 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`

Check warning on line 12 in src/commands/export/packwiz.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/commands/export/packwiz.rs:12:18 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^ help: if this is intentional, prefix it with an underscore: `_app`


Ok(())
}

Check warning on line 16 in src/commands/export/packwiz.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `async` for function with no await statements

warning: unused `async` for function with no await statements --> src/commands/export/packwiz.rs:12:1 | 12 | / pub async fn run(app: Arc<App>, args: Args) -> Result<()> { 13 | | 14 | | 15 | | Ok(()) 16 | | } | |_^ | = help: consider removing the `async` from this function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod migrate;
pub mod websocket;
pub mod run;
pub mod update;
pub mod export;
28 changes: 25 additions & 3 deletions src/commands/sources/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@ use std::sync::Arc;
use anyhow::Result;
use console::style;

use crate::api::{app::App, models::{ModpackSource, SourceType}};
use crate::api::{app::App, models::SourceType};

#[derive(clap::Args)]
pub struct Args {}
pub struct Args {
#[arg(short = 'a', long)]
pub with_addons: bool,
}

pub async fn run(app: Arc<App>, args: Args) -> Result<()> {
if let Some((base, network)) = &*app.network.read().await {
println!("{} {}", style("Network:").bold(), network.name);
println!(" -> {:?}", style(base.parent().unwrap()).dim());
println!();
};

if let Some((base, server)) = &*app.server.read().await {
println!("{} {}", style("Server:").bold(), server.name);
println!(" -> {:?}", style(base.parent().unwrap()).dim());
};

println!();

let sources = app.collect_sources().await?;

for (idx, (base, source)) in sources.iter().enumerate() {
Expand All @@ -24,7 +40,13 @@ pub async fn run(app: Arc<App>, args: Args) -> Result<()> {
}
);

println!(" -> {}", style(source.accessor(base)?.to_string()).dim())
println!(" -> {}", style(source.accessor(base)?.to_string()).dim());

if args.with_addons {
for (idx, addon) in source.resolve_addons(&app, base).await?.iter().enumerate() {
println!(" {}. {} {}", style(idx).bold(), addon.addon_type.to_string(), style(addon.target.as_str()).dim());
}
}
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::sync::Arc;

use anyhow::Result;

use crate::api::{app::App, tools::git::version_check};
use crate::api::{app::App, tools::git};

#[derive(clap::Args)]
pub struct Args {

}

pub async fn run(app: Arc<App>, args: Args) -> Result<()> {

Check warning on line 12 in src/commands/update.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `args`

warning: unused variable: `args` --> src/commands/update.rs:12:33 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_args`

Check warning on line 12 in src/commands/update.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `app`

warning: unused variable: `app` --> src/commands/update.rs:12:18 | 12 | pub async fn run(app: Arc<App>, args: Args) -> Result<()> { | ^^^ help: if this is intentional, prefix it with an underscore: `_app`
println!("{:#?}", version_check());
println!("{:#?}", git::version_check());

Ok(())
}

Check warning on line 16 in src/commands/update.rs

View workflow job for this annotation

GitHub Actions / clippy

unused `async` for function with no await statements

warning: unused `async` for function with no await statements --> src/commands/update.rs:12:1 | 12 | / pub async fn run(app: Arc<App>, args: Args) -> Result<()> { 13 | | println!("{:#?}", git::version_check()); 14 | | 15 | | Ok(()) 16 | | } | |_^ | = help: consider removing the `async` from this function = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_async
Expand Down
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;
use std::{path::Path, sync::Arc};

Check warning on line 1 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `path::Path`

warning: unused import: `path::Path` --> src/main.rs:1:11 | 1 | use std::{path::Path, sync::Arc}; | ^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

use anyhow::Result;
use api::{app::App, models::{server::{Server, SERVER_TOML}, Source, SourceType}, utils::logger::init_logger};
use api::{app::App, models::{packwiz::{PackwizPack, PACK_TOML}, server::{Server, SERVER_TOML}, ModpackSource, ModpackType, Source, SourceType}, utils::{logger::init_logger, toml::try_find_toml_upwards}};
use clap::Parser;

mod api;
Expand Down Expand Up @@ -32,6 +32,8 @@ enum Commands {
Java(commands::java::Commands),
#[command(alias = "md", subcommand)]
Markdown(commands::markdown::Commands),
#[command(subcommand)]
Export(commands::export::Commands),
Migrate(commands::migrate::Args),
#[command(alias = "ws")]
WebSocket(commands::websocket::Args),
Expand Down Expand Up @@ -61,6 +63,23 @@ async fn main() -> Result<()> {
}
}

if let Ok(Some((base, _))) = try_find_toml_upwards::<PackwizPack>(PACK_TOML) {
let mut wg = app.server.write().await;
// if no server.toml etc and is inside a packwiz folder
if wg.is_none() {
let (_, server) = wg.get_or_insert_with(|| (
base.into(),

Check failure on line 71 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::path::PathBuf`

error: useless conversion to the same type: `std::path::PathBuf` --> src/main.rs:71:17 | 71 | base.into(), | ^^^^^^^^^^^ help: consider removing `.into()`: `base` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
Server::default()
));
server.sources.push(Source {
source_type: SourceType::Modpack {
modpack_source: ModpackSource::Local { path: String::from(PACK_TOML), can_update: false },
modpack_type: ModpackType::Packwiz,
}
});
}
}

match args.command {
Commands::Init(args) => commands::init::run(app, args).await,
Commands::Sources(args) => commands::sources::run(app, args).await,
Expand All @@ -71,5 +90,6 @@ async fn main() -> Result<()> {
Commands::Migrate(args) => commands::migrate::run(app, args).await,
Commands::WebSocket(args) => commands::websocket::run(app, args).await,
Commands::Update(args) => commands::update::run(app, args).await,
Commands::Export(args) => commands::export::run(app, args).await,
}
}

0 comments on commit 47dae95

Please sign in to comment.