| 90 | } |
| 91 | |
| 92 | func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Writer) (bool, error) { |
| 93 | if len(args) == 0 { |
| 94 | return false, errors.New("too few arguments in list") |
| 95 | } |
| 96 | |
| 97 | var exe string |
| 98 | extName := args[0] |
| 99 | forwardArgs := args[1:] |
| 100 | |
| 101 | exts, _ := m.list(false) |
| 102 | var ext *Extension |
| 103 | for _, e := range exts { |
| 104 | if e.Name() == extName { |
| 105 | ext = e |
| 106 | exe = ext.Path() |
| 107 | break |
| 108 | } |
| 109 | } |
| 110 | if exe == "" { |
| 111 | return false, nil |
| 112 | } |
| 113 | |
| 114 | var externalCmd *exec.Cmd |
| 115 | |
| 116 | if ext.IsBinary() || runtime.GOOS != "windows" { |
| 117 | externalCmd = m.newCommand(exe, forwardArgs...) |
| 118 | } else if runtime.GOOS == "windows" { |
| 119 | // Dispatch all extension calls through the `sh` interpreter to support executable files with a |
| 120 | // shebang line on Windows. |
| 121 | shExe, err := m.findSh() |
| 122 | if err != nil { |
| 123 | if errors.Is(err, exec.ErrNotFound) { |
| 124 | return true, errors.New("the `sh.exe` interpreter is required. Please install Git for Windows and try again") |
| 125 | } |
| 126 | return true, err |
| 127 | } |
| 128 | forwardArgs = append([]string{"-c", `command "$@"`, "--", exe}, forwardArgs...) |
| 129 | externalCmd = m.newCommand(shExe, forwardArgs...) |
| 130 | } |
| 131 | // Signal to the extension that it is being run by gh rather than standalone, so it can |
| 132 | // adjust things like usage strings. |
| 133 | externalCmd.Env = append(externalCmd.Environ(), "GH_EXTENSION=1") |
| 134 | |
| 135 | externalCmd.Stdin = stdin |
| 136 | externalCmd.Stdout = stdout |
| 137 | externalCmd.Stderr = stderr |
| 138 | return true, externalCmd.Run() |
| 139 | } |
| 140 | |
| 141 | func (m *Manager) List() []extensions.Extension { |
| 142 | exts, _ := m.list(false) |