@@ -101,6 +101,38 @@ impl Container {
101
101
} )
102
102
}
103
103
104
+ /// Start the container, if it isn't already running.
105
+ pub async fn start ( & self ) -> anyhow:: Result < ( ) > {
106
+ let mut pause_cmd = Command :: new ( RUNTIME_BIN ) ;
107
+ pause_cmd. args ( & [ "start" , & self . name ] ) ;
108
+ exec_command ( & mut pause_cmd) . await ?;
109
+ Ok ( ( ) )
110
+ }
111
+
112
+ /// Pause the container's execution, if it currently running.
113
+ pub async fn pause ( & self ) -> anyhow:: Result < ( ) > {
114
+ let mut pause_cmd = Command :: new ( RUNTIME_BIN ) ;
115
+ pause_cmd. args ( & [ "pause" , & self . name ] ) ;
116
+ exec_command ( & mut pause_cmd) . await ?;
117
+ Ok ( ( ) )
118
+ }
119
+
120
+ /// Resume the container's execution, if it currently paused.
121
+ pub async fn resume ( & self ) -> anyhow:: Result < ( ) > {
122
+ let mut resume_cmd = Command :: new ( RUNTIME_BIN ) ;
123
+ resume_cmd. args ( & [ "resume" , & self . name ] ) ;
124
+ exec_command ( & mut resume_cmd) . await ?;
125
+ Ok ( ( ) )
126
+ }
127
+
128
+ /// Delete the container immediately.
129
+ pub async fn delete ( self ) -> anyhow:: Result < ( ) > {
130
+ let mut delete_cmd = Command :: new ( RUNTIME_BIN ) ;
131
+ delete_cmd. args ( & [ "delete" , "--force" , & self . name ] ) ;
132
+ exec_command ( & mut delete_cmd) . await ?;
133
+ Ok ( ( ) )
134
+ }
135
+
104
136
/// Returns the name of the container.
105
137
pub fn name ( & self ) -> & str {
106
138
& self . name
@@ -117,3 +149,13 @@ impl Drop for Container {
117
149
unsafe { libc:: kill ( self . pid , libc:: SIGKILL ) } ;
118
150
}
119
151
}
152
+
153
+ async fn exec_command ( cmd : & mut Command ) -> anyhow:: Result < ( ) > {
154
+ let output = cmd. output ( ) . await ?;
155
+ if !output. status . success ( ) {
156
+ let stderr = std:: str:: from_utf8 ( & output. stderr ) ?;
157
+ return Err ( anyhow ! ( "`{:?}` returned: [{}]" , cmd, stderr) ) ;
158
+ }
159
+
160
+ Ok ( ( ) )
161
+ }
0 commit comments