Skip to content

Commit 2a89787

Browse files
authored
Add Init command (#10)
* added and implemented init * and_then * renamed to basalt * locking fr * uncommented web client thing * removed rust-version
1 parent 8ae83e8 commit 2a89787

File tree

6 files changed

+163
-27
lines changed

6 files changed

+163
-27
lines changed

Cargo.lock

Lines changed: 2 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[package]
2-
name = "basalt-cli"
2+
name = "basalt"
33
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
77
anyhow = "1.0.95"
88
clap = { version = "4.5.27", features = ["derive"] }
9-
bedrock = { git = "http://github.com/basalt-rs/bedrock.git", rev = "f8ce557", features = ["tokio"] }
9+
bedrock = { git = "http://github.com/basalt-rs/bedrock.git", rev = "6bec1a2", features = [
10+
"tokio",
11+
] }
1012
tokio = { version = "1.43.0", features = ["full"] }
1113
lazy_static = "1.5.0"
1214
tera = "1.20.0"

data/default.toml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Generated by basalt-cli
2+
#
3+
# This basic example just includes a custom language and a supported language,
4+
# two competitors, a host, a single problem, and some sane runner configurations.
5+
#
6+
# There are also examples showing how you could instead import pieces for a multi-file config setup.
7+
#
8+
# Happy hacking!
9+
10+
port = 8517
11+
12+
[setup]
13+
# import = "./setup.toml"
14+
15+
# install = { import = "./install.sh" }
16+
install = '''
17+
dnf install opam
18+
'''
19+
20+
# init = { import = "./init.sh" }
21+
init = '''
22+
opam init -y
23+
eval $(opam env)
24+
'''
25+
26+
[test_runner]
27+
# import = "./test-runner.toml"
28+
29+
timeout_ms = 60_000
30+
trim_output = true
31+
max_memory = { compile = 128, run = 64 }
32+
max_file_size = 8192
33+
34+
[languages]
35+
python3 = "latest"
36+
java = "21"
37+
ocaml = { build = "ocamlc -o out solution.ml", run = "./out", source_file = "solution.ml" }
38+
39+
[[accounts.hosts]]
40+
name = "host"
41+
password = "pwd1"
42+
43+
[[accounts.competitors]]
44+
name = "team1"
45+
password = "pwd1"
46+
47+
[[accounts.competitors]]
48+
name = "team2"
49+
password = "pwd1"
50+
51+
# Specify information about the packet itself
52+
[packet]
53+
# import = "./packet.toml"
54+
title = "{{ name }}"
55+
preamble = '''
56+
This packet includes problems of a difficulty *vastly*
57+
surpassing the capabilities of the average computer
58+
science student. Be wary as these problems will
59+
certainly give you great intellectual trouble. There
60+
is little hope for anyone without a Ph.D in computer
61+
science.
62+
63+
If you decide to attempt these problems anyways, good
64+
luck. You will be rewarded for swiftness in your answers.
65+
'''
66+
67+
[[packet.problems]]
68+
# import = "./problem1.toml"
69+
title = "Reversing a string"
70+
description = '''
71+
Reversing a string is one of the most *basic* algorithmic
72+
problems for a beginner computer science student to solve.
73+
74+
Solve it.
75+
'''
76+
77+
[[packet.problems.tests]]
78+
input = "hello"
79+
output = "olleh"
80+
visible = true
81+
82+
[[packet.problems.tests]]
83+
input = "world"
84+
output = "dlrow"
85+
visible = true
86+
87+
[[packet.problems.tests]]
88+
input = ""
89+
output = ""
90+
91+
[[packet.problems.tests]]
92+
input = "aa"
93+
output = "aa"
94+
95+
[[packet.problems.tests]]
96+
input = "racecar"
97+
output = "racecar"

src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub enum SubCmd {
1818
/// The configuration file to verify
1919
config_file: PathBuf,
2020
},
21+
Init {
22+
#[arg()]
23+
path: Option<PathBuf>,
24+
},
2125
/// Build the docker file based on a given configuration file
2226
Build {
2327
/// Specifies tag for docker image. Not recommended unless you're familiar with Docker.

src/init.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::path::PathBuf;
2+
3+
use anyhow::Context;
4+
use lazy_static::lazy_static;
5+
6+
const BASE_DEFAULT_CONFIG: &str = include_str!("../data/default.toml");
7+
8+
lazy_static! {
9+
static ref tmpl: tera::Tera = {
10+
let mut t = tera::Tera::default();
11+
t.add_raw_template("template", BASE_DEFAULT_CONFIG)
12+
.expect("Failed to register docker source template");
13+
t
14+
};
15+
}
16+
17+
pub async fn handle(path: Option<PathBuf>) -> anyhow::Result<()> {
18+
// configuration name should equal the filename in the path provided or default to `basalt` if no path is provided or the filename is empty.
19+
let name = if let Some(mut path) = path.clone() {
20+
path.set_extension("");
21+
path.file_name()
22+
.and_then(|f| f.to_str())
23+
.map_or("basalt", |f| {
24+
if f.trim().is_empty() {
25+
"basalt"
26+
} else {
27+
f.trim()
28+
}
29+
})
30+
.to_owned()
31+
} else {
32+
"basalt".to_owned()
33+
};
34+
35+
let path = path.map_or(PathBuf::from(&name), |p| {
36+
if p.file_name().is_some() {
37+
p
38+
} else {
39+
p.with_file_name(name.clone()).with_extension("toml")
40+
}
41+
});
42+
43+
let mut ctx = tera::Context::new();
44+
ctx.insert("name", &name);
45+
let content = tmpl
46+
.render("template", &ctx)
47+
.context("Failed to render template")?;
48+
49+
tokio::fs::write(path, content)
50+
.await
51+
.context("Failed to write data")?;
52+
53+
Ok(())
54+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod build;
22
mod cli;
3+
mod init;
34
use std::{ffi::OsStr, path::Path, process};
45

56
use ansi_term::Colour::{Blue, Green};
@@ -46,6 +47,7 @@ async fn main() -> anyhow::Result<()> {
4647

4748
match cli.subcommand {
4849
cli::SubCmd::Verify { config_file } => verify(&config_file).await?,
50+
cli::SubCmd::Init { path } => init::handle(path).await?,
4951
cli::SubCmd::Build {
5052
tag,
5153
output,

0 commit comments

Comments
 (0)