Skip to content

Commit 7fea119

Browse files
dervoetinightkr
andauthored
feat: init product command for patchable (#1112)
* feat: init product command for patchable * Update rust/patchable/src/main.rs Co-authored-by: Natalie Klestrup Röijezon <[email protected]> * chore: un-nest match blocks * chore: rename InitType to InitCmd * feat: allow explicitly empty mirror URLs --------- Co-authored-by: Natalie Klestrup Röijezon <[email protected]>
1 parent 16808bc commit 7fea119

File tree

2 files changed

+102
-28
lines changed

2 files changed

+102
-28
lines changed

rust/patchable/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ The version-level config contains:
4848

4949
### Template
5050

51-
If you're adding a completely new product, you need to create the product-level config once:
51+
If you're adding a completely new product, you need to initialize the product-level config once using patchable:
5252

53-
```toml
54-
# docker-images/druid/stackable/patches/patchable.toml
55-
upstream = "https://github.com/apache/druid.git"
56-
mirror = "https://github.com/stackabletech/druid.git"
53+
```sh
54+
cargo patchable init product druid --upstream https://github.com/apache/druid.git --default-mirror https://github.com/stackabletech/druid.git
5755
```
5856

57+
This will create the product-level configuration in `docker-images/druid/stackable/patches/patchable.toml`.
58+
5959
If you just want to add a new version, initialize the version-level config with patchable:
6060

6161
```sh
62-
cargo patchable init druid 28.0.0 --base=druid-28.0.0 --mirror
62+
cargo patchable init version druid 28.0.0 --base druid-28.0.0 --mirror
6363
```
6464

65-
This will initialize the version-level config with the base commit hash and the default mirror URL from the product-level config.
65+
This will initialize the version-level config in `docker-images/druid/stackable/patches/28.0.0/patchable.toml` with the base commit hash and the default mirror URL from the product-level config.
6666
You can optionally provide the `--ssh` flag to use SSH instead of HTTPS for Git operations.
6767

6868
## Glossary

rust/patchable/src/main.rs

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,10 @@ enum Cmd {
180180
pv: ProductVersion,
181181
},
182182

183-
/// Creates a patchable.toml for a given product version
183+
/// Creates patchable.toml configuration files
184184
Init {
185-
#[clap(flatten)]
186-
pv: ProductVersion,
187-
188-
/// The upstream commit-ish (such as druid-28.0.0) that the patch series applies to
189-
///
190-
/// Refs (such as tags and branches) will be resolved to commit IDs.
191-
#[clap(long)]
192-
base: String,
193-
194-
/// Mirror the product version to the default mirror repository
195-
#[clap(long)]
196-
mirror: bool,
197-
198-
/// Use SSH for git operations
199-
#[clap(long)]
200-
ssh: bool,
185+
#[clap(subcommand)]
186+
init_type: InitCmd,
201187
},
202188

203189
/// Shows the patch directory for a given product version
@@ -218,6 +204,41 @@ enum Cmd {
218204
ImagesDir,
219205
}
220206

207+
#[derive(clap::Parser)]
208+
enum InitCmd {
209+
/// Creates a patchable.toml for a given product
210+
Product {
211+
/// The product name slug (such as druid)
212+
product: String,
213+
/// The upstream repository URL (e.g. https://github.com/apache/druid.git)
214+
#[clap(long)]
215+
upstream: String,
216+
/// The default mirror repository URL (e.g. https://github.com/stackabletech/druid.git)
217+
#[clap(long)]
218+
default_mirror: Option<String>,
219+
},
220+
221+
/// Creates a patchable.toml for a given product version
222+
Version {
223+
#[clap(flatten)]
224+
pv: ProductVersion,
225+
226+
/// The upstream commit-ish (such as druid-28.0.0) that the patch series applies to
227+
///
228+
/// Refs (such as tags and branches) will be resolved to commit IDs.
229+
#[clap(long)]
230+
base: String,
231+
232+
/// Mirror the product version to the default mirror repository
233+
#[clap(long)]
234+
mirror: bool,
235+
236+
/// Use SSH for git operations
237+
#[clap(long)]
238+
ssh: bool,
239+
},
240+
}
241+
221242
#[derive(Debug, Snafu)]
222243
pub enum Error {
223244
#[snafu(display("failed to configure git logging"))]
@@ -475,10 +496,61 @@ fn main() -> Result<()> {
475496
}
476497

477498
Cmd::Init {
478-
pv,
479-
base,
480-
mirror,
481-
ssh,
499+
init_type:
500+
InitCmd::Product {
501+
product,
502+
upstream,
503+
default_mirror,
504+
},
505+
} => {
506+
let product_config_path = ProductVersionContext {
507+
pv: ProductVersion {
508+
product: product.clone(),
509+
version: "".to_string(),
510+
},
511+
images_repo_root,
512+
}
513+
.product_config_path();
514+
515+
tracing::info!(
516+
path = ?product_config_path,
517+
"creating product configuration directory and file"
518+
);
519+
520+
if let Some(product_config_dir) = product_config_path.parent() {
521+
std::fs::create_dir_all(product_config_dir).context(CreatePatchDirSnafu {
522+
path: product_config_dir,
523+
})?;
524+
}
525+
526+
let product_config = ProductConfig {
527+
upstream,
528+
default_mirror,
529+
};
530+
531+
let config_toml =
532+
toml::to_string_pretty(&product_config).context(SerializeConfigSnafu)?;
533+
File::create_new(&product_config_path)
534+
.and_then(|mut f| f.write_all(config_toml.as_bytes()))
535+
.context(SaveConfigSnafu {
536+
path: &product_config_path,
537+
})?;
538+
539+
tracing::info!(
540+
config.path = ?product_config_path,
541+
product = product,
542+
"created configuration for product"
543+
);
544+
}
545+
546+
Cmd::Init {
547+
init_type:
548+
InitCmd::Version {
549+
pv,
550+
base,
551+
mirror,
552+
ssh,
553+
},
482554
} => {
483555
let ctx = ProductVersionContext {
484556
pv,
@@ -510,6 +582,7 @@ fn main() -> Result<()> {
510582
let mirror_url = if mirror {
511583
let mut mirror_url = config
512584
.default_mirror
585+
.filter(|s| !s.is_empty())
513586
.context(InitMirrorNotConfiguredSnafu)?;
514587
if ssh {
515588
mirror_url =
@@ -572,6 +645,7 @@ fn main() -> Result<()> {
572645
std::fs::create_dir_all(config_dir)
573646
.context(CreatePatchDirSnafu { path: config_dir })?;
574647
}
648+
575649
let config_toml = toml::to_string_pretty(&config).context(SerializeConfigSnafu)?;
576650
File::create_new(&config_path)
577651
.and_then(|mut f| f.write_all(config_toml.as_bytes()))

0 commit comments

Comments
 (0)