summaryrefslogtreecommitdiff
path: root/goSrcScan.go
blob: 4e23fed187c987e873dc7eae9964e5652c4c2d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package forgepb

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

	"github.com/destel/rill"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func (f *Forge) ScanGoSrc() (bool, error) {
	dirs, err := gitDirectories(f.goSrc)
	if err != nil {
		return false, err
	}

	var gopaths []string
	for _, dir := range dirs {
		if strings.HasPrefix(dir, f.goSrc) {
			gopath := strings.TrimPrefix(dir, f.goSrc)
			gopath = strings.Trim(gopath, "/")
			gopaths = append(gopaths, gopath)
		} else {
			log.Log(FORGEPBWARN, "ScanGoSrc() bad:", dir)
			return false, errors.New("forgepb.ScanGoSrc() bad dir: " + dir)
		}
	}
	f.rillScanDirs(gopaths)

	/*
		for _, dir := range dirs {
			if strings.HasPrefix(dir, f.goSrc) {
				gopath := strings.TrimPrefix(dir, f.goSrc)
				gopath = strings.Trim(gopath, "/")
				repo, err := f.Repos.NewGoPath(f.goSrc, gopath)
			} else {
				log.Log(FORGEPBWARN, "ScanGoSrc() bad:", dir)
			}
		}
	*/
	return true, err
}

func gitDirectories(srcDir string) ([]string, error) {
	var all []string
	err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Log(FORGEPBWARN, "Error accessing path:", path, err)
			return nil
		}

		// Check if the path is a directory and has a .git subdirectory
		if info.IsDir() && IsGitDir(path) {
			all = append(all, path)
		}

		return nil
	})

	if err != nil {
		log.Log(FORGEPBWARN, "Error walking the path:", srcDir, err)
	}

	return all, err
}

// IsGitDir checks if a .git directory exists inside the given directory
func IsGitDir(dir string) bool {
	gitDir := filepath.Join(dir, ".git")
	info, err := os.Stat(gitDir)
	if os.IsNotExist(err) {
		return false
	}
	return info.IsDir()
}

/*
// rill is awesome. long live rill
func rillAddDirs(gopaths []string) {
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(gopaths, nil)

	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, 20, func(id string) (*repolist.RepoRow, error) {
		return me.repos.View.FindByName(id), nil
	})

	// Activate users.
	// Concurrency = 10
	err := rill.ForEach(dirs, 10, func(repo *repolist.RepoRow) error {
		fmt.Printf("Repo found : %s\n", repo.GoPath())
		repo.Run([]string{"git", "pull"})
		return nil
	})

	// Handle errors
	fmt.Println("Error:", err)
}
*/

// rill is awesome. long live rill
// attempt scan with rill
func (f *Forge) rillScanDirs(gopaths []string) error {
	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(gopaths, nil)

	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, 20, func(id string) (*gitpb.Repo, error) {
		return f.Repos.NewGoPath(f.goSrc, id)
	})

	// Activate users.
	// Concurrency = 10
	err := rill.ForEach(dirs, 10, func(repo *gitpb.Repo) error {
		// could do something here
		// fmt.Printf("Repo found : %s\n", repo.GoPath)
		// repo.Run([]string{"git", "pull"})
		return nil
	})

	return err
}