github.com/blend/go-sdk@v1.20220411.3/sh/fork.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sh
     9  
    10  import (
    11  	"context"
    12  	"os"
    13  	"os/exec"
    14  )
    15  
    16  // ForkParsed parses a command into binary and arguments.
    17  // It resolves the command name in your $PATH list for you.
    18  // It shows output and allows input.
    19  // It is intended to be used in scripting.
    20  func ForkParsed(command string) error {
    21  	cmd, err := CmdParsed(command)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	cmd.Env = os.Environ()
    26  	cmd.Stdout = os.Stdout
    27  	cmd.Stderr = os.Stderr
    28  	cmd.Stdin = os.Stdin
    29  	return cmd.Run()
    30  }
    31  
    32  // ForkParsedContext parses a command into binary and arguments.
    33  // It resolves the command name in your $PATH list for you.
    34  // It shows output and allows input.
    35  // It is intended to be used in scripting.
    36  func ForkParsedContext(ctx context.Context, command string) error {
    37  	cmd, err := CmdParsedContext(ctx, command)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	cmd.Env = os.Environ()
    42  	cmd.Stdout = os.Stdout
    43  	cmd.Stderr = os.Stderr
    44  	cmd.Stdin = os.Stdin
    45  	return cmd.Run()
    46  }
    47  
    48  // Fork runs a command with a given list of arguments.
    49  // It resolves the command name in your $PATH list for you.
    50  // It shows output and allows input.
    51  // It is intended to be used in scripting.
    52  func Fork(command string, args ...string) error {
    53  	absoluteCommand, err := exec.LookPath(command)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	cmd := exec.Command(absoluteCommand, args...)
    58  	cmd.Env = os.Environ()
    59  	cmd.Stdout = os.Stdout
    60  	cmd.Stderr = os.Stderr
    61  	cmd.Stdin = os.Stdin
    62  	return cmd.Run()
    63  }
    64  
    65  // ForkContext runs a command with a given list of arguments and context.
    66  // It resolves the command name in your $PATH list for you.
    67  // It shows output and allows input.
    68  // It is intended to be used in scripting.
    69  func ForkContext(ctx context.Context, command string, args ...string) error {
    70  	absoluteCommand, err := exec.LookPath(command)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	cmd := exec.CommandContext(ctx, absoluteCommand, args...)
    75  	cmd.Env = os.Environ()
    76  	cmd.Stdout = os.Stdout
    77  	cmd.Stderr = os.Stderr
    78  	cmd.Stdin = os.Stdin
    79  	return cmd.Run()
    80  }