agaainnn
This commit is contained in:
@@ -27,15 +27,33 @@ func NewGitService() Service {
|
||||
}
|
||||
}
|
||||
|
||||
func shouldUseSSH() bool {
|
||||
_, err := os.Stat("/root/.ssh/id_rsa")
|
||||
return err == nil
|
||||
func prepareSSHKey() (string, bool) {
|
||||
b, err := os.ReadFile("/root/.ssh/id_rsa")
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Fix literal escaped newlines and CRLF issues that cause libcrypto errors
|
||||
content := strings.ReplaceAll(string(b), "\\n", "\n")
|
||||
content = strings.ReplaceAll(content, "\r", "")
|
||||
|
||||
// Ensure there is a trailing newline
|
||||
if !strings.HasSuffix(content, "\n") {
|
||||
content += "\n"
|
||||
}
|
||||
|
||||
tmpPath := "/tmp/id_rsa"
|
||||
if err := os.WriteFile(tmpPath, []byte(content), 0600); err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return tmpPath, true
|
||||
}
|
||||
|
||||
func (s *gitService) CheckConnection(url string) error {
|
||||
cmd := exec.Command("git", "ls-remote", url)
|
||||
if shouldUseSSH() {
|
||||
cmd.Env = append(os.Environ(), "GIT_SSH_COMMAND=ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no")
|
||||
if keyPath, ok := prepareSSHKey(); ok {
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_SSH_COMMAND=ssh -i %s -o StrictHostKeyChecking=no", keyPath))
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("failed to check git connection: %w", err)
|
||||
@@ -52,9 +70,9 @@ func (s *gitService) FetchContents(url string) error {
|
||||
fmt.Println("Cloning repository")
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if shouldUseSSH() {
|
||||
if keyPath, ok := prepareSSHKey(); ok {
|
||||
cmd = exec.Command("git", "clone", url, s.repoDir)
|
||||
cmd.Env = append(os.Environ(), "GIT_SSH_COMMAND=ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no")
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_SSH_COMMAND=ssh -i %s -o StrictHostKeyChecking=no", keyPath))
|
||||
} else {
|
||||
cmd = exec.Command("git", "clone", url, s.repoDir)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user