Skip to content
Myr

proxy.go

This file

Path

src/proxy.go

161 lines · 4.0 KiB · Go

Last changed by

Nullify unwanted FZF_DEFAULT_* variables in tmux popupJunegunn Choibefore adoptionaefb9a5

History of this file

Take it

The walk stopped after 200 commits: lines older than 3b0f976 point at it.

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 1–61why, one step back
1package fzf
23import (4 "bufio"5 "errors"6 "fmt"7 "io"8 "os"9 "os/exec"10 "os/signal"11 "path/filepath"12 "regexp"13 "strings"14 "time"1516 "github.com/junegunn/fzf/src/tui"17 "github.com/junegunn/fzf/src/util"18)1920const becomeSuffix = ".become"2122func escapeSingleQuote(str string) string {23 return "'" + strings.ReplaceAll(str, "'", "'\\''") + "'"24}2526func fifo(name string) (string, error) {27 ns := time.Now().UnixNano()28 output := filepath.Join(os.TempDir(), fmt.Sprintf("fzf-%s-%d", name, ns))29 output, err := mkfifo(output, 0600)30 if err != nil {31 return output, err32 }33 return output, nil34}3536func runProxy(commandPrefix string, cmdBuilder func(temp string, needBash bool) (*exec.Cmd, error), opts *Options, withExports bool) (int, error) {37 output, err := fifo("proxy-output")38 if err != nil {39 return ExitError, err40 }41 defer os.Remove(output)4243 // Take the output44 go func() {45 withOutputPipe(output, func(outputFile io.ReadCloser) {46 if opts.Output == nil {47 io.Copy(os.Stdout, outputFile)48 } else {49 reader := bufio.NewReader(outputFile)50 sep := opts.PrintSep[0]51 for {52 item, err := reader.ReadString(sep)53 if err != nil {54 break55 }56 opts.Output <- item57 }58 }59 })60 }()61

Remove temp files before 'become' when using --tmux optionJunegunn Choibefore adoptionb7248d4

line 62why, one step back
62 var command, input string

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 63–66why, one step back
63 commandPrefix += ` --no-force-tty-in --proxy-script "$0"`
64 if opts.Input == nil && (opts.ForceTtyIn || util.IsTty(os.Stdin)) {65 command = fmt.Sprintf(`%s > %q`, commandPrefix, output)66 } else {

Remove temp files before 'become' when using --tmux optionJunegunn Choibefore adoptionb7248d4

line 67why, one step back
67 input, err = fifo("proxy-input")

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 68–92why, one step back
68 if err != nil {
69 return ExitError, err70 }71 defer os.Remove(input)7273 go func() {74 withInputPipe(input, func(inputFile io.WriteCloser) {75 if opts.Input == nil {76 io.Copy(inputFile, os.Stdin)77 } else {78 for item := range opts.Input {79 fmt.Fprint(inputFile, item+opts.PrintSep)80 }81 }82 })83 }()8485 if withExports {86 command = fmt.Sprintf(`%s < %q > %q`, commandPrefix, input, output)87 } else {88 // For mintty: cannot directly read named pipe from Go code89 command = fmt.Sprintf(`command cat %q | %s > %q`, input, commandPrefix, output)90 }91 }92

Nullify unwanted FZF_DEFAULT_* variables in tmux popupJunegunn Choibefore adoptionaefb9a5

lines 93–95why, one step back
93 // * Write the command to a temporary file and run it with sh to ensure POSIX compliance.
94 // * Nullify FZF_DEFAULT_* variables as tmux popup may inject them even when undefined.95 exports := []string{"FZF_DEFAULT_COMMAND=", "FZF_DEFAULT_OPTS=", "FZF_DEFAULT_OPTS_FILE="}

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 96–150why, one step back
96 needBash := false
97 if withExports {98 validIdentifier := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)99 for _, pairStr := range os.Environ() {100 pair := strings.SplitN(pairStr, "=", 2)101 if validIdentifier.MatchString(pair[0]) {102 exports = append(exports, fmt.Sprintf("export %s=%s", pair[0], escapeSingleQuote(pair[1])))103 } else if strings.HasPrefix(pair[0], "BASH_FUNC_") && strings.HasSuffix(pair[0], "%%") {104 name := pair[0][10 : len(pair[0])-2]105 exports = append(exports, name+pair[1])106 exports = append(exports, "export -f "+name)107 needBash = true108 }109 }110 }111 temp := WriteTemporaryFile(append(exports, command), "\n")112 defer os.Remove(temp)113114 cmd, err := cmdBuilder(temp, needBash)115 if err != nil {116 return ExitError, err117 }118 cmd.Stderr = os.Stderr119 intChan := make(chan os.Signal, 1)120 defer close(intChan)121 go func() {122 if sig, valid := <-intChan; valid {123 cmd.Process.Signal(sig)124 }125 }()126 signal.Notify(intChan, os.Interrupt)127 if err := cmd.Run(); err != nil {128 if exitError, ok := err.(*exec.ExitError); ok {129 code := exitError.ExitCode()130 if code == ExitBecome {131 becomeFile := temp + becomeSuffix132 data, err := os.ReadFile(becomeFile)133 os.Remove(becomeFile)134 if err != nil {135 return ExitError, err136 }137 elems := strings.Split(string(data), "\x00")138 if len(elems) < 1 {139 return ExitError, errors.New("invalid become command")140 }141 command := elems[0]142 env := []string{}143 if len(elems) > 1 {144 env = elems[1:]145 }146 executor := util.NewExecutor(opts.WithShell)147 ttyin, err := tui.TtyIn()148 if err != nil {149 return ExitError, err150 }

Remove temp files before 'become' when using --tmux optionJunegunn Choibefore adoptionb7248d4

lines 151–153why, one step back
151 os.Remove(temp)
152 os.Remove(input)153 os.Remove(output)

[fish] Enable home dir expansion of leading ~/bitraidbefore adoption3b0f976

lines 154–161why, one step back
154 executor.Become(ttyin, env, command)
155 }156 return code, err157 }158 }159160 return ExitOk, nil161}