/
display.go
100 lines (86 loc) · 2.75 KB
/
display.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package task
import (
"encoding/json"
"fmt"
"github.com/turbot/go-kit/helpers"
"log/slog"
"os"
"github.com/spf13/cobra"
"github.com/turbot/pipe-fittings/error_helpers"
"github.com/turbot/pipe-fittings/filepaths"
"github.com/turbot/pipe-fittings/utils"
)
const (
AvailableVersionsCacheStructVersion = 20230117
)
func (r *Runner) saveAvailableVersions(cli *CLIVersionCheckResponse) error {
utils.LogTime("Runner.saveAvailableVersions start")
defer utils.LogTime("Runner.saveAvailableVersions end")
if cli == nil {
// nothing to save
return nil
}
notifs := &AvailableVersionCache{
StructVersion: AvailableVersionsCacheStructVersion,
CliCache: cli,
}
// create the file - if it exists, it will be truncated by os.Create
f, err := os.Create(filepaths.AvailableVersionsFilePath())
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
return encoder.Encode(notifs)
}
func (r *Runner) loadAvailableVersions() (*AvailableVersionCache, error) {
utils.LogTime("Runner.getNotifications start")
defer utils.LogTime("Runner.getNotifications end")
// TODO graza why not put this in the update check file (later)
f, err := os.Open(filepaths.AvailableVersionsFilePath())
if err != nil {
return nil, err
}
notifications := &AvailableVersionCache{}
decoder := json.NewDecoder(f)
if err := decoder.Decode(notifications); err != nil {
return nil, err
}
if err := error_helpers.CombineErrors(f.Close(), os.Remove(filepaths.AvailableVersionsFilePath())); err != nil {
// if Go couldn't close the file handle, no matter - this was just good practise
// if Go couldn't remove the notification file, it'll get truncated next time we try to write to it
// worst case is that the notification gets shown more than once
slog.Debug("could not close/delete notification file", err)
}
return notifications, nil
}
// displayNotifications checks if there are any pending notifications to display
// and if so, displays them
// does nothing if the given command is a command where notifications are not displayed
func (r *Runner) displayNotifications(cmd *cobra.Command, cmdArgs []string) error {
utils.LogTime("Runner.displayNotifications start")
defer utils.LogTime("Runner.displayNotifications end")
if !r.options.showNotifications(cmd, cmdArgs) {
// do not do anything - just return
return nil
}
availableVersions, err := r.loadAvailableVersions()
if err != nil {
return err
}
if helpers.IsNil(availableVersions) {
return nil
}
table, err := availableVersions.asTable()
if err != nil {
return err
}
// table can be nil if there are no notifications to display
if table == nil {
return nil
}
fmt.Println() //nolint:forbidigo // acceptable
table.Render()
fmt.Println() //nolint:forbidigo // acceptable
return nil
}