Skip to content

Commit f0b3300

Browse files
committed
Progress
1 parent f39ea25 commit f0b3300

8 files changed

Lines changed: 96 additions & 260 deletions

File tree

cmd.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

cmd/gitops.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"gitte/config"
6+
"gitte/internal"
67
"os"
78

89
"github.com/spf13/cobra"
@@ -12,22 +13,24 @@ import (
1213
var gitopsCmd = &cobra.Command{
1314
Use: "gitops",
1415
Short: "GitOps refer to GitOperations and will ensure that the enabled projects are cloned and up to date if possible.",
15-
Run: func(cmd *cobra.Command, args []string) {
16+
RunE: func(cmd *cobra.Command, args []string) error {
1617
fmt.Println("gitops called")
1718
fs := os.DirFS(".")
1819
fd, err := config.ResolveGitteDir(fs)
1920
if err != nil {
20-
fmt.Println("Error resolving .gitte dir:", err)
21-
return
21+
return fmt.Errorf("error resolving gitte dir: %w", err)
2222
}
2323

2424
gitteConfig, err := config.LoadConfig(fd)
2525
if err != nil {
26-
fmt.Println("Error loading gitte config:", err)
27-
return
26+
return fmt.Errorf("error loading gitte config: %w", err)
2827
}
2928

30-
fmt.Println("Loaded Gitte Config:", gitteConfig)
29+
//fmt.Println("Loaded Gitte Config:", gitteConfig)
30+
31+
cwd := "/home/lejo/gitte-test" // TODO move config loading and cwd detection to shared place
32+
33+
return internal.GitOps(cmd.Context(), cwd, *gitteConfig)
3134
},
3235
}
3336

executor/cmd.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ type ExecuteResult struct {
1212
Stderr []byte
1313
}
1414

15-
func ExecuteSync(command string, args ...string) (*ExecuteResult, error) {
15+
// ExecuteSyncInDir executes a command synchronously in the specified directory (cwd).
16+
// If cwd is an empty string, it executes in the current working directory.
17+
// It returns an ExecuteResult containing the exit code, stdout, and stderr.
18+
// It will only return an error if there was a problem starting or running the command itself. Not if the command exits with a non-zero exit code.
19+
func ExecuteSyncInDir(cwd string, command string, args ...string) (*ExecuteResult, error) {
1620
cmd := exec.Command(command, args...)
21+
22+
cmd.Dir = cwd
1723
var stdout bytes.Buffer
1824
var stderr bytes.Buffer
1925

@@ -42,3 +48,7 @@ func ExecuteSync(command string, args ...string) (*ExecuteResult, error) {
4248
Stderr: stderr.Bytes(),
4349
}, nil
4450
}
51+
52+
func ExecuteSync(command string, args ...string) (*ExecuteResult, error) {
53+
return ExecuteSyncInDir("", command, args...)
54+
}

gitops.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

internal/gitops.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"gitte/config"
7+
"gitte/executor"
8+
"os"
9+
"regexp"
10+
)
11+
12+
func GitOps(ctx context.Context, cwd string, gitteConfig config.GitteConfig) error {
13+
for _, pc := range gitteConfig.Projects {
14+
if err := gitopsProject(ctx, cwd, pc); err != nil {
15+
return err
16+
}
17+
}
18+
return nil
19+
}
20+
21+
func gitopsProject(ctx context.Context, cwd string, pc config.ProjectConfig) error {
22+
relativeDirectory, err := getProjectDirFromRemote(pc)
23+
if err != nil {
24+
return err
25+
}
26+
27+
projectPath := fmt.Sprintf("%s/%s", cwd, relativeDirectory) // TODO join prettier
28+
29+
// Check if directory exists
30+
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
31+
// Directory does not exist, clone the repository
32+
fmt.Println("Cloning repository:", pc.Remote, "into", projectPath)
33+
return clone(ctx, cwd, pc)
34+
}
35+
return nil // TODO pull and update
36+
}
37+
38+
func clone(ctx context.Context, cwd string, pc config.ProjectConfig) error {
39+
relativeDirectory, err := getProjectDirFromRemote(pc)
40+
if err != nil {
41+
return err
42+
}
43+
44+
res, err := executor.ExecuteSyncInDir(cwd, "git", "clone", pc.Remote, relativeDirectory)
45+
if err != nil {
46+
return fmt.Errorf("failed to execute git clone command: %w", err)
47+
}
48+
49+
if res.ExitCode != 0 {
50+
// If res.Stderr contains "Permission denied" return a specific error
51+
if regexp.MustCompile(`(?i)permission denied`).Match(res.Stderr) {
52+
return fmt.Errorf("permission denied when trying to clone repository '%s'. Please check your SSH keys and access rights", pc.Remote)
53+
}
54+
return fmt.Errorf("git clone command failed with exit code %d: \n%s", res.ExitCode, string(res.Stderr))
55+
}
56+
57+
return nil
58+
}
59+
60+
func getProjectDirFromRemote(pc config.ProjectConfig) (string, error) {
61+
// const match = remote.match(/git@.*?:.*?\.git/);
62+
63+
regex := regexp.MustCompile(`git@.*?:(.*)?\.git`)
64+
match := regex.FindStringSubmatch(pc.Remote)
65+
66+
firstMatch := ""
67+
if len(match) > 1 {
68+
firstMatch = match[1]
69+
}
70+
71+
if firstMatch == "" {
72+
return "", fmt.Errorf("'%' is not a valid git ssh url. Use git@gitlab.com:example/cego.git syntax", pc.Remote)
73+
}
74+
75+
return firstMatch, nil
76+
}

loader.go

Lines changed: 0 additions & 127 deletions
This file was deleted.

main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
3-
4-
*/
51
package main
62

73
import "gitte/cmd"

0 commit comments

Comments
 (0)