/
available_versions.go
203 lines (177 loc) · 6.5 KB
/
available_versions.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package task
import (
"bytes"
"context"
"fmt"
"os"
"sort"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/plugin"
"github.com/turbot/steampipe/pkg/steampipeconfig"
"github.com/turbot/steampipe/pkg/utils"
)
type AvailableVersionCache struct {
StructVersion uint32 `json:"struct_version"`
CliCache *CLIVersionCheckResponse `json:"cli_version"`
PluginCache map[string]plugin.VersionCheckReport `json:"plugin_version"`
}
func (av *AvailableVersionCache) asTable(ctx context.Context) (*bytes.Buffer, error) {
notificationLines, err := av.buildNotification(ctx)
if err != nil {
return nil, err
}
notificationTable := utils.Map(notificationLines, func(line string) []string {
return []string{line}
})
if len(notificationLines) == 0 {
return nil, nil
}
// create a buffer writer to pass to the tablewriter
// so that we can capture the output
var buffer bytes.Buffer // c
table := tablewriter.NewWriter(&buffer)
table.SetHeader([]string{}) // no headers please
table.SetAlignment(tablewriter.ALIGN_LEFT) // we align to the left
table.SetAutoWrapText(false) // let's not wrap the text
table.SetBorder(true) // there needs to be a border to provide the dialog feel
table.AppendBulk(notificationTable) // Add Bulk Data
// render the table into the buffer
table.Render()
return &buffer, nil
}
func (av *AvailableVersionCache) buildNotification(ctx context.Context) ([]string, error) {
cliLines, err := av.cliNotificationMessage()
if err != nil {
return nil, err
}
pluginLines := av.pluginNotificationMessage(ctx)
// convert notificationLines into an array of arrays
// since that's what our table renderer expects
return append(cliLines, pluginLines...), nil
}
func (av *AvailableVersionCache) cliNotificationMessage() ([]string, error) {
info := av.CliCache
if info == nil {
return nil, nil
}
if info.NewVersion == "" {
return nil, nil
}
newVersion, err := semver.NewVersion(info.NewVersion)
if err != nil {
return nil, err
}
currentVersion, err := semver.NewVersion(currentVersion)
if err != nil {
fmt.Println(fmt.Errorf("there's something wrong with the Current Version"))
fmt.Println(err)
}
if newVersion.GreaterThan(currentVersion) {
var downloadURLColor = color.New(color.FgYellow)
var notificationLines = []string{
"",
fmt.Sprintf("A new version of Steampipe is available! %s → %s", constants.Bold(currentVersion), constants.Bold(newVersion)),
fmt.Sprintf("You can update by downloading from %s", downloadURLColor.Sprint("https://steampipe.io/downloads")),
"",
}
return notificationLines, nil
}
return nil, nil
}
func ppNotificationLines() []string {
var notificationLines = []string{
"",
fmt.Sprintf("%s Steampipe mods and dashboards are now separately available in Powerpipe (%s), a new open-source project (%s).", constants.Bold("Introducing Powerpipe:"), color.YellowString("https://powerpipe.io"), color.YellowString("https://github.com/turbot/powerpipe")),
"",
fmt.Sprintf("The steampipe mod, check and dashboard commands %s in a future version. Migration guide - %s", constants.Bold("will be removed"), color.YellowString("https://powerpipe.io/blog/migrating-from-steampipe")),
"",
}
return notificationLines
}
func (av *AvailableVersionCache) pluginNotificationMessage(ctx context.Context) []string {
var pluginsToUpdate []plugin.VersionCheckReport
// retrieve the plugin version data from steampipe config
pluginVersions := steampipeconfig.GlobalConfig.PluginVersions
for _, r := range av.PluginCache {
installedVersion := pluginVersions[r.Plugin.Name]
skip, _ := plugin.SkipUpdate(r)
if !skip && installedVersion.ImageDigest != r.CheckResponse.Digest {
pluginsToUpdate = append(pluginsToUpdate, r)
}
}
notificationLines := []string{}
if len(pluginsToUpdate) > 0 {
notificationLines = av.getPluginNotificationLines(pluginsToUpdate)
}
return notificationLines
}
func (av *AvailableVersionCache) getPluginNotificationLines(reports []plugin.VersionCheckReport) []string {
var notificationLines = []string{
"",
"Updated versions of the following plugins are available:",
"",
}
longestNameLength := 0
for _, report := range reports {
thisName := report.ShortName()
if len(thisName) > longestNameLength {
longestNameLength = len(thisName)
}
}
// sort alphabetically
sort.Slice(reports, func(i, j int) bool {
return reports[i].ShortName() < reports[j].ShortName()
})
for _, report := range reports {
thisName := report.ShortName()
line := ""
if len(report.Plugin.Version) == 0 {
format := fmt.Sprintf(" %%-%ds @ %%-10s → %%10s", longestNameLength)
line = fmt.Sprintf(
format,
thisName,
report.CheckResponse.Stream,
constants.Bold(report.CheckResponse.Version),
)
} else {
version := report.CheckResponse.Version
format := fmt.Sprintf(" %%-%ds @ %%-10s %%10s → %%-10s", longestNameLength)
// an arm64 binary of the plugin might exist for the same version
if report.Plugin.Version == report.CheckResponse.Version {
version = fmt.Sprintf("%s (arm64)", version)
}
line = fmt.Sprintf(
format,
thisName,
report.CheckResponse.Stream,
constants.Bold(report.Plugin.Version),
constants.Bold(version),
)
}
notificationLines = append(notificationLines, line)
}
notificationLines = append(notificationLines, "")
notificationLines = append(notificationLines, fmt.Sprintf("You can update by running %s", constants.Bold("steampipe plugin update --all")))
notificationLines = append(notificationLines, "")
return notificationLines
}
func ppNoptificationAsTable(colWidth int) (*tablewriter.Table, error) {
notificationLines := ppNotificationLines()
notificationTable := utils.Map(notificationLines, func(line string) []string {
return []string{line}
})
if len(notificationLines) == 0 {
return nil, nil
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{}) // no headers please
table.SetAlignment(tablewriter.ALIGN_LEFT) // we align to the left
table.SetAutoWrapText(true) // let's wrap the text
table.SetBorder(true) // there needs to be a border to provide the dialog feel
table.SetColWidth(colWidth - 4) // set the column width which matches the steampipe notification table width
table.AppendBulk(notificationTable) // Add Bulk Data
return table, nil
}