From 86870c22db770b05eae62ba93c27cc8ae49b48d3 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sat, 14 Mar 2026 12:03:35 -0300 Subject: [PATCH] agaainnn --- mindforge.cronjob/internal/git/git.go | 32 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/mindforge.cronjob/internal/git/git.go b/mindforge.cronjob/internal/git/git.go index 5ca68dc..cfe962f 100644 --- a/mindforge.cronjob/internal/git/git.go +++ b/mindforge.cronjob/internal/git/git.go @@ -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) }