-
Notifications
You must be signed in to change notification settings - Fork 21
/
package_search.go
68 lines (63 loc) · 1.71 KB
/
package_search.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
package pkg
import (
"encoding/json"
"errors"
"strconv"
"unicode/utf8"
"github.com/dcos/dcos-cli/api"
"github.com/dcos/dcos-cli/pkg/cli"
"github.com/dcos/dcos-core-cli/pkg/cosmos"
"github.com/dcos/dcos-core-cli/pkg/pluginutil"
"github.com/spf13/cobra"
)
func newCmdPackageSearch(ctx api.Context) *cobra.Command {
var jsonOutput bool
cmd := &cobra.Command{
Use: "search <query>",
Short: "Search the package repository",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
query := ""
if len(args) == 1 {
query = args[0]
}
c, err := cosmos.NewClient(ctx, pluginutil.HTTPClient(""))
if err != nil {
return err
}
return search(ctx, query, jsonOutput, c)
},
}
cmd.Flags().BoolVar(&jsonOutput, "json", false, "Print in json format")
return cmd
}
func search(ctx api.Context, query string, jsonOutput bool, c cosmos.Client) error {
searchResult, err := c.PackageSearch(query)
if err != nil {
return err
}
if jsonOutput {
enc := json.NewEncoder(ctx.Out())
enc.SetIndent("", " ")
return enc.Encode(searchResult)
}
if query != "" && len(searchResult.Packages) == 0 {
return errors.New("no packages found")
}
table := cli.NewTable(ctx.Out(), []string{"NAME", "VERSION", "CERTIFIED", "FRAMEWORK", "DESCRIPTION"})
for _, cosmosPackage := range searchResult.Packages {
description := cosmosPackage.Description
if utf8.RuneCountInString(cosmosPackage.Description) >= 80 {
description = description[0:76] + "..."
}
table.Append([]string{
cosmosPackage.Name,
cosmosPackage.CurrentVersion,
strconv.FormatBool(cosmosPackage.Selected),
strconv.FormatBool(cosmosPackage.Framework),
description,
})
}
table.Render()
return nil
}