github.com/blend/go-sdk@v1.20220411.3/sh/cmd.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  	"strings"
    15  
    16  	"github.com/blend/go-sdk/ex"
    17  )
    18  
    19  // MustCmds returns a list of commands for a given set of statements.
    20  // It is useful for running a batch of commands.
    21  // It panics on error.
    22  func MustCmds(statements ...string) []*exec.Cmd {
    23  	var output []*exec.Cmd
    24  	for _, statement := range statements {
    25  		output = append(output, MustCmdParsed(statement))
    26  	}
    27  	return output
    28  }
    29  
    30  // Cmds returns a list of commands for a given set of statements.
    31  // It is useful for running a batch of commands.
    32  // It will return the first error it encounters.
    33  func Cmds(statements ...string) ([]*exec.Cmd, error) {
    34  	var output []*exec.Cmd
    35  	for _, statement := range statements {
    36  		cmd, err := CmdParsed(statement)
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		output = append(output, cmd)
    41  	}
    42  	return output, nil
    43  }
    44  
    45  // MustCmdParsed returns a command for a full comamnd statement.
    46  func MustCmdParsed(statement string) *exec.Cmd {
    47  	cmd, err := CmdParsed(statement)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return cmd
    52  }
    53  
    54  // CmdParsed returns a command for a full comamnd statement.
    55  func CmdParsed(statement string) (*exec.Cmd, error) {
    56  	parts := strings.Split(statement, " ")
    57  	if len(parts) > 1 {
    58  		return Cmd(parts[0], parts[1:]...)
    59  	}
    60  	return Cmd(parts[0])
    61  }
    62  
    63  // CmdParsedContext returns a command for a full comamnd statement within a context..
    64  func CmdParsedContext(ctx context.Context, statement string) (*exec.Cmd, error) {
    65  	parts := strings.Split(statement, " ")
    66  	if len(parts) > 1 {
    67  		return CmdContext(ctx, parts[0], parts[1:]...)
    68  	}
    69  	return CmdContext(ctx, parts[0])
    70  }
    71  
    72  // MustCmd returns a new command with the fully qualified path of the executable.
    73  // It panics on error.
    74  func MustCmd(command string, args ...string) *exec.Cmd {
    75  	cmd, err := Cmd(command, args...)
    76  	if err != nil {
    77  		panic(ex.New(err, ex.OptMessagef("looking for: %s", command)))
    78  	}
    79  	return cmd
    80  }
    81  
    82  // Cmd returns a new command with the fully qualified path of the executable.
    83  func Cmd(command string, args ...string) (*exec.Cmd, error) {
    84  	absoluteCommand, err := exec.LookPath(command)
    85  	if err != nil {
    86  		return nil, ex.New(err, ex.OptMessagef("looking for: %s", command))
    87  	}
    88  	cmd := exec.Command(absoluteCommand, args...)
    89  	cmd.Env = os.Environ()
    90  	return cmd, nil
    91  }
    92  
    93  // CmdContext returns a new command with the fully qualified path of the executable within a context.
    94  func CmdContext(ctx context.Context, command string, args ...string) (*exec.Cmd, error) {
    95  	absoluteCommand, err := exec.LookPath(command)
    96  	if err != nil {
    97  		return nil, ex.New(err, ex.OptMessagef("looking for: %s", command))
    98  	}
    99  	cmd := exec.CommandContext(ctx, absoluteCommand, args...)
   100  	cmd.Env = os.Environ()
   101  	return cmd, nil
   102  }