...

Source file src/github.com/opencontainers/runc/utils.go

Documentation: github.com/opencontainers/runc

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/opencontainers/runtime-spec/specs-go"
    11  
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/urfave/cli"
    14  )
    15  
    16  const (
    17  	exactArgs = iota
    18  	minArgs
    19  	maxArgs
    20  )
    21  
    22  func checkArgs(context *cli.Context, expected, checkType int) error {
    23  	var err error
    24  	cmdName := context.Command.Name
    25  	switch checkType {
    26  	case exactArgs:
    27  		if context.NArg() != expected {
    28  			err = fmt.Errorf("%s: %q requires exactly %d argument(s)", os.Args[0], cmdName, expected)
    29  		}
    30  	case minArgs:
    31  		if context.NArg() < expected {
    32  			err = fmt.Errorf("%s: %q requires a minimum of %d argument(s)", os.Args[0], cmdName, expected)
    33  		}
    34  	case maxArgs:
    35  		if context.NArg() > expected {
    36  			err = fmt.Errorf("%s: %q requires a maximum of %d argument(s)", os.Args[0], cmdName, expected)
    37  		}
    38  	}
    39  
    40  	if err != nil {
    41  		fmt.Printf("Incorrect Usage.\n\n")
    42  		_ = cli.ShowCommandHelp(context, cmdName)
    43  		return err
    44  	}
    45  	return nil
    46  }
    47  
    48  func logrusToStderr() bool {
    49  	l, ok := logrus.StandardLogger().Out.(*os.File)
    50  	return ok && l.Fd() == os.Stderr.Fd()
    51  }
    52  
    53  // fatal prints the error's details if it is a libcontainer specific error type
    54  // then exits the program with an exit status of 1.
    55  func fatal(err error) {
    56  	fatalWithCode(err, 1)
    57  }
    58  
    59  func fatalWithCode(err error, ret int) {
    60  	// Make sure the error is written to the logger.
    61  	logrus.Error(err)
    62  	if !logrusToStderr() {
    63  		fmt.Fprintln(os.Stderr, err)
    64  	}
    65  
    66  	os.Exit(ret)
    67  }
    68  
    69  // setupSpec performs initial setup based on the cli.Context for the container
    70  func setupSpec(context *cli.Context) (*specs.Spec, error) {
    71  	bundle := context.String("bundle")
    72  	if bundle != "" {
    73  		if err := os.Chdir(bundle); err != nil {
    74  			return nil, err
    75  		}
    76  	}
    77  	spec, err := loadSpec(specConfig)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return spec, nil
    82  }
    83  
    84  func revisePidFile(context *cli.Context) error {
    85  	pidFile := context.String("pid-file")
    86  	if pidFile == "" {
    87  		return nil
    88  	}
    89  
    90  	// convert pid-file to an absolute path so we can write to the right
    91  	// file after chdir to bundle
    92  	pidFile, err := filepath.Abs(pidFile)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	return context.Set("pid-file", pidFile)
    97  }
    98  
    99  // reviseRootDir convert the root to absolute path
   100  func reviseRootDir(context *cli.Context) error {
   101  	root := context.GlobalString("root")
   102  	if root == "" {
   103  		return nil
   104  	}
   105  
   106  	root, err := filepath.Abs(root)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	return context.GlobalSet("root", root)
   112  }
   113  
   114  // parseBoolOrAuto returns (nil, nil) if s is empty or "auto"
   115  func parseBoolOrAuto(s string) (*bool, error) {
   116  	if s == "" || strings.ToLower(s) == "auto" {
   117  		return nil, nil
   118  	}
   119  	b, err := strconv.ParseBool(s)
   120  	return &b, err
   121  }
   122  

View as plain text