github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/script/state.go (about) 1 // Copyright 2022 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 script 6 7 import ( 8 "bytes" 9 "context" 10 "fmt" 11 "io" 12 "io/fs" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "regexp" 17 "strings" 18 19 "github.com/go-asm/go/txtar" 20 ) 21 22 // A State encapsulates the current state of a running script engine, 23 // including the script environment and any running background commands. 24 type State struct { 25 engine *Engine // the engine currently executing the script, if any 26 27 ctx context.Context 28 cancel context.CancelFunc 29 file string 30 log bytes.Buffer 31 32 workdir string // initial working directory 33 pwd string // current working directory during execution 34 env []string // environment list (for os/exec) 35 envMap map[string]string // environment mapping (matches env) 36 stdout string // standard output from last 'go' command; for 'stdout' command 37 stderr string // standard error from last 'go' command; for 'stderr' command 38 39 background []backgroundCmd 40 } 41 42 type backgroundCmd struct { 43 *command 44 wait WaitFunc 45 } 46 47 // NewState returns a new State permanently associated with ctx, with its 48 // initial working directory in workdir and its initial environment set to 49 // initialEnv (or os.Environ(), if initialEnv is nil). 50 // 51 // The new State also contains pseudo-environment-variables for 52 // ${/} and ${:} (for the platform's path and list separators respectively), 53 // but does not pass those to subprocesses. 54 func NewState(ctx context.Context, workdir string, initialEnv []string) (*State, error) { 55 absWork, err := filepath.Abs(workdir) 56 if err != nil { 57 return nil, err 58 } 59 60 ctx, cancel := context.WithCancel(ctx) 61 62 // Make a fresh copy of the env slice to avoid aliasing bugs if we ever 63 // start modifying it in place; this also establishes the invariant that 64 // s.env contains no duplicates. 65 env := cleanEnv(initialEnv, absWork) 66 67 envMap := make(map[string]string, len(env)) 68 69 // Add entries for ${:} and ${/} to make it easier to write platform-independent 70 // paths in scripts. 71 envMap["/"] = string(os.PathSeparator) 72 envMap[":"] = string(os.PathListSeparator) 73 74 for _, kv := range env { 75 if k, v, ok := strings.Cut(kv, "="); ok { 76 envMap[k] = v 77 } 78 } 79 80 s := &State{ 81 ctx: ctx, 82 cancel: cancel, 83 workdir: absWork, 84 pwd: absWork, 85 env: env, 86 envMap: envMap, 87 } 88 s.Setenv("PWD", absWork) 89 return s, nil 90 } 91 92 // CloseAndWait cancels the State's Context and waits for any background commands to 93 // finish. If any remaining background command ended in an unexpected state, 94 // Close returns a non-nil error. 95 func (s *State) CloseAndWait(log io.Writer) error { 96 s.cancel() 97 wait, err := Wait().Run(s) 98 if wait != nil { 99 panic("script: internal error: Wait unexpectedly returns its own WaitFunc") 100 } 101 if flushErr := s.flushLog(log); err == nil { 102 err = flushErr 103 } 104 return err 105 } 106 107 // Chdir changes the State's working directory to the given path. 108 func (s *State) Chdir(path string) error { 109 dir := s.Path(path) 110 if _, err := os.Stat(dir); err != nil { 111 return &fs.PathError{Op: "Chdir", Path: dir, Err: err} 112 } 113 s.pwd = dir 114 s.Setenv("PWD", dir) 115 return nil 116 } 117 118 // Context returns the Context with which the State was created. 119 func (s *State) Context() context.Context { 120 return s.ctx 121 } 122 123 // Environ returns a copy of the current script environment, 124 // in the form "key=value". 125 func (s *State) Environ() []string { 126 return append([]string(nil), s.env...) 127 } 128 129 // ExpandEnv replaces ${var} or $var in the string according to the values of 130 // the environment variables in s. References to undefined variables are 131 // replaced by the empty string. 132 func (s *State) ExpandEnv(str string, inRegexp bool) string { 133 return os.Expand(str, func(key string) string { 134 e := s.envMap[key] 135 if inRegexp { 136 // Quote to literal strings: we want paths like C:\work\go1.4 to remain 137 // paths rather than regular expressions. 138 e = regexp.QuoteMeta(e) 139 } 140 return e 141 }) 142 } 143 144 // ExtractFiles extracts the files in ar to the state's current directory, 145 // expanding any environment variables within each name. 146 // 147 // The files must reside within the working directory with which the State was 148 // originally created. 149 func (s *State) ExtractFiles(ar *txtar.Archive) error { 150 wd := s.workdir 151 152 // Add trailing separator to terminate wd. 153 // This prevents extracting to outside paths which prefix wd, 154 // e.g. extracting to /home/foobar when wd is /home/foo 155 if wd == "" { 156 panic("s.workdir is unexpectedly empty") 157 } 158 if !os.IsPathSeparator(wd[len(wd)-1]) { 159 wd += string(filepath.Separator) 160 } 161 162 for _, f := range ar.Files { 163 name := s.Path(s.ExpandEnv(f.Name, false)) 164 165 if !strings.HasPrefix(name, wd) { 166 return fmt.Errorf("file %#q is outside working directory", f.Name) 167 } 168 169 if err := os.MkdirAll(filepath.Dir(name), 0777); err != nil { 170 return err 171 } 172 if err := os.WriteFile(name, f.Data, 0666); err != nil { 173 return err 174 } 175 } 176 177 return nil 178 } 179 180 // Getwd returns the directory in which to run the next script command. 181 func (s *State) Getwd() string { return s.pwd } 182 183 // Logf writes output to the script's log without updating its stdout or stderr 184 // buffers. (The output log functions as a kind of meta-stderr.) 185 func (s *State) Logf(format string, args ...any) { 186 fmt.Fprintf(&s.log, format, args...) 187 } 188 189 // flushLog writes the contents of the script's log to w and clears the log. 190 func (s *State) flushLog(w io.Writer) error { 191 _, err := w.Write(s.log.Bytes()) 192 s.log.Reset() 193 return err 194 } 195 196 // LookupEnv retrieves the value of the environment variable in s named by the key. 197 func (s *State) LookupEnv(key string) (string, bool) { 198 v, ok := s.envMap[key] 199 return v, ok 200 } 201 202 // Path returns the absolute path in the host operating system for a 203 // script-based (generally slash-separated and relative) path. 204 func (s *State) Path(path string) string { 205 if filepath.IsAbs(path) { 206 return filepath.Clean(path) 207 } 208 return filepath.Join(s.pwd, path) 209 } 210 211 // Setenv sets the value of the environment variable in s named by the key. 212 func (s *State) Setenv(key, value string) error { 213 s.env = cleanEnv(append(s.env, key+"="+value), s.pwd) 214 s.envMap[key] = value 215 return nil 216 } 217 218 // Stdout returns the stdout output of the last command run, 219 // or the empty string if no command has been run. 220 func (s *State) Stdout() string { return s.stdout } 221 222 // Stderr returns the stderr output of the last command run, 223 // or the empty string if no command has been run. 224 func (s *State) Stderr() string { return s.stderr } 225 226 // cleanEnv returns a copy of env with any duplicates removed in favor of 227 // later values and any required system variables defined. 228 // 229 // If env is nil, cleanEnv copies the environment from os.Environ(). 230 func cleanEnv(env []string, pwd string) []string { 231 // There are some funky edge-cases in this logic, especially on Windows (with 232 // case-insensitive environment variables and variables with keys like "=C:"). 233 // Rather than duplicating exec.dedupEnv here, cheat and use exec.Cmd directly. 234 cmd := &exec.Cmd{Env: env} 235 cmd.Dir = pwd 236 return cmd.Environ() 237 }