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

     1  // Copyright 2017-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 checker
     6  
     7  import (
     8  	"log"
     9  	"os"
    10  	"os/exec"
    11  )
    12  
    13  // DefaultShell is used by EmergencyShell
    14  var DefaultShell = "elvish"
    15  
    16  func runCmd(prog string, args ...string) error {
    17  	cmd := exec.Command(prog, args...)
    18  	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
    19  	return cmd.Run()
    20  }
    21  
    22  // CommandExecutor returns a check that runs the provided command and arguments.
    23  func CommandExecutor(prog string, args ...string) Checker {
    24  	return func() error {
    25  		return runCmd(prog, args...)
    26  	}
    27  }
    28  
    29  // CommandExecutorRemediation is like CommandExecutor, but returns a Remediator.
    30  func CommandExecutorRemediation(prog string, args ...string) Remediator {
    31  	return func() error {
    32  		return runCmd(prog, args...)
    33  	}
    34  }
    35  
    36  // EmergencyShell is a remediation that prints the given banner, and then calls
    37  // an emergency shell.
    38  func EmergencyShell(banner string) Remediator {
    39  	return func() error {
    40  		log.Print(green("Running emergency shell: %s", DefaultShell))
    41  		if banner != "" {
    42  			log.Print(banner)
    43  		}
    44  		return CommandExecutorRemediation(DefaultShell)()
    45  	}
    46  }