| 128 | } |
| 129 | |
| 130 | func ExecWithEnv(opts ExecOpts) error { |
| 131 | var userEnv []string |
| 132 | if opts.User != "" { |
| 133 | userEnv = UserEnv(opts.User) |
| 134 | SwitchUser(opts.User) |
| 135 | } |
| 136 | |
| 137 | if runtime.GOOS == "windows" && opts.SameProcess { |
| 138 | return fmt.Errorf("The --same-process flag is not supported on Windows") |
| 139 | } |
| 140 | |
| 141 | var env []string |
| 142 | |
| 143 | if !opts.Pristine { |
| 144 | env = os.Environ() |
| 145 | } |
| 146 | |
| 147 | lines := bytes.Split(opts.Plaintext, []byte("\n")) |
| 148 | for _, line := range lines { |
| 149 | if len(line) == 0 { |
| 150 | continue |
| 151 | } |
| 152 | if line[0] == '#' { |
| 153 | continue |
| 154 | } |
| 155 | env = append(env, string(line)) |
| 156 | } |
| 157 | |
| 158 | env = append(env, userEnv...) |
| 159 | env = append(env, opts.Env...) |
| 160 | |
| 161 | if opts.SameProcess { |
| 162 | if opts.Background { |
| 163 | log.Fatal("background is not supported for same-process") |
| 164 | } |
| 165 | |
| 166 | // Note that the call does NOT return, unless an error happens. |
| 167 | return ExecSyscall(opts.Command, env) |
| 168 | } |
| 169 | |
| 170 | cmd := BuildCommand(opts.Command) |
| 171 | cmd.Env = env |
| 172 | |
| 173 | if opts.Background { |
| 174 | return cmd.Start() |
| 175 | } |
| 176 | |
| 177 | cmd.Stdin = os.Stdin |
| 178 | cmd.Stdout = os.Stdout |
| 179 | cmd.Stderr = os.Stderr |
| 180 | |
| 181 | return cmd.Run() |
| 182 | } |