1
0
mirror of https://github.com/containers/skopeo.git synced 2025-05-07 15:36:31 +00:00

add command timeout support for skopeo

* add global command-timeout option for skopeo
This commit is contained in:
jianqli 2018-11-01 23:02:33 +08:00
parent b52e700666
commit 3456577268
8 changed files with 28 additions and 8 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"context"
"errors"
"fmt"
"os"
@ -88,7 +87,10 @@ func copyHandler(c *cli.Context) error {
}
}
_, err = copy.Image(context.Background(), policyContext, destRef, srcRef, &copy.Options{
ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
_, err = copy.Image(ctx, policyContext, destRef, srcRef, &copy.Options{
RemoveSignatures: removeSignatures,
SignBy: signBy,
ReportWriter: os.Stdout,

View File

@ -1,7 +1,6 @@
package main
import (
"context"
"errors"
"fmt"
"strings"
@ -25,7 +24,10 @@ func deleteHandler(c *cli.Context) error {
if err != nil {
return err
}
return ref.DeleteImage(context.Background(), sys)
ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
return ref.DeleteImage(ctx, sys)
}
var deleteCmd = cli.Command{

View File

@ -1,7 +1,6 @@
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
@ -67,7 +66,8 @@ var inspectCmd = cli.Command{
},
},
Action: func(c *cli.Context) (retErr error) {
ctx := context.Background()
ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
img, err := parseImage(ctx, c)
if err != nil {

View File

@ -1,7 +1,6 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
@ -26,7 +25,8 @@ var layersCmd = cli.Command{
return errors.New("Usage: layers imageReference [layer...]")
}
ctx := context.Background()
ctx, cancel := commandTimeoutContextFromGlobalOptions(c)
defer cancel()
sys, err := contextFromGlobalOptions(c, "")
if err != nil {

View File

@ -60,6 +60,10 @@ func createApp() *cli.App {
Value: "",
Usage: "use `OS` instead of the running OS for choosing images",
},
cli.DurationFlag{
Name: "command-timeout",
Usage: "timeout for the command execution",
},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") {

View File

@ -40,6 +40,15 @@ func contextFromGlobalOptions(c *cli.Context, flagPrefix string) (*types.SystemC
return ctx, nil
}
func commandTimeoutContextFromGlobalOptions(c *cli.Context) (context.Context, context.CancelFunc) {
ctx := context.Background()
var cancel context.CancelFunc = func() {}
if c.GlobalDuration("command-timeout") > 0 {
ctx, cancel = context.WithTimeout(ctx, c.GlobalDuration("command-timeout"))
}
return ctx, cancel
}
func parseCreds(creds string) (string, string, error) {
if creds == "" {
return "", "", errors.New("credentials can't be empty")

View File

@ -110,6 +110,7 @@ _skopeo_skopeo() {
--registries.d
--override-arch
--override-os
--command-timeout
"
local boolean_options="
--insecure-policy

View File

@ -59,6 +59,8 @@ Most commands refer to container images, using a _transport_`:`_details_ format.
**--override-os** _OS_ Use _OS_ instead of the running OS for choosing images.
**--command-timeout** _duration_ Timeout for the command execution.
**--help**|**-h** Show help
**--version**|**-v** print the version number