summaryrefslogtreecommitdiff
path: root/redoGoMod.go
blob: 4593b2f0a38f6ab17e890ae0a059e40ad5ac3d58 (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
package main

// recreates the go.mod and go.sum files

import (
	"errors"
	"os"
	"strings"

	"github.com/go-cmd/cmd"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// remove every go.mod and go.sum
// testing to see where this stuff is coming from
func eraseGoMod(repo *gitpb.Repo) {
	// unset the go development ENV var to generate release files
	if err := repo.StrictRun([]string{"rm", "-f", "go.mod", "go.sum"}); err != nil {
		log.Warn(repo.GetGoPath(), "rm go.mod go.sum failed", err)
	}
}

// sets the required golang version in go.mod
func setGoVersion(repo *gitpb.Repo, version string) error {
	if err := repo.StrictRun([]string{"go", "mod", "edit", "-go=" + version}); err != nil {
		log.Warn(repo.GetGoPath(), "go mod edit failed", err)
		return err
	}
	return nil
}

func goTidy(fullpath string) (cmd.Status, error) {
	if result, err := runVerbose(fullpath, []string{"go", "mod", "tidy", "-go=" + golangVersion}); err == nil {
		return result, nil
	} else {
		return result, err
	}
}

// wrapper around 'go mod init' and 'go mod tidy'
func redoGoMod(repo *gitpb.Repo) error {
	// unset the go development ENV var to generate release files
	os.Unsetenv("GO111MODULE")
	if err := repo.StrictRun([]string{"rm", "-f", "go.mod", "go.sum"}); err != nil {
		log.Warn("rm go.mod go.sum failed", err)
		return err
	}
	if err := repo.StrictRun([]string{"go", "mod", "init", repo.GetGoPath()}); err != nil {
		log.Warn("go mod init failed", err)
		return err
	}
	if result, err := goTidy(repo.FullPath); err != nil {
		if tinyFixer(result) {
			if _, err := goTidy(repo.FullPath); err != nil {
				return err
			}
		}
	}

	// most things should build with golang after 1.21 // todo: allow this to be set somewhere
	if err := setGoVersion(repo, golangVersion); err != nil {
		log.Warn(repo.GetGoPath(), "go mod edit failed", err)
		return err
	}

	repo.GoDeps = nil
	repo.SetGoPrimitive(false)

	// if there is not a go.sum file, it better be a primitive golang project
	if !repo.Exists("go.sum") {
		// todo. fix this logic
		err := repo.SetPrimitive()
		if err != nil {
			// this means this repo does not depend on any other package
			log.Info("PRIMATIVE repo error:", repo.GetGoPath(), "err =", err)
			return err
		}
	}

	// now check if GoPrimitive is true
	if repo.GetGoPrimitive() {
		// perfect!
		return nil
	}

	// well, it's not a primitive and we are still missing the go.sum file
	if !repo.Exists("go.sum") {
		// this means something else went wrong!
		// display the go.mod file and try to figure out what happened
		// maybe the format of go.mod changed in some future version of golang
		data, _ := repo.ReadFile("go.mod")
		log.Info(string(data))
		return errors.New("missing go.sum file on non-primitive go.mod")
	}

	repo.GoDeps = new(gitpb.GoDeps)
	// return the attempt to parse go.sum
	_, err := repo.ParseGoSum()
	return err
}

func tinyFixer(result cmd.Status) bool {
	for _, line := range result.Stdout {
		if strings.Contains(line, "requires go@") {
			log.Info("tinyFixer:", line)
			parts := strings.Split(line, "requires go@")
			if len(parts) == 2 {
				parts = strings.Split(parts[1], ",")
				golangVersion = parts[0]
				return true
			}
			log.Info("tinyFixer:", line, "golangVersion", golangVersion)
		}
	}
	return false
}