-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.go
78 lines (66 loc) · 2.13 KB
/
misc.go
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
// Copyright 2022 Outreach Corporation. All Rights Reserved.
// Description: Provides miscellaneous helpers for the updater
package updater
import (
"fmt"
"path"
"runtime/debug"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/charmbracelet/glamour"
"github.com/manifoldco/promptui"
"github.com/sirupsen/logrus"
)
// getRepoFromBuild reads the repository from the embedded go module information
func getRepoFromBuild() (string, error) {
info, ok := debug.ReadBuildInfo()
if !ok {
return "", fmt.Errorf("failed to read build info, was this built with go module support")
}
// split on / so we can try to ignore a major version at the end
spl := strings.Split(info.Main.Path, "/")
if len(spl) < 3 {
return "", fmt.Errorf("failed to parse repository from build info (len less than 3)")
}
// github.com getoutreach devenv -> github.com/getoutreach/devenv
return path.Join(spl[0], spl[1], spl[2]), nil
}
// handleMajorVersion prompts the user when a new major version is available, returns
// true if we should continue, or false if we shouldn't
func handleMajorVersion(log logrus.FieldLogger, curV, newV, relNotes string) bool {
// we skip errors because the above logic already parsed these version strings
cver, err := semver.NewVersion(curV)
if err != nil {
return true
}
nver, err := semver.NewVersion(newV)
if err != nil {
return true
}
// if the current major is less than the new release
// major then just return
if !(cver.Major() < nver.Major()) {
return true
}
out := relNotes
r, err := glamour.NewTermRenderer(glamour.WithAutoStyle())
if err == nil {
out, err = r.Render(out)
if err != nil {
log.WithError(err).Warn("Failed to render release notes, using raw release notes")
}
} else if err != nil {
log.WithError(err).Warn("Failed to create markdown render, using raw release notes")
}
fmt.Println(out)
log.Infof("Detected major version upgrade (%d -> %d). Would you like to upgrade?", cver.Major, nver.Major)
prompt := promptui.Select{
Label: "Select",
Items: []string{"Yes", "No"},
}
_, resp, err := prompt.Run()
if err != nil {
return false
}
return strings.EqualFold(resp, "yes")
}