gen_circleci_orb/commands/
init.rs1use anyhow::Result;
2use std::path::PathBuf;
3
4use crate::{ci_patcher, commands::generate::Generate};
5
6pub const DEFAULT_DOCKER_ORB_VERSION: &str = "3.0.1";
7
8#[derive(Debug, clap::Args)]
10pub struct Init {
11 #[arg(long)]
13 pub binary: String,
14
15 #[arg(long = "public-orb-namespace")]
18 pub public_orb_namespaces: Vec<String>,
19
20 #[arg(long = "private-orb-namespace")]
24 pub private_orb_namespaces: Vec<String>,
25
26 #[arg(long)]
28 pub build_workflow: String,
29
30 #[arg(long)]
32 pub release_workflow: String,
33
34 #[arg(long)]
36 pub requires_job: Option<String>,
37
38 #[arg(long)]
43 pub release_after_job: String,
44
45 #[arg(long, default_value = "orb")]
47 pub orb_dir: String,
48
49 #[arg(long, default_value = ".circleci")]
51 pub ci_dir: PathBuf,
52
53 #[arg(long, default_value = "12.3.3")]
55 pub orb_tools_version: String,
56
57 #[arg(long, default_value = DEFAULT_DOCKER_ORB_VERSION)]
59 pub docker_orb_version: String,
60
61 #[arg(long)]
63 pub docker_namespace: String,
64
65 #[arg(long, default_value = "docker-credentials")]
67 pub docker_context: String,
68
69 #[arg(long, default_value = "orb-publishing")]
71 pub orb_context: String,
72
73 #[arg(long)]
75 pub mcp: bool,
76
77 #[arg(long)]
79 pub dry_run: bool,
80}
81
82impl Init {
83 pub fn run(&self) -> Result<()> {
84 let namespaces: Vec<String> = self
85 .public_orb_namespaces
86 .iter()
87 .chain(self.private_orb_namespaces.iter())
88 .cloned()
89 .collect();
90
91 tracing::info!("Generating orb source into ./{}", self.orb_dir);
93 let gen = Generate {
94 binary: self.binary.clone(),
95 namespaces: namespaces.clone(),
96 output: PathBuf::from("."),
97 orb_dir: self.orb_dir.clone(),
98 install_method: crate::commands::generate::InstallMethod::Binstall,
99 base_image: crate::commands::generate::DEFAULT_BASE_IMAGE.to_string(),
100 home_url: None,
101 source_url: None,
102 dry_run: self.dry_run,
103 };
104 gen.run()?;
105
106 let opts = ci_patcher::PatchOpts {
108 binary: self.binary.clone(),
109 namespaces,
110 docker_namespace: self.docker_namespace.clone(),
111 orb_dir: self.orb_dir.clone(),
112 build_workflow: self.build_workflow.clone(),
113 release_workflow: self.release_workflow.clone(),
114 requires_job: self.requires_job.clone(),
115 release_after_job: self.release_after_job.clone(),
116 orb_tools_version: self.orb_tools_version.clone(),
117 docker_orb_version: self.docker_orb_version.clone(),
118 docker_context: self.docker_context.clone(),
119 orb_context: self.orb_context.clone(),
120 private_namespaces: self.private_orb_namespaces.clone(),
121 mcp: self.mcp,
122 };
123
124 let summary = ci_patcher::apply_patches(&self.ci_dir, &opts, self.dry_run)?;
125 for line in &summary {
126 println!("{line}");
127 }
128
129 if self.dry_run {
130 println!("(dry-run: no files written)");
131 } else {
132 println!("Done.");
133 }
134
135 Ok(())
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use super::DEFAULT_DOCKER_ORB_VERSION;
142
143 #[test]
144 fn default_docker_orb_version_matches_registry() {
145 assert_eq!(
148 DEFAULT_DOCKER_ORB_VERSION, "3.0.1",
149 "DEFAULT_DOCKER_ORB_VERSION must be the registry-available version"
150 );
151 }
152}