From 3662b343b6be84ec2d9248a5e3382731195a7057 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 11 Aug 2023 19:46:08 +0200 Subject: [PATCH] docs: add examples Signed-off-by: kjuulh --- Cargo.lock | 10 + crates/dagger-cuddle-please/src/lib.rs | 225 ++++++++++++++---- .../Cargo.toml | 13 + .../cuddle.please.yaml | 6 + .../src/main.rs | 20 ++ examples/gitea-cuddle-please/src/main.rs | 4 +- 6 files changed, 231 insertions(+), 47 deletions(-) create mode 100644 examples/gitea-cuddle-file-cuddle-please/Cargo.toml create mode 100644 examples/gitea-cuddle-file-cuddle-please/cuddle.please.yaml create mode 100644 examples/gitea-cuddle-file-cuddle-please/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 409a6a6..acab2ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/dagger-cuddle-please/src/lib.rs b/crates/dagger-cuddle-please/src/lib.rs index e2720f5..3b4d4b9 100644 --- a/crates/dagger-cuddle-please/src/lib.rs +++ b/crates/dagger-cuddle-please/src/lib.rs @@ -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, +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, + + /// 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, + + /// 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, + }, + 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, - }, - 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); impl DaggerCuddlePleaseAction { + /// Create a [`traits::CuddlePlease`] client based on dagger pub fn dagger(client: Arc) -> 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, } @@ -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, + 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(()) } } diff --git a/examples/gitea-cuddle-file-cuddle-please/Cargo.toml b/examples/gitea-cuddle-file-cuddle-please/Cargo.toml new file mode 100644 index 0000000..3ce1bd9 --- /dev/null +++ b/examples/gitea-cuddle-file-cuddle-please/Cargo.toml @@ -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 diff --git a/examples/gitea-cuddle-file-cuddle-please/cuddle.please.yaml b/examples/gitea-cuddle-file-cuddle-please/cuddle.please.yaml new file mode 100644 index 0000000..4b21589 --- /dev/null +++ b/examples/gitea-cuddle-file-cuddle-please/cuddle.please.yaml @@ -0,0 +1,6 @@ +project: + owner: kjuulh + repository: dagger-components + branch: main +settings: + api_url: https://git.front.kjuulh.io diff --git a/examples/gitea-cuddle-file-cuddle-please/src/main.rs b/examples/gitea-cuddle-file-cuddle-please/src/main.rs new file mode 100644 index 0000000..17d112b --- /dev/null +++ b/examples/gitea-cuddle-file-cuddle-please/src/main.rs @@ -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(()) +} diff --git a/examples/gitea-cuddle-please/src/main.rs b/examples/gitea-cuddle-please/src/main.rs index e7bd7ee..f60698f 100644 --- a/examples/gitea-cuddle-please/src/main.rs +++ b/examples/gitea-cuddle-please/src/main.rs @@ -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")