Debian Codegraph @

internal/command/cmd.go

raw symbols SCIP
1 package commandscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/
2
3 import (
4 "logscip-go gomod github.com/golang/go/src go1.16 log/"
5 "os/execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/"
6 "stringsscip-go gomod github.com/golang/go/src go1.16 strings/"
7 "syscallscip-go gomod github.com/golang/go/src go1.16 syscall/"
8 )
9
10 // Mode indicates that whether command should run native code or use git
11 // command to operate.
12 type Modescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/Mode# uint8
13
14 const (
15 // ModeLegacy uses traditional git command line tool to operate
16 ModeLegacyscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/ModeLegacy. = iota
17 // ModeNative uses native implementation of given git command
18 ModeNativescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/ModeNative.
19 )
20
21 // Run runs the OS command and return its output. If the output
22 // returns error it also encapsulates it as a golang.error which is a return code
23 // of the command except zero
24 func Runscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/Run().(d string, c string, args []string) (string, error) {
25 cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().(c, args...)
26 if d != "" {
27 cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = d
28 }
29 output, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#CombinedOutput().()
30 return trimTrailingNewlinescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/trimTrailingNewline().(string(output)), err
31 }
32
33 // Return returns if we supposed to get return value as an int of a command
34 // this method can be used. It is practical when you use a command and process a
35 // failover according to a specific return code
36 func Returnscip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/Return().(d string, c string, args []string) (int, error) {
37 cmd := execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Command().(c, args...)
38 if d != "" {
39 cmd.Dirscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Dir. = d
40 }
41 var err error
42 // this time the execution is a little different
43 if err := cmd.Startscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Start().(); err != nil {
44 return -1, err
45 }
46 if err := cmd.Waitscip-go gomod github.com/golang/go/src go1.16 `os/exec`/Cmd#Wait().(); err != nil {
47 if exiterr, ok := err.(*execscip-go gomod github.com/golang/go/src go1.16 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.16 `os/exec`/ExitError#); ok {
48 // The program has exited with an exit code != 0
49
50 // This works on both Unix and Windows. Although package
51 // syscall is generally platform dependent, WaitStatus is
52 // defined for both Unix and Windows and in both cases has
53 // an ExitStatus() method with the same signature.
54 if status, ok := exiterr.Sysscip-go gomod github.com/golang/go/src go1.16 os/ProcessState#Sys().().(syscallscip-go gomod github.com/golang/go/src go1.16 syscall/.WaitStatusscip-go gomod github.com/golang/go/src go1.16 syscall/WaitStatus#); ok {
55 statusCode := status.ExitStatusscip-go gomod github.com/golang/go/src go1.16 syscall/WaitStatus#ExitStatus().()
56 return statusCode, err
57 }
58 } else {
59 logscip-go gomod github.com/golang/go/src go1.16 log/.Fatalfscip-go gomod github.com/golang/go/src go1.16 log/Fatalf().("cmd.Wait: %v", err)
60 }
61 }
62 return -1, err
63 }
64
65 // trimTrailingNewline removes the trailing new line form a string. this method
66 // is used mostly on outputs of a command
67 func trimTrailingNewlinescip-go gomod github.com/isacikgoz/gitbatch . `github.com/isacikgoz/gitbatch/internal/command`/trimTrailingNewline().(s string) string {
68 if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.16 strings/HasSuffix().(s, "\n") || stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.16 strings/HasSuffix().(s, "\r") {
69 return s[:len(s)-1]
70 }
71 return s
72 }