podman_api/opts/
exec.rs

1use containers_api::{
2    impl_field, impl_opts_builder, impl_str_enum_field, impl_str_field, impl_vec_field,
3};
4use std::fmt;
5
6impl_opts_builder!(json =>
7    /// Modify how an exec session is run inside a container.
8    ExecCreate
9);
10
11#[derive(Debug, Clone)]
12/// One of the variants accepted by [`ExecCreateOptsBuilder::user`](ExecCreateOptsBuilder::user).
13pub enum UserOpt {
14    User(String),
15    UserGroup(String, String),
16    Uid(isize),
17    UidGid(isize, isize),
18}
19
20impl fmt::Display for UserOpt {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        use UserOpt::*;
23        match self {
24            User(user) => write!(f, "{user}"),
25            Uid(uid) => write!(f, "{uid}"),
26            UserGroup(user, group) => write!(f, "{user}:{group}"),
27            UidGid(uid, gid) => write!(f, "{uid}:{gid}"),
28        }
29    }
30}
31
32impl ExecCreateOptsBuilder {
33    impl_field!(
34        /// Attach to stderr of the exec command
35        attach_stderr: bool => "AttachStderr"
36    );
37
38    impl_field!(
39        /// Attach to stdin of the exec command
40        attach_stdin: bool => "AttachStdin"
41    );
42
43    impl_field!(
44        /// Attach to stdout of the exec command
45        attach_stdout: bool => "AttachStdout"
46    );
47
48    impl_vec_field!(
49        /// Command to run, as a string or array of strings.
50        command => "Cmd"
51    );
52
53    impl_str_field!(
54        /// Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl- where is one of: a-z, @, ^, [, , or _.
55        detach_keys => "DetachKeys"
56    );
57
58    /// A list of environment variables to use for the command execution.
59    pub fn env<K, V>(mut self, vars: impl IntoIterator<Item = (K, V)>) -> Self
60    where
61        K: AsRef<str>,
62        V: AsRef<str>,
63    {
64        self.params.insert(
65            "Env",
66            vars.into_iter()
67                .map(|(k, v)| format!("{}={}", k.as_ref(), v.as_ref()))
68                .collect(),
69        );
70        self
71    }
72
73    impl_field!(
74        /// Runs the exec process with extended privileges
75        privileged: bool => "Privileged"
76    );
77
78    impl_field!(
79        /// Allocate a pseudo-TTY
80        tty: bool => "Tty"
81    );
82
83    impl_str_enum_field!(
84        /// The user, and optionally, group to run the exec process inside the container.
85        user: UserOpt => "User"
86    );
87
88    impl_str_field!(
89        /// The working directory for the exec process inside the container.
90        working_dir => "WorkingDir"
91    );
92}
93
94impl_opts_builder!(json =>
95    /// Adjust how an exec instance is started inside of a running container.
96    ExecStart
97);
98
99impl ExecStartOptsBuilder {
100    impl_field!(
101        /// Detach from the command.
102        detach: bool => "Detach"
103    );
104
105    impl_field!(
106        /// Height of the TTY session in characters. Tty must be set to true to use it.
107        height: usize => "h"
108    );
109
110    impl_field!(
111        /// Allocate a pseudo-TTY.
112        tty: bool => "Tty"
113    );
114
115    impl_field!(
116        /// Width of the TTY session in characters. Tty must be set to true to use it.
117        width: usize => "w"
118    );
119}