/
findformatter.go
52 lines (43 loc) · 1.37 KB
/
findformatter.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package crossmodel
import (
"fmt"
"io"
"sort"
"strings"
"github.com/juju/errors"
"github.com/juju/juju/cmd/output"
"github.com/juju/juju/core/crossmodel"
)
// formatFindTabular returns a tabular summary of remote applications or
// errors out if parameter is not of expected type.
func formatFindTabular(writer io.Writer, value interface{}) error {
endpoints, ok := value.(map[string]ApplicationOfferResult)
if !ok {
return errors.Errorf("expected value of type %T, got %T", endpoints, value)
}
return formatFoundEndpointsTabular(writer, endpoints)
}
// formatFoundEndpointsTabular returns a tabular summary of offered applications' endpoints.
func formatFoundEndpointsTabular(writer io.Writer, all map[string]ApplicationOfferResult) error {
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.Println("Store", "URL", "Access", "Interfaces")
for urlStr, one := range all {
url, err := crossmodel.ParseOfferURL(urlStr)
if err != nil {
return err
}
store := url.Source
url.Source = ""
interfaces := []string{}
for name, ep := range one.Endpoints {
interfaces = append(interfaces, fmt.Sprintf("%s:%s", ep.Interface, name))
}
sort.Strings(interfaces)
w.Println(store, url.String(), one.Access, strings.Join(interfaces, ", "))
}
tw.Flush()
return nil
}