docs: add examples
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-11 19:46:08 +02:00
parent 7f01e8c674
commit 3662b343b6
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 231 additions and 47 deletions

10
Cargo.lock generated
View File

@ -672,6 +672,16 @@ version = "0.27.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e"
[[package]]
name = "gitea-cuddle-file-cuddle-please"
version = "0.1.0"
dependencies = [
"dagger-cuddle-please",
"dagger-sdk",
"eyre",
"tokio",
]
[[package]]
name = "gitea-cuddle-please"
version = "0.1.0"

View File

@ -1,60 +1,112 @@
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct CuddlePleaseArgs {
pub repository: String,
pub owner: String,
pub branch: String,
use models::{CuddlePleaseArgs, CuddlePleaseSrcArgs};
use traits::CuddlePlease;
pub cuddle_image: String,
pub server: Server,
use crate::models::{LogLevel, Server, SrcServer};
pub log_level: Option<LogLevel>,
pub mod models {
#[derive(Clone, Debug)]
pub struct CuddlePleaseArgs {
/// Which repository, example: "dagger-components"
pub repository: String,
pub use_ssh_socket: bool,
/// Whom owns the repository, example: "kjuulh"
pub owner: String,
/// which branch to fetch, example: "main"
pub branch: String,
/// Which image to use for cuddle-please, example: "docker.io/kasperhermansen/cuddle-please:latest"
pub cuddle_image: String,
/// Which server to use, see inner field for more docs
pub server: Server,
/// Which log level to output for cuddle-please. Defaults to INFO
pub log_level: Option<LogLevel>,
/// Whether to include an ssh socket, if not set will default to the token set in server and http/https depending on the server
pub use_ssh_socket: bool,
}
#[derive(Clone, Debug)]
pub struct CuddlePleaseSrcArgs {
/// Which image to use for cuddle-please, example: "docker.io/kasperhermansen/cuddle-please:latest"
pub cuddle_image: String,
/// Which log level to output for cuddle-please. Defaults to INFO
pub log_level: Option<LogLevel>,
/// Which server to use, see inner field for more docs
pub server: SrcServer,
/// Whether to include an ssh socket, if not set will default to the token set in server and http/https depending on the server
pub use_ssh_socket: bool,
}
#[derive(Clone, Debug)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
#[derive(Clone, Debug)]
pub enum Server {
Gitea {
url: String,
user: String,
token: String,
insecure: Option<bool>,
},
GitHub {
token: String,
},
}
#[derive(Clone, Debug)]
pub enum SrcServer {
Gitea { token: String },
GitHub { token: String },
}
}
#[derive(Clone, Debug)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
pub mod traits {
use crate::models::{CuddlePleaseArgs, CuddlePleaseSrcArgs};
#[derive(Clone, Debug)]
pub enum Server {
Gitea {
url: String,
user: String,
token: String,
insecure: Option<bool>,
},
GitHub {
token: String,
},
}
#[async_trait::async_trait]
pub trait CuddlePlease {
async fn execute(&self, args: &CuddlePleaseArgs) -> eyre::Result<()>;
#[async_trait::async_trait]
pub trait CuddlePlease {
async fn execute(&self, args: &CuddlePleaseArgs) -> eyre::Result<()>;
async fn execute_src(&self, args: &CuddlePleaseSrcArgs) -> eyre::Result<()>;
}
}
pub struct DaggerCuddlePleaseAction(Arc<dyn CuddlePlease + Send + Sync + 'static>);
impl DaggerCuddlePleaseAction {
/// Create a [`traits::CuddlePlease`] client based on dagger
pub fn dagger(client: Arc<dagger_sdk::Query>) -> Self {
Self(Arc::new(DaggerCuddlePlease::new(client)))
}
/// Executes the cuddle-please action. This is a slow operation, but async, so make sure to not spawn blocking
/// This relies exclively on arguments, as such this pull the repository directly, instead of using local src
pub async fn execute(&self, args: &CuddlePleaseArgs) -> eyre::Result<()> {
self.0.execute(args).await
}
/// Executes the cuddle-please action. This is a slow operation, but async, so make sure to not spawn blocking
/// This function relies exclusively on cuddle.yaml and cuddle.please.yaml
/// For private repository access, make sure to use [`CuddlePleaseSrcArgs::use_ssh_agent`] otherwise we may not able to push to your repository
pub async fn execute_src(&self, args: &CuddlePleaseSrcArgs) -> eyre::Result<()> {
self.0.execute_src(args).await
}
}
#[derive(Clone)]
pub struct DaggerCuddlePlease {
struct DaggerCuddlePlease {
client: Arc<dagger_sdk::Query>,
}
@ -63,6 +115,10 @@ impl CuddlePlease for DaggerCuddlePlease {
async fn execute(&self, args: &CuddlePleaseArgs) -> eyre::Result<()> {
self.cuddle_please(self.client.clone(), args).await
}
async fn execute_src(&self, args: &CuddlePleaseSrcArgs) -> eyre::Result<()> {
self.cuddle_please_src(self.client.clone(), args).await
}
}
impl DaggerCuddlePlease {
@ -106,7 +162,11 @@ impl DaggerCuddlePlease {
}
}
Server::GitHub { .. } => {
format!("https://github.com/{}/{}", &args.owner, &args.repository)
if args.use_ssh_socket {
format!("git@github.com:{}/{}", &args.owner, &args.repository)
} else {
format!("https://github.com/{}/{}", &args.owner, &args.repository)
}
}
};
let api_url = match &args.server {
@ -124,16 +184,36 @@ impl DaggerCuddlePlease {
Server::GitHub { .. } => "https://github.com".into(),
};
let src = client
.git_opts(
&repo_url,
dagger_sdk::QueryGitOpts {
experimental_service_host: None,
keep_git_dir: Some(true),
},
)
.branch("main")
.tree();
let src = if args.use_ssh_socket {
let socket = client
.host()
.unix_socket(std::env::var("SSH_AGENT").expect("SSH_AGENT to be set"));
client
.git_opts(
&repo_url,
dagger_sdk::QueryGitOpts {
experimental_service_host: None,
keep_git_dir: Some(true),
},
)
.branch("main")
.tree_opts(dagger_sdk::GitRefTreeOpts {
ssh_auth_socket: Some(socket.id().await?),
ssh_known_hosts: None,
})
} else {
client
.git_opts(
&repo_url,
dagger_sdk::QueryGitOpts {
experimental_service_host: None,
keep_git_dir: Some(true),
},
)
.branch("main")
.tree()
};
let res = build_image
.with_secret_variable(
@ -190,6 +270,61 @@ impl DaggerCuddlePlease {
let please_out = res.stderr().await?;
println!("{please_out}");
Ok(())
}
pub async fn cuddle_please_src(
&self,
client: Arc<dagger_sdk::Query>,
args: &CuddlePleaseSrcArgs,
) -> eyre::Result<()> {
let build_image = client.container().from(&args.cuddle_image);
let res = build_image
.with_secret_variable(
"CUDDLE_PLEASE_TOKEN",
client
.set_secret(
"CUDDLE_PLEASE_TOKEN",
match &args.server {
SrcServer::Gitea { token, .. } => token,
SrcServer::GitHub { token } => token,
},
)
.id()
.await?,
)
.with_workdir("/mnt/app")
.with_directory(".", client.host().directory(".").id().await?)
.with_exec(vec![
"cuddle-please",
"release",
&format!(
"--engine={}",
match &args.server {
SrcServer::Gitea { .. } => "gitea",
SrcServer::GitHub { .. } => "github",
}
),
"--log-level",
match args.log_level.as_ref().unwrap_or(&LogLevel::Info) {
LogLevel::Trace => "trace",
LogLevel::Debug => "debug",
LogLevel::Info => "info",
LogLevel::Warn => "warn",
LogLevel::Error => "error",
},
]);
let exit_code = res.exit_code().await?;
if exit_code != 0 {
eyre::bail!("failed to run cuddle-please");
}
let please_out = res.stdout().await?;
println!("{please_out}");
let please_out = res.stderr().await?;
println!("{please_out}");
Ok(())
}
}

View File

@ -0,0 +1,13 @@
[package]
name = "gitea-cuddle-file-cuddle-please"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dagger-cuddle-please.workspace = true
eyre.workspace = true
dagger-sdk.workspace = true
tokio.workspace = true

View File

@ -0,0 +1,6 @@
project:
owner: kjuulh
repository: dagger-components
branch: main
settings:
api_url: https://git.front.kjuulh.io

View File

@ -0,0 +1,20 @@
use dagger_cuddle_please::{models::CuddlePleaseSrcArgs, DaggerCuddlePleaseAction};
#[tokio::main]
pub async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect().await?;
DaggerCuddlePleaseAction::dagger(client.clone())
.execute_src(&CuddlePleaseSrcArgs {
cuddle_image: "kasperhermansen/cuddle-please:main-1691504183".into(),
server: dagger_cuddle_please::models::SrcServer::Gitea {
token: std::env::var("CUDDLE_PLEASE_TOKEN")
.expect("CUDDLE_PLEASE_TOKEN to be present"),
},
log_level: None,
use_ssh_socket: true,
})
.await?;
Ok(())
}

View File

@ -1,4 +1,4 @@
use dagger_cuddle_please::{CuddlePleaseArgs, DaggerCuddlePleaseAction};
use dagger_cuddle_please::{models::CuddlePleaseArgs, DaggerCuddlePleaseAction};
#[tokio::main]
pub async fn main() -> eyre::Result<()> {
@ -10,7 +10,7 @@ pub async fn main() -> eyre::Result<()> {
owner: "kjuulh".into(),
branch: "main".into(),
cuddle_image: "kasperhermansen/cuddle-please:latest".into(),
server: dagger_cuddle_please::Server::Gitea {
server: dagger_cuddle_please::models::Server::Gitea {
url: "https://git.front.kjuulh.io".into(),
user: "git".into(),
token: std::env::var("CUDDLE_PLEASE_TOKEN")