github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/work/shell.go (about)

     1  // Copyright 2023 The Go 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 work
     6  
     7  import (
     8  	"github.com/shogo82148/std/io/fs"
     9  )
    10  
    11  // A Shell runs shell commands and performs shell-like file system operations.
    12  //
    13  // Shell tracks context related to running commands, and form a tree much like
    14  // context.Context.
    15  type Shell struct {
    16  	action *Action
    17  	*shellShared
    18  }
    19  
    20  // NewShell returns a new Shell.
    21  //
    22  // Shell will internally serialize calls to the print function.
    23  // If print is nil, it defaults to printing to stderr.
    24  func NewShell(workDir string, print func(a ...any) (int, error)) *Shell
    25  
    26  // Print emits a to this Shell's output stream, formatting it like fmt.Print.
    27  // It is safe to call concurrently.
    28  func (sh *Shell) Print(a ...any)
    29  
    30  // WithAction returns a Shell identical to sh, but bound to Action a.
    31  func (sh *Shell) WithAction(a *Action) *Shell
    32  
    33  // Shell returns a shell for running commands on behalf of Action a.
    34  func (b *Builder) Shell(a *Action) *Shell
    35  
    36  // BackgroundShell returns a Builder-wide Shell that's not bound to any Action.
    37  // Try not to use this unless there's really no sensible Action available.
    38  func (b *Builder) BackgroundShell() *Shell
    39  
    40  // copyFile is like 'cp src dst'.
    41  func (sh *Shell) CopyFile(dst, src string, perm fs.FileMode, force bool) error
    42  
    43  // Mkdir makes the named directory.
    44  func (sh *Shell) Mkdir(dir string) error
    45  
    46  // RemoveAll is like 'rm -rf'. It attempts to remove all paths even if there's
    47  // an error, and returns the first error.
    48  func (sh *Shell) RemoveAll(paths ...string) error
    49  
    50  // Symlink creates a symlink newname -> oldname.
    51  func (sh *Shell) Symlink(oldname, newname string) error
    52  
    53  // ShowCmd prints the given command to standard output
    54  // for the implementation of -n or -x.
    55  //
    56  // ShowCmd also replaces the name of the current script directory with dot (.)
    57  // but only when it is at the beginning of a space-separated token.
    58  //
    59  // If dir is not "" or "/" and not the current script directory, ShowCmd first
    60  // prints a "cd" command to switch to dir and updates the script directory.
    61  func (sh *Shell) ShowCmd(dir string, format string, args ...any)