新增git 命令运行方法
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func main() {
|
import "github.com/zhuy1228/GitPilot/internal"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
proxyInfo, err := internal.GetCurrentProxy()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
println(proxyInfo.String())
|
||||||
}
|
}
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -1,3 +1,8 @@
|
|||||||
module github.com/zhuy1228/GitPilot
|
module github.com/zhuy1228/GitPilot
|
||||||
|
|
||||||
go 1.25.5
|
go 1.25.5
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
5
go.sum
Normal file
5
go.sum
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
48
internal/git/client.go
Normal file
48
internal/git/client.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zhuy1228/GitPilot/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GitClient struct {
|
||||||
|
Enabled bool
|
||||||
|
Proxy string
|
||||||
|
Timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGitClient() *GitClient {
|
||||||
|
proxy, _ := internal.GetCurrentProxy()
|
||||||
|
return &GitClient{
|
||||||
|
Enabled: proxy.Enabled,
|
||||||
|
Proxy: proxy.Protocol + "://" + proxy.Server,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run 执行 git 命令,支持超时和代理设置
|
||||||
|
func (g *GitClient) Run(path string, args ...string) (string, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
header := []string{"-C", path, "git"}
|
||||||
|
if g.Enabled {
|
||||||
|
header = append(header, "-c", "http.proxy="+g.Proxy, "-c", "https.proxy="+g.Proxy)
|
||||||
|
}
|
||||||
|
argsArr := append(header, args...)
|
||||||
|
cmd := exec.CommandContext(ctx, "git", argsArr...)
|
||||||
|
var stdout, stderr bytes.Buffer
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
err := cmd.Run()
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
return "", fmt.Errorf("git command timeout: git %v", args)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("git command error: %v, stderr: %s", err, stderr.String())
|
||||||
|
}
|
||||||
|
return stdout.String(), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user