summaryrefslogtreecommitdiff
path: root/main.go
blob: 6f15a745063bc56720f188afa0739f76e0fc1af1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main

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

	"go.wit.com/dev/alexflint/arg"
	"go.wit.com/gui"
	"go.wit.com/lib/gui/repolist"
	"go.wit.com/lib/gui/repostatus"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

var VERSION string

var rv *repolist.RepoList
var myargs args

func main() {
	arg.MustParse(&myargs)

	if myargs.Repo == "" {
		// tmp.WriteHelp(os.Stdout)
		// fmt.Println("hello world")
		tmp := myargs.Description()
		fmt.Println(tmp)
		fmt.Println(myargs.Version())
		os.Exit(0)
	}

	wdir, err := findWorkFile()
	if err != nil {
		log.Info(err)
		os.Exit(-1)
	}
	log.Info("scanning directory:", wdir)
	os.Setenv("REPO_WORK_PATH", wdir)

	// readControlFile()

	b := gui.RawBox()
	rv = repolist.AutotypistView(b)

	os.Setenv("REPO_AUTO_CLONE", "true")
	newr, err := rv.NewRepo(myargs.Repo)
	if err != nil {
		log.Info("could not download:", err)
		os.Exit(-1)
	}
	newr.Status.MakeRedomod()

	// rv.NewRepo("go.wit.com/apps/helloworld")
	for _, path := range repostatus.ScanGitDirectories(wdir) {
		gopath := strings.TrimPrefix(path, wdir)
		gopath = strings.Trim(gopath, "/")
		// log.Info("Also should add:", gopath)
		rv.NewRepo(gopath)
	}

	godep := newr.Status.GetGoDeps()
	for gopath, version := range godep {
		repo, err := rv.NewRepo(gopath)
		if err != nil {
			log.Info("git clone failed for", gopath, version)
			continue
		}
		repo.Status.MakeRedomod()
	}

	for _, repo := range rv.AllRepos() {
		if ! repo.Status.Exists("go.mod") {
			repo.Status.MakeRedomod()
		}
	}

	log.Info("all repositories are cloned")
	if !myargs.NoWork {
		log.Info("Creating", wdir+"/go.work")
		rv.MakeGoWork()
		shell.RunPath(wdir, []string{"go", "work", "use"})
	}

	for _, repo := range rv.AllRepos() {
		log.Info("found repo", repo.GoPath(), repo.Status.Path())
	}

}

// look for or make a go.work file
// otherwise use ~/go/src
func findWorkFile() (string, error) {
	pwd, err := os.Getwd()
	if err == nil {
		// Check for go.work in the current directory and then move up until root
		if pwd, err := digup(pwd); err == nil {
			// found an existing go.work file
			os.Chdir(pwd)
			return pwd, nil
		}

		// if the user added '--work' on the cmdline, make a work directory and init the go.work file
		if myargs.Work {
			pwd, err = os.Getwd()
			newpwd := filepath.Join(pwd, "work")
			shell.Mkdir(newpwd)
			os.Chdir(newpwd)
			if _, err := os.Stat("go.work"); err == nil {
				return newpwd, nil
			}
			shell.RunPath(newpwd, []string{"go", "work", "init"})
			if shell.Exists("go.work") {
				return newpwd, nil
			}
		}
	}

	// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
	// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory
	// because I know exactly what is in it: GO stuff & nothing else
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}
	pwd = filepath.Join(homeDir, "go/src")
	shell.Mkdir(pwd)
	os.Chdir(pwd)
	return pwd, nil
}

func digup(path string) (string, error) {
	for {
		workFilePath := filepath.Join(path, "go.work")
		if _, err := os.Stat(workFilePath); err == nil {
			return path, nil // Found the go.work file
		} else if !os.IsNotExist(err) {
			return "", err // An error other than not existing
		}

		parentPath := filepath.Dir(path)
		if parentPath == path {
			break // Reached the filesystem root
		}
		path = parentPath
	}

	return "", fmt.Errorf("no go.work file found")
}