Skip to content

Commit 96a2305

Browse files
committed
Eliminate instance of code duplication
1 parent 744dd61 commit 96a2305

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

cmd/launcher/gui/reporting.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import (
1919
"github.com/setlog/trivrost/cmd/launcher/flags"
2020
)
2121

22-
func ReportFatalError(fatalError error, launcherFlags *flags.LauncherFlags) {
23-
WaitUntilReady()
24-
defer Quit()
25-
PanicInformatively(fatalError, launcherFlags)
26-
}
27-
2822
func HandlePanic(launcherFlags *flags.LauncherFlags) {
2923
if r := recover(); r != nil {
3024
if err, ok := r.(error); ok && errors.Is(err, context.Canceled) {

cmd/launcher/launcher/entry.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ import (
99
log "github.com/sirupsen/logrus"
1010

1111
"github.com/setlog/trivrost/cmd/launcher/flags"
12-
"github.com/setlog/trivrost/cmd/launcher/gui"
1312
)
1413

1514
func LauncherMain(ctx context.Context, launcherFlags *flags.LauncherFlags) {
16-
gui.WaitUntilReady()
17-
defer gui.Quit()
18-
defer gui.HandlePanic(launcherFlags)
19-
2015
places.MakePlaces()
2116
defer Linger()
2217
locking.AcquireLock(ctx, launcherFlags)

cmd/launcher/main.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,12 @@ var gitBranch string
4545

4646
func main() {
4747
defer misc.LogPanic()
48-
launcherFlags, fatalError := initializeEnvironment()
48+
launcherFlags, envErr := initializeEnvironment()
4949
ctx, cancelFunc := context.WithCancel(context.Background())
5050

51-
if fatalError == nil {
52-
go launcher.LauncherMain(ctx, launcherFlags)
53-
} else {
54-
go gui.ReportFatalError(fatalError, launcherFlags)
55-
}
56-
57-
// On MacOS, only the first thread created by the OS is allowed to be the main GUI thread.
58-
// Also, on Windows, OLE code needs to run on the main thread, which we rely on when creating shortcuts.
59-
runtime.LockOSThread()
51+
go runLauncher(ctx, envErr, launcherFlags)
52+
runGUI(ctx, cancelFunc, launcherFlags, envErr == nil)
6053

61-
err := gui.Main(ctx, cancelFunc, resources.LauncherConfig.BrandingName, !launcherFlags.Uninstall && fatalError == nil)
62-
if err != nil {
63-
log.Fatalf("gui.Main() failed: %v\n", err)
64-
}
65-
66-
log.Info("End of main().")
6754
log.Exit(0)
6855
}
6956

@@ -80,6 +67,28 @@ func initializeEnvironment() (*flags.LauncherFlags, error) {
8067
return launcherFlags, misc.NewNestedErrorFromFirstCause(argumentError, flagError, pathError, placesError)
8168
}
8269

70+
func runLauncher(ctx context.Context, fatalError error, launcherFlags *flags.LauncherFlags) {
71+
gui.WaitUntilReady()
72+
defer gui.Quit()
73+
if fatalError != nil {
74+
gui.PanicInformatively(fatalError, launcherFlags)
75+
}
76+
defer gui.HandlePanic(launcherFlags)
77+
launcher.LauncherMain(ctx, launcherFlags)
78+
}
79+
80+
func runGUI(ctx context.Context, cancelFunc context.CancelFunc, launcherFlags *flags.LauncherFlags, showMainWindow bool) {
81+
// On MacOS, only the first thread created by the OS is allowed to be the main GUI thread.
82+
// Also, on Windows, OLE code needs to run on the main thread, which we rely on when creating shortcuts.
83+
runtime.LockOSThread()
84+
85+
err := gui.Main(ctx, cancelFunc, resources.LauncherConfig.BrandingName, !launcherFlags.Uninstall && showMainWindow)
86+
if err != nil {
87+
log.Fatalf("gui.Main() failed: %v\n", err)
88+
}
89+
log.Info("runGUI() terminated.")
90+
}
91+
8392
func parseEnvironment() (launcherFlags *flags.LauncherFlags, argumentError, flagError, pathError, evalError, placesError error) {
8493
launcherFlags = &flags.LauncherFlags{}
8594
if len(os.Args) < 1 {

0 commit comments

Comments
 (0)