11package command
22
33import (
4- "errors"
54 "fmt"
65 "os"
7- "os/exec"
8- "path/filepath"
9- "regexp"
10- "runtime"
116 "runtime/debug"
127 "strings"
138
149 "github.com/cli/cli/api"
15- "github.com/cli/cli/context"
1610 "github.com/cli/cli/internal/config"
1711 "github.com/cli/cli/internal/ghinstance"
18- "github.com/cli/cli/internal/run"
19- "github.com/cli/cli/pkg/cmd/factory"
20- "github.com/cli/cli/pkg/cmd/root"
2112 "github.com/cli/cli/utils"
22- "github.com/google/shlex"
23- "github.com/spf13/cobra"
2413)
2514
2615// Version is dynamically set by the toolchain or overridden by the Makefile.
@@ -29,22 +18,12 @@ var Version = "DEV"
2918// BuildDate is dynamically set at build time in the Makefile.
3019var BuildDate = "" // YYYY-MM-DD
3120
32- var RootCmd * cobra.Command
33-
3421func init () {
3522 if Version == "DEV" {
3623 if info , ok := debug .ReadBuildInfo (); ok && info .Main .Version != "(devel)" {
3724 Version = info .Main .Version
3825 }
3926 }
40-
41- cmdFactory := factory .New (Version )
42- RootCmd = root .NewCmdRoot (cmdFactory , Version , BuildDate )
43- }
44-
45- // overridden in tests
46- var initContext = func () context.Context {
47- return context .New ()
4827}
4928
5029// BasicClient returns an API client for github.com only that borrows from but
@@ -73,110 +52,3 @@ func apiVerboseLog() api.ClientOption {
7352 colorize := utils .IsTerminal (os .Stderr )
7453 return api .VerboseLog (utils .NewColorable (os .Stderr ), logTraffic , colorize )
7554}
76-
77- func ExecuteShellAlias (args []string ) error {
78- externalCmd := exec .Command (args [0 ], args [1 :]... )
79- externalCmd .Stderr = os .Stderr
80- externalCmd .Stdout = os .Stdout
81- externalCmd .Stdin = os .Stdin
82- preparedCmd := run .PrepareCmd (externalCmd )
83-
84- return preparedCmd .Run ()
85- }
86-
87- var findSh = func () (string , error ) {
88- shPath , err := exec .LookPath ("sh" )
89- if err == nil {
90- return shPath , nil
91- }
92-
93- if runtime .GOOS == "windows" {
94- winNotFoundErr := errors .New ("unable to locate sh to execute the shell alias with. The sh.exe interpreter is typically distributed with Git for Windows." )
95- // We can try and find a sh executable in a Git for Windows install
96- gitPath , err := exec .LookPath ("git" )
97- if err != nil {
98- return "" , winNotFoundErr
99- }
100-
101- shPath = filepath .Join (filepath .Dir (gitPath ), ".." , "bin" , "sh.exe" )
102- _ , err = os .Stat (shPath )
103- if err != nil {
104- return "" , winNotFoundErr
105- }
106-
107- return shPath , nil
108- }
109-
110- return "" , errors .New ("unable to locate sh to execute shell alias with" )
111- }
112-
113- // ExpandAlias processes argv to see if it should be rewritten according to a user's aliases. The
114- // second return value indicates whether the alias should be executed in a new shell process instead
115- // of running gh itself.
116- func ExpandAlias (args []string ) (expanded []string , isShell bool , err error ) {
117- err = nil
118- isShell = false
119- expanded = []string {}
120-
121- if len (args ) < 2 {
122- // the command is lacking a subcommand
123- return
124- }
125-
126- ctx := initContext ()
127- cfg , err := ctx .Config ()
128- if err != nil {
129- return
130- }
131- aliases , err := cfg .Aliases ()
132- if err != nil {
133- return
134- }
135-
136- expansion , ok := aliases .Get (args [1 ])
137- if ok {
138- if strings .HasPrefix (expansion , "!" ) {
139- isShell = true
140- shPath , shErr := findSh ()
141- if shErr != nil {
142- err = shErr
143- return
144- }
145-
146- expanded = []string {shPath , "-c" , expansion [1 :]}
147-
148- if len (args [2 :]) > 0 {
149- expanded = append (expanded , "--" )
150- expanded = append (expanded , args [2 :]... )
151- }
152-
153- return
154- }
155-
156- extraArgs := []string {}
157- for i , a := range args [2 :] {
158- if ! strings .Contains (expansion , "$" ) {
159- extraArgs = append (extraArgs , a )
160- } else {
161- expansion = strings .ReplaceAll (expansion , fmt .Sprintf ("$%d" , i + 1 ), a )
162- }
163- }
164- lingeringRE := regexp .MustCompile (`\$\d` )
165- if lingeringRE .MatchString (expansion ) {
166- err = fmt .Errorf ("not enough arguments for alias: %s" , expansion )
167- return
168- }
169-
170- var newArgs []string
171- newArgs , err = shlex .Split (expansion )
172- if err != nil {
173- return
174- }
175-
176- expanded = append (newArgs , extraArgs ... )
177- return
178- }
179-
180- expanded = args [1 :]
181- return
182- }
0 commit comments