-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xiechengsheng <[email protected]>
- Loading branch information
1 parent
0d0d697
commit 70e58f1
Showing
14 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// waitDescription is used to describe wait command in detail and auto generate command doc. | ||
var waitDescription = "Block until one or more containers stop, then print their exit codes. " + | ||
"If container state is already stopped, the command will return exit code immediately. " + | ||
"On a successful stop, the exit code of the container is returned. " | ||
|
||
// WaitCommand is used to implement 'wait' command. | ||
type WaitCommand struct { | ||
baseCommand | ||
} | ||
|
||
// Init initialize wait command. | ||
func (wait *WaitCommand) Init(c *Cli) { | ||
wait.cli = c | ||
wait.cmd = &cobra.Command{ | ||
Use: "wait CONTAINER [CONTAINER...]", | ||
Short: "Block until one or more containers stop, then print their exit codes", | ||
Long: waitDescription, | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return wait.runWait(args) | ||
}, | ||
Example: waitExamples(), | ||
} | ||
} | ||
|
||
// runWait is the entry of wait command. | ||
func (wait *WaitCommand) runWait(args []string) error { | ||
ctx := context.Background() | ||
apiClient := wait.cli.Client() | ||
|
||
var errs []string | ||
for _, name := range args { | ||
status, err := apiClient.ContainerWait(ctx, name) | ||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
continue | ||
} | ||
fmt.Printf("%d\n", status) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errors.New(strings.Join(errs, "\n")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// waitExamples shows examples in wait command, and is used in auto-generated cli docs. | ||
func waitExamples() string { | ||
return `$ pouch ps | ||
Name ID Status Created Image Runtime | ||
foo f6717e Up 2 seconds 3 seconds ago registry.hub.docker.com/library/busybox:latest runc | ||
$ pouch stop foo | ||
$ pouch ps -a | ||
Name ID Status Created Image Runtime | ||
foo f6717e Stopped (0) 1 minute 2 minutes ago registry.hub.docker.com/library/busybox:latest runc | ||
$ pouch wait foo | ||
0` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
// ContainerWait pauses execution until a container exits. | ||
// It returns the API status code as response of its readiness. | ||
func (client *APIClient) ContainerWait(ctx context.Context, name string) (int64, error) { | ||
resp, err := client.post(ctx, "/containers/"+name+"/wait", nil, nil, nil) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
var response types.ContainerWaitOKBody | ||
err = decodeBody(&response, resp.Body) | ||
ensureCloseReader(resp) | ||
|
||
if response.Error == "" { | ||
return response.StatusCode, nil | ||
} | ||
return response.StatusCode, errors.New(response.Error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.