gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/libinit/proc.go (about)

     1  // Copyright 2014-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package libinit
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  
    11  	"github.com/u-root/u-root/pkg/upath"
    12  )
    13  
    14  var osDefault = func(*exec.Cmd) {}
    15  
    16  // CommandModifier makes *exec.Cmd construction modular.
    17  type CommandModifier func(c *exec.Cmd)
    18  
    19  // WithArguments adds command-line arguments to a command.
    20  func WithArguments(arg ...string) CommandModifier {
    21  	return func(c *exec.Cmd) {
    22  		if len(arg) > 0 {
    23  			c.Args = append(c.Args, arg...)
    24  		}
    25  	}
    26  }
    27  
    28  // Command constructs an *exec.Cmd object.
    29  func Command(bin string, m ...CommandModifier) *exec.Cmd {
    30  	bin = upath.UrootPath(bin)
    31  	cmd := exec.Command(bin)
    32  	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
    33  	osDefault(cmd)
    34  	for _, mod := range m {
    35  		mod(cmd)
    36  	}
    37  	return cmd
    38  }