feat(sdk): without Some in _opts functions

Option has been removed as a wrapper around opts. This makes it much
more convenient to use

```rust
client.container_opts(Some(ContainerOpts{}))
// ->
client.container_opts(ContainerOpts{})
```

The same options are still available, either an empty object can be
passed, or a non _opts function can be used
This commit is contained in:
Kasper Juul Hermansen 2023-02-19 17:49:22 +01:00 committed by Kasper Juul Hermansen
parent 9762da895a
commit f29ff836cf
8 changed files with 160 additions and 228 deletions

View File

@ -171,9 +171,7 @@ fn render_optional_args(_funcs: &CommonFunctions, field: &FullTypeFields) -> Opt
}
let required_args = quote! {
if let Some(opts) = opts {
$(for arg in args join ($['\r']) => $arg)
}
};
Some(required_args)
@ -273,7 +271,7 @@ fn format_function_args(
Some((
quote! {
$(required_args)
opts: Option<$(field_options_struct_name(field))>
opts: $(field_options_struct_name(field))
},
true,
))

View File

@ -5,10 +5,10 @@ fn main() -> eyre::Result<()> {
let host_source_dir = client.host().directory_opts(
"examples/build-the-application/app",
Some(HostDirectoryOpts {
HostDirectoryOpts {
exclude: Some(vec!["node_modules".into(), "ci/".into()]),
include: None,
}),
},
);
let source = client

View File

@ -5,11 +5,9 @@ fn main() -> eyre::Result<()> {
let host_source_dir = client.host().directory_opts(
"./examples/caching/app",
Some(
dagger_sdk::HostDirectoryOptsBuilder::default()
.exclude(vec!["node_modules", "ci/"])
.build()?,
),
);
let node_cache = client.cache_volume("node").id()?;

View File

@ -6,10 +6,10 @@ fn main() -> eyre::Result<()> {
let host_source_dir = client.host().directory_opts(
"examples/publish-the-application/app",
Some(HostDirectoryOpts {
HostDirectoryOpts {
exclude: Some(vec!["node_modules", "ci/"]),
include: None,
}),
},
);
let source = client

View File

@ -7,10 +7,10 @@ fn main() -> eyre::Result<()> {
let host_source_dir = client.host().directory_opts(
"examples/publish-the-application/app",
Some(HostDirectoryOpts {
HostDirectoryOpts {
exclude: Some(vec!["node_modules", "ci/"]),
include: None,
}),
},
);
let source = client

View File

@ -5,10 +5,10 @@ fn main() -> eyre::Result<()> {
let host_source_dir = client.host().directory_opts(
"examples/test-the-application/app",
Some(HostDirectoryOpts {
HostDirectoryOpts {
exclude: Some(vec!["node_modules", "ci/"]),
include: None,
}),
},
);
let source = client

View File

@ -22,8 +22,8 @@ pub struct SecretId(String);
pub struct SocketId(String);
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct BuildArg {
pub value: String,
pub name: String,
pub value: String,
}
pub struct CacheVolume {
pub proc: Arc<Child>,
@ -137,11 +137,10 @@ impl Container {
};
}
pub fn build_opts(&self, context: DirectoryId, opts: Option<ContainerBuildOpts>) -> Container {
pub fn build_opts(&self, context: DirectoryId, opts: ContainerBuildOpts) -> Container {
let mut query = self.selection.select("build");
query = query.arg("context", context);
if let Some(opts) = opts {
if let Some(dockerfile) = opts.dockerfile {
query = query.arg("dockerfile", dockerfile);
}
@ -151,7 +150,6 @@ impl Container {
if let Some(target) = opts.target {
query = query.arg("target", target);
}
}
return Container {
proc: self.proc.clone(),
@ -206,10 +204,9 @@ impl Container {
};
}
pub fn exec_opts(&self, opts: Option<ContainerExecOpts>) -> Container {
pub fn exec_opts(&self, opts: ContainerExecOpts) -> Container {
let mut query = self.selection.select("exec");
if let Some(opts) = opts {
if let Some(args) = opts.args {
query = query.arg("args", args);
}
@ -228,7 +225,6 @@ impl Container {
experimental_privileged_nesting,
);
}
}
return Container {
proc: self.proc.clone(),
@ -252,16 +248,14 @@ impl Container {
pub fn export_opts(
&self,
path: impl Into<String>,
opts: Option<ContainerExportOpts>,
opts: ContainerExportOpts,
) -> eyre::Result<bool> {
let mut query = self.selection.select("export");
query = query.arg("path", path.into());
if let Some(opts) = opts {
if let Some(platform_variants) = opts.platform_variants {
query = query.arg("platformVariants", platform_variants);
}
}
query.execute(&graphql_client(&self.conn))
}
@ -334,19 +328,13 @@ impl Container {
};
}
pub fn pipeline_opts(
&self,
name: impl Into<String>,
opts: Option<ContainerPipelineOpts>,
) -> Container {
pub fn pipeline_opts(&self, name: impl Into<String>, opts: ContainerPipelineOpts) -> Container {
let mut query = self.selection.select("pipeline");
query = query.arg("name", name.into());
if let Some(opts) = opts {
if let Some(description) = opts.description {
query = query.arg("description", description);
}
}
return Container {
proc: self.proc.clone(),
@ -370,16 +358,14 @@ impl Container {
pub fn publish_opts(
&self,
address: impl Into<String>,
opts: Option<ContainerPublishOpts>,
opts: ContainerPublishOpts,
) -> eyre::Result<String> {
let mut query = self.selection.select("publish");
query = query.arg("address", address.into());
if let Some(opts) = opts {
if let Some(platform_variants) = opts.platform_variants {
query = query.arg("platformVariants", platform_variants);
}
}
query.execute(&graphql_client(&self.conn))
}
@ -417,14 +403,12 @@ impl Container {
};
}
pub fn with_default_args_opts(&self, opts: Option<ContainerWithDefaultArgsOpts>) -> Container {
pub fn with_default_args_opts(&self, opts: ContainerWithDefaultArgsOpts) -> Container {
let mut query = self.selection.select("withDefaultArgs");
if let Some(opts) = opts {
if let Some(args) = opts.args {
query = query.arg("args", args);
}
}
return Container {
proc: self.proc.clone(),
@ -449,20 +433,18 @@ impl Container {
&self,
path: impl Into<String>,
directory: DirectoryId,
opts: Option<ContainerWithDirectoryOpts>,
opts: ContainerWithDirectoryOpts,
) -> Container {
let mut query = self.selection.select("withDirectory");
query = query.arg("path", path.into());
query = query.arg("directory", directory);
if let Some(opts) = opts {
if let Some(exclude) = opts.exclude {
query = query.arg("exclude", exclude);
}
if let Some(include) = opts.include {
query = query.arg("include", include);
}
}
return Container {
proc: self.proc.clone(),
@ -518,7 +500,7 @@ impl Container {
pub fn with_exec_opts(
&self,
args: Vec<impl Into<String>>,
opts: Option<ContainerWithExecOpts>,
opts: ContainerWithExecOpts,
) -> Container {
let mut query = self.selection.select("withExec");
@ -526,7 +508,6 @@ impl Container {
"args",
args.into_iter().map(|i| i.into()).collect::<Vec<String>>(),
);
if let Some(opts) = opts {
if let Some(stdin) = opts.stdin {
query = query.arg("stdin", stdin);
}
@ -542,7 +523,6 @@ impl Container {
experimental_privileged_nesting,
);
}
}
return Container {
proc: self.proc.clone(),
@ -578,17 +558,15 @@ impl Container {
&self,
path: impl Into<String>,
source: FileId,
opts: Option<ContainerWithFileOpts>,
opts: ContainerWithFileOpts,
) -> Container {
let mut query = self.selection.select("withFile");
query = query.arg("path", path.into());
query = query.arg("source", source);
if let Some(opts) = opts {
if let Some(permissions) = opts.permissions {
query = query.arg("permissions", permissions);
}
}
return Container {
proc: self.proc.clone(),
@ -625,20 +603,18 @@ impl Container {
&self,
path: impl Into<String>,
cache: CacheId,
opts: Option<ContainerWithMountedCacheOpts>,
opts: ContainerWithMountedCacheOpts,
) -> Container {
let mut query = self.selection.select("withMountedCache");
query = query.arg("path", path.into());
query = query.arg("cache", cache);
if let Some(opts) = opts {
if let Some(source) = opts.source {
query = query.arg("source", source);
}
if let Some(sharing) = opts.sharing {
query = query.arg("sharing", sharing);
}
}
return Container {
proc: self.proc.clone(),
@ -712,19 +688,17 @@ impl Container {
pub fn with_new_file_opts(
&self,
path: impl Into<String>,
opts: Option<ContainerWithNewFileOpts>,
opts: ContainerWithNewFileOpts,
) -> Container {
let mut query = self.selection.select("withNewFile");
query = query.arg("path", path.into());
if let Some(opts) = opts {
if let Some(contents) = opts.contents {
query = query.arg("contents", contents);
}
if let Some(permissions) = opts.permissions {
query = query.arg("permissions", permissions);
}
}
return Container {
proc: self.proc.clone(),
@ -951,10 +925,9 @@ impl Directory {
};
}
pub fn docker_build_opts(&self, opts: Option<DirectoryDockerBuildOpts>) -> Container {
pub fn docker_build_opts(&self, opts: DirectoryDockerBuildOpts) -> Container {
let mut query = self.selection.select("dockerBuild");
if let Some(opts) = opts {
if let Some(dockerfile) = opts.dockerfile {
query = query.arg("dockerfile", dockerfile);
}
@ -967,7 +940,6 @@ impl Directory {
if let Some(target) = opts.target {
query = query.arg("target", target);
}
}
return Container {
proc: self.proc.clone(),
@ -981,14 +953,12 @@ impl Directory {
query.execute(&graphql_client(&self.conn))
}
pub fn entries_opts(&self, opts: Option<DirectoryEntriesOpts>) -> eyre::Result<Vec<String>> {
pub fn entries_opts(&self, opts: DirectoryEntriesOpts) -> eyre::Result<Vec<String>> {
let mut query = self.selection.select("entries");
if let Some(opts) = opts {
if let Some(path) = opts.path {
query = query.arg("path", path);
}
}
query.execute(&graphql_client(&self.conn))
}
@ -1038,19 +1008,13 @@ impl Directory {
};
}
pub fn pipeline_opts(
&self,
name: impl Into<String>,
opts: Option<DirectoryPipelineOpts>,
) -> Directory {
pub fn pipeline_opts(&self, name: impl Into<String>, opts: DirectoryPipelineOpts) -> Directory {
let mut query = self.selection.select("pipeline");
query = query.arg("name", name.into());
if let Some(opts) = opts {
if let Some(description) = opts.description {
query = query.arg("description", description);
}
}
return Directory {
proc: self.proc.clone(),
@ -1075,20 +1039,18 @@ impl Directory {
&self,
path: impl Into<String>,
directory: DirectoryId,
opts: Option<DirectoryWithDirectoryOpts>,
opts: DirectoryWithDirectoryOpts,
) -> Directory {
let mut query = self.selection.select("withDirectory");
query = query.arg("path", path.into());
query = query.arg("directory", directory);
if let Some(opts) = opts {
if let Some(exclude) = opts.exclude {
query = query.arg("exclude", exclude);
}
if let Some(include) = opts.include {
query = query.arg("include", include);
}
}
return Directory {
proc: self.proc.clone(),
@ -1113,17 +1075,15 @@ impl Directory {
&self,
path: impl Into<String>,
source: FileId,
opts: Option<DirectoryWithFileOpts>,
opts: DirectoryWithFileOpts,
) -> Directory {
let mut query = self.selection.select("withFile");
query = query.arg("path", path.into());
query = query.arg("source", source);
if let Some(opts) = opts {
if let Some(permissions) = opts.permissions {
query = query.arg("permissions", permissions);
}
}
return Directory {
proc: self.proc.clone(),
@ -1146,16 +1106,14 @@ impl Directory {
pub fn with_new_directory_opts(
&self,
path: impl Into<String>,
opts: Option<DirectoryWithNewDirectoryOpts>,
opts: DirectoryWithNewDirectoryOpts,
) -> Directory {
let mut query = self.selection.select("withNewDirectory");
query = query.arg("path", path.into());
if let Some(opts) = opts {
if let Some(permissions) = opts.permissions {
query = query.arg("permissions", permissions);
}
}
return Directory {
proc: self.proc.clone(),
@ -1180,17 +1138,15 @@ impl Directory {
&self,
path: impl Into<String>,
contents: impl Into<String>,
opts: Option<DirectoryWithNewFileOpts>,
opts: DirectoryWithNewFileOpts,
) -> Directory {
let mut query = self.selection.select("withNewFile");
query = query.arg("path", path.into());
query = query.arg("contents", contents.into());
if let Some(opts) = opts {
if let Some(permissions) = opts.permissions {
query = query.arg("permissions", permissions);
}
}
return Directory {
proc: self.proc.clone(),
@ -1330,17 +1286,15 @@ impl GitRef {
};
}
pub fn tree_opts(&self, opts: Option<GitRefTreeOpts>) -> Directory {
pub fn tree_opts(&self, opts: GitRefTreeOpts) -> Directory {
let mut query = self.selection.select("tree");
if let Some(opts) = opts {
if let Some(ssh_known_hosts) = opts.ssh_known_hosts {
query = query.arg("sshKnownHosts", ssh_known_hosts);
}
if let Some(ssh_auth_socket) = opts.ssh_auth_socket {
query = query.arg("sshAuthSocket", ssh_auth_socket);
}
}
return Directory {
proc: self.proc.clone(),
@ -1434,22 +1388,16 @@ impl Host {
};
}
pub fn directory_opts(
&self,
path: impl Into<String>,
opts: Option<HostDirectoryOpts>,
) -> Directory {
pub fn directory_opts(&self, path: impl Into<String>, opts: HostDirectoryOpts) -> Directory {
let mut query = self.selection.select("directory");
query = query.arg("path", path.into());
if let Some(opts) = opts {
if let Some(exclude) = opts.exclude {
query = query.arg("exclude", exclude);
}
if let Some(include) = opts.include {
query = query.arg("include", include);
}
}
return Directory {
proc: self.proc.clone(),
@ -1489,17 +1437,15 @@ impl Host {
};
}
pub fn workdir_opts(&self, opts: Option<HostWorkdirOpts>) -> Directory {
pub fn workdir_opts(&self, opts: HostWorkdirOpts) -> Directory {
let mut query = self.selection.select("workdir");
if let Some(opts) = opts {
if let Some(exclude) = opts.exclude {
query = query.arg("exclude", exclude);
}
if let Some(include) = opts.include {
query = query.arg("include", include);
}
}
return Directory {
proc: self.proc.clone(),
@ -1650,17 +1596,15 @@ impl Query {
};
}
pub fn container_opts(&self, opts: Option<QueryContainerOpts>) -> Container {
pub fn container_opts(&self, opts: QueryContainerOpts) -> Container {
let mut query = self.selection.select("container");
if let Some(opts) = opts {
if let Some(id) = opts.id {
query = query.arg("id", id);
}
if let Some(platform) = opts.platform {
query = query.arg("platform", platform);
}
}
return Container {
proc: self.proc.clone(),
@ -1683,14 +1627,12 @@ impl Query {
};
}
pub fn directory_opts(&self, opts: Option<QueryDirectoryOpts>) -> Directory {
pub fn directory_opts(&self, opts: QueryDirectoryOpts) -> Directory {
let mut query = self.selection.select("directory");
if let Some(opts) = opts {
if let Some(id) = opts.id {
query = query.arg("id", id);
}
}
return Directory {
proc: self.proc.clone(),
@ -1721,15 +1663,13 @@ impl Query {
};
}
pub fn git_opts(&self, url: impl Into<String>, opts: Option<QueryGitOpts>) -> GitRepository {
pub fn git_opts(&self, url: impl Into<String>, opts: QueryGitOpts) -> GitRepository {
let mut query = self.selection.select("git");
query = query.arg("url", url.into());
if let Some(opts) = opts {
if let Some(keep_git_dir) = opts.keep_git_dir {
query = query.arg("keepGitDir", keep_git_dir);
}
}
return GitRepository {
proc: self.proc.clone(),
@ -1769,15 +1709,13 @@ impl Query {
};
}
pub fn pipeline_opts(&self, name: impl Into<String>, opts: Option<QueryPipelineOpts>) -> Query {
pub fn pipeline_opts(&self, name: impl Into<String>, opts: QueryPipelineOpts) -> Query {
let mut query = self.selection.select("pipeline");
query = query.arg("name", name.into());
if let Some(opts) = opts {
if let Some(description) = opts.description {
query = query.arg("description", description);
}
}
return Query {
proc: self.proc.clone(),
@ -1817,14 +1755,12 @@ impl Query {
};
}
pub fn socket_opts(&self, opts: Option<QuerySocketOpts>) -> Socket {
pub fn socket_opts(&self, opts: QuerySocketOpts) -> Socket {
let mut query = self.selection.select("socket");
if let Some(opts) = opts {
if let Some(id) = opts.id {
query = query.arg("id", id);
}
}
return Socket {
proc: self.proc.clone(),

View File

@ -7,12 +7,12 @@ fn test_example_container() {
let alpine = client.container().from("alpine:3.16.2");
let out = alpine
.exec_opts(Some(
.exec_opts(
ContainerExecOptsBuilder::default()
.args(vec!["cat", "/etc/alpine-release"])
.build()
.unwrap(),
))
)
.stdout()
.unwrap();