Skip to content

Commit

Permalink
feat: Manage stage ARG (#277)
Browse files Browse the repository at this point in the history
* feat: Manage stage ARG

* docs: JSON Schema
  • Loading branch information
taorepoara authored Sep 23, 2024
1 parent 0e67979 commit 7afc4aa
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 19 deletions.
33 changes: 22 additions & 11 deletions docs/dofigen.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
}
],
"properties": {
"arg": {
"anyOf": [
{
"$ref": "#/definitions/HashMapPatch<String, String>"
},
{
"type": "null"
}
],
"nullable": true
},
"bind": {
"anyOf": [
{
Expand Down Expand Up @@ -336,17 +347,6 @@
],
"nullable": true
},
"exclude": {
"anyOf": [
{
"$ref": "#/definitions/VecPatch<String>"
},
{
"type": "null"
}
],
"nullable": true
},
"keepGitDir": {
"default": null,
"type": [
Expand Down Expand Up @@ -1033,6 +1033,17 @@
}
],
"properties": {
"arg": {
"anyOf": [
{
"$ref": "#/definitions/HashMapPatch<String, String>"
},
{
"type": "null"
}
],
"nullable": true
},
"bind": {
"anyOf": [
{
Expand Down
6 changes: 5 additions & 1 deletion dofigen.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ effective: |
context:
- /builds
workdir: /app
arg:
TARGETPLATFORM: ''
copy:
- paths:
- builds/${TARGETPLATFORM}/dofigen
Expand All @@ -13,10 +15,12 @@ effective: |
images: {}
resources:
dofigen.yml:
hash: f71f101575b39dec6c2b8d21a2a695173eabd8f5be3d02456b6b6b928677fa59
hash: 160f8547a6b42e0996778ad72c6a73328f8460e826475def77213acb69f45284
content: |
# Runtime
workdir: /app
arg:
TARGETPLATFORM: ""
copy:
- paths: builds/${TARGETPLATFORM}/dofigen
target: /bin/
Expand Down
2 changes: 2 additions & 0 deletions dofigen.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Runtime
workdir: /app
arg:
TARGETPLATFORM: ""
copy:
- paths: builds/${TARGETPLATFORM}/dofigen
target: /bin/
Expand Down
5 changes: 5 additions & 0 deletions src/dofigen_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ pub struct Stage {
#[serde(skip_serializing_if = "Option::is_none")]
pub workdir: Option<String>,

#[patch(name = "HashMapPatch<String, String>")]
#[cfg_attr(not(feature = "strict"), patch(attribute(serde(alias = "args"))))]
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub arg: HashMap<String, String>,

#[patch(name = "HashMapPatch<String, String>")]
#[cfg_attr(not(feature = "strict"), patch(attribute(serde(alias = "envs"))))]
#[serde(skip_serializing_if = "HashMap::is_empty")]
Expand Down
63 changes: 58 additions & 5 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,24 @@ impl DockerfileGenerator for Stage {
}),
];

// Arg
if !self.arg.is_empty() {
let mut keys = self.arg.keys().collect::<Vec<&String>>();
keys.sort();
keys.iter().for_each(|key| {
let value = self.arg.get(*key).unwrap();
lines.push(DockerfileLine::Instruction(DockerfileInsctruction {
command: "ARG".into(),
content: if value.is_empty() {
key.to_string()
} else {
format!("{}={}", key, value)
},
options: vec![],
}));
});
}

// Env
if !self.env.is_empty() {
lines.push(DockerfileLine::Instruction(DockerfileInsctruction {
Expand Down Expand Up @@ -730,16 +748,16 @@ mod test {
use super::*;
use pretty_assertions_sorted::assert_eq_sorted;

mod builder {
mod stage {
use super::*;

#[test]
fn user_with_user() {
let builder = Stage {
let stage = Stage {
user: Some(User::new_without_group("my-user").into()),
..Default::default()
};
let user = builder.user(&GenerationContext::default());
let user = stage.user(&GenerationContext::default());
assert_eq_sorted!(
user,
Some(User {
Expand All @@ -751,10 +769,45 @@ mod test {

#[test]
fn user_without_user() {
let builder = Stage::default();
let user = builder.user(&GenerationContext::default());
let stage = Stage::default();
let user = stage.user(&GenerationContext::default());
assert_eq_sorted!(user, None);
}

#[test]
fn stage_args() {
let stage = Stage {
arg: HashMap::from([("arg2".into(), "".into()), ("arg1".into(), "value1".into())]),
..Default::default()
};

let lines = stage.generate_dockerfile_lines(&GenerationContext {
stage_name: "test".into(),
..Default::default()
});

assert_eq_sorted!(
lines.unwrap(),
vec![
DockerfileLine::Comment("test".into()),
DockerfileLine::Instruction(DockerfileInsctruction {
command: "FROM".into(),
content: "scratch AS test".into(),
options: vec![],
}),
DockerfileLine::Instruction(DockerfileInsctruction {
command: "ARG".into(),
content: "arg1=value1".into(),
options: vec![],
}),
DockerfileLine::Instruction(DockerfileInsctruction {
command: "ARG".into(),
content: "arg2".into(),
options: vec![],
}),
]
);
}
}

mod image_name {
Expand Down
9 changes: 7 additions & 2 deletions tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ builders:
- ls -al
- cargo build --release
cache: /usr/local/cargo/registry
arg:
TARGETPLATFORM: ""
APP_NAME: template-rust
env:
fprocess: /app
artifacts:
- fromBuilder: builder
source: /home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust
source: /home/rust/src/target/x86_64-unknown-linux-musl/release/${APP_NAME}
target: /app
- fromImage: ghcr.io/openfaas/of-watchdog:0.9.6
source: /fwatchdog
Expand Down Expand Up @@ -91,12 +94,14 @@ EOF
# runtime
FROM scratch AS runtime
ARG APP_NAME=template-rust
ARG TARGETPLATFORM
ENV fprocess="/app"
COPY \
--from=builder \
--chown=1000:1000 \
--link \
"/home/rust/src/target/x86_64-unknown-linux-musl/release/template-rust" "/app"
"/home/rust/src/target/x86_64-unknown-linux-musl/release/${APP_NAME}" "/app"
COPY \
--from=ghcr.io/openfaas/of-watchdog:0.9.6 \
--chown=1000:1000 \
Expand Down

0 comments on commit 7afc4aa

Please sign in to comment.