github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/crossmodel/findformatter.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package crossmodel
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"sort"
    10  	"strings"
    11  
    12  	"github.com/juju/errors"
    13  
    14  	"github.com/juju/juju/cmd/output"
    15  	"github.com/juju/juju/core/crossmodel"
    16  )
    17  
    18  // formatFindTabular returns a tabular summary of remote applications or
    19  // errors out if parameter is not of expected type.
    20  func formatFindTabular(writer io.Writer, value interface{}) error {
    21  	endpoints, ok := value.(map[string]ApplicationOfferResult)
    22  	if !ok {
    23  		return errors.Errorf("expected value of type %T, got %T", endpoints, value)
    24  	}
    25  	return formatFoundEndpointsTabular(writer, endpoints)
    26  }
    27  
    28  // formatFoundEndpointsTabular returns a tabular summary of offered applications' endpoints.
    29  func formatFoundEndpointsTabular(writer io.Writer, all map[string]ApplicationOfferResult) error {
    30  	tw := output.TabWriter(writer)
    31  	w := output.Wrapper{tw}
    32  	w.Println("Store", "URL", "Access", "Interfaces")
    33  
    34  	for urlStr, one := range all {
    35  		url, err := crossmodel.ParseOfferURL(urlStr)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		store := url.Source
    40  		url.Source = ""
    41  
    42  		interfaces := []string{}
    43  		for name, ep := range one.Endpoints {
    44  			interfaces = append(interfaces, fmt.Sprintf("%s:%s", ep.Interface, name))
    45  		}
    46  		sort.Strings(interfaces)
    47  		w.Println(store, url.String(), one.Access, strings.Join(interfaces, ", "))
    48  	}
    49  	tw.Flush()
    50  
    51  	return nil
    52  }