summaryrefslogtreecommitdiff
path: root/main.go
blob: cc3d81b8a4e67c55229e3052c05ac16ea76d6f60 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

// An app to submit patches for the 30 GO GUI repos

import (
	"embed"
	"errors"
	"path/filepath"

	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// at build time, this can be used to store GUI plugins with matching GO deps
//
//go:embed resources/*
var resources embed.FS

func findTagname(stats *gitpb.Stats, brname string) *gitpb.Stat {
	ref := stats.FindByName(brname)
	if ref != nil {
		return ref
	}
	ref = stats.FindByName(filepath.Join("refs/heads", brname))
	if ref != nil {
		return ref
	}
	//	for r := range stats.IterAll() {
	//		if strings.HasPrefix(r.Name, "refs/tags/") {
	//			continue
	//		}
	//		log.Printf("brname(%s) ref(%s)\n", brname, r.Name)
	//	}
	return nil
}

// true if something changed
func setStats(repo *gitpb.Repo) error {
	var err error
	stats, err := repo.LoadRefs()
	if err != nil {
		log.Printf("%s LoadRefs() err(%v)\n", stats.Filename, err)
		return err
	}
	// log.Printf("%s LoadRefs() len(%d)\n", stats.Filename, stats.Len())

	var brname string
	var ref *gitpb.Stat
	// brname = me.forge.Config.FindMasterBranch(repo.Namespace)
	brname = repo.GetMasterBranchName()
	ref = findTagname(stats, brname)
	if repo.MasterStat == nil {
		if ref != nil {
			err = errors.New("master stat was nil")
		}
	}
	repo.MasterStat = ref

	// brname = me.forge.Config.FindDevelBranch(repo.Namespace)
	brname = repo.GetDevelBranchName()
	ref = findTagname(stats, brname)
	if repo.DevelStat == nil {
		if ref != nil {
			err = errors.New("devel stat was nil")
		}
	}
	repo.DevelStat = ref

	// brname = me.forge.Config.FindUserBranch(repo.Namespace)
	brname = repo.GetUserBranchName()
	ref = findTagname(stats, brname)
	if repo.UserStat == nil {
		if ref != nil {
			err = errors.New("user stat was nil")
		}
	}
	repo.UserStat = ref
	if repo.Tags.Len() != stats.Len() {
		log.Printf("tags Tags.len(%d) vs stats.Len(%d)\n", repo.Tags.Len(), stats.Len())
		repo.Tags = stats
		err = errors.New("adding stats to repo.Tags")
	} else {
		// log.Printf("tags already here with len(%d)\n", repo.Tags.Len())
	}

	return err
}

func doCoreChecks() {
	me.forge.RescanRepos() // looks for new dirs, checks existing repos for changes

	// if you are in "normal" mode, always run normal every time to catch accidental errors
	// for example, if you accidentally changed branches from your user branch
	if me.forge.IsModeNormal() {
		// show what is still normal and what is not
		if doNormalStatus() {
			// log.Info("things are still normal")
		} else {
			log.Info("doNormalStatus() failed. things are not normal anymore")
			me.forge.SetUserMode()
		}
	} else {
		if !me.forge.IsModeNormal() {
		}
		if me.forge.IsModeDevel() {
			dumpDebug()
		}
	}

	var trip bool
	for repo := range me.forge.Repos.IterAll() {
		if err := setStats(repo); err != nil {
			log.Printf("%s err(%v)\n", repo.Namespace, err)
			trip = true
		}
	}
	if trip {
		me.forge.Repos.SaveVerbose()
	}
}

func main() {
	var s string
	var err error
	me = new(mainType)
	argvpb.Init(&argv, APPNAME, BUILDTIME, VERSION) // adds shell auto-complete
	me.forge, err = forgepb.Init()
	if err != nil {
		log.Printf("forge failure on Init err=(%v)\n", err)
		argvpb.BadExit("WTF", err)
	}

	// allow Dev subcommands to run before the normal flow of the application
	if argv.Dev != nil {
		s, err = doDev()
		if err != nil {
			argvpb.BadExit(s, err)
		}
		argvpb.GoodExit(s)
	}

	if me.forge.IsModeUnknown() || me.forge.IsModeNewUser() {
		doNewUser()
	}

	// put things to do every time forge runs here
	doCoreChecks()

	if argvpb.GetCmd() == "" {
		// no command line arguments were given
		// do the default behavior and exit
		s, err := doDefaultBehavior()
		if err != nil {
			argvpb.BadExit(s, err)
		}
		argvpb.GoodExit(s)
	}

	log.Info("Starting forge with subcommand:", argvpb.Real())
	s, err = doSubcommand()

	// if the gui starts, it doesn't yet go to the end normally
	if argvpb.GetCmd() == "gui" {
		me.myGui.Start() // loads the GUI toolkit
		doGui()          // start making our forge GUI
		debug()          // sits here forever
	}

	// safe exits back to your shell (with timing and toolkit close)
	if err != nil {
		argvpb.BadExit(s, err)
	}
	argvpb.GoodExit(s)
}