-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathcmd_find.go
129 lines (104 loc) · 2.86 KB
/
cmd_find.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"sort"
"text/tabwriter"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/i18n"
"github.com/jessevdk/go-flags"
)
var shortFindHelp = i18n.G("Finds packages to install")
var longFindHelp = i18n.G(`
The find command queries the store for available packages.
`)
func getPrice(prices map[string]float64, currency string) string {
// If there are no prices, then the snap is free
if len(prices) == 0 {
return "-"
}
// Look up the price by currency code
val, ok := prices[currency]
// Fall back to dollars
if !ok {
currency = "USD"
val, ok = prices["USD"]
}
// If there aren't even dollars, grab the first currency,
// ordered alphabetically by currency code
if !ok {
currency = "ZZZ"
for c, v := range prices {
if c < currency {
currency, val = c, v
}
}
}
return fmt.Sprintf("%.2f%s", val, currency)
}
type cmdFind struct {
Positional struct {
Query string `positional-arg-name:"<query>"`
} `positional-args:"yes"`
}
func init() {
addCommand("find", shortFindHelp, longFindHelp, func() flags.Commander {
return &cmdFind{}
})
}
func hasPrices(snaps []*client.Snap) bool {
for _, snap := range snaps {
if len(snap.Prices) > 0 {
return true
}
}
return false
}
func printWithPrices(w *tabwriter.Writer, snaps []*client.Snap, suggestedCurrency string) {
fmt.Fprintln(w, i18n.G("Name\tVersion\tPrice\tSummary"))
for _, snap := range snaps {
price := getPrice(snap.Prices, suggestedCurrency)
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", snap.Name, snap.Version, price, snap.Summary)
}
}
func printNoPrices(w *tabwriter.Writer, snaps []*client.Snap) {
fmt.Fprintln(w, i18n.G("Name\tVersion\tSummary"))
for _, snap := range snaps {
fmt.Fprintf(w, "%s\t%s\t%s\n", snap.Name, snap.Version, snap.Summary)
}
}
func (x *cmdFind) Execute([]string) error {
cli := Client()
snaps, resInfo, err := cli.FindSnaps(x.Positional.Query)
if err != nil {
return err
}
if len(snaps) == 0 {
return fmt.Errorf("no snaps found for %q", x.Positional.Query)
}
sort.Sort(snapsByName(snaps))
w := tabWriter()
defer w.Flush()
if hasPrices(snaps) {
printWithPrices(w, snaps, resInfo.SuggestedCurrency)
} else {
printNoPrices(w, snaps)
}
return nil
}