github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/status/output_oneline.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package status
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/juju/errors"
    12  
    13  	"github.com/juju/juju/cmd/juju/common"
    14  )
    15  
    16  // FormatOneline returns a brief list of units and their subordinates.
    17  // Subordinates will be indented 2 spaces and listed under their
    18  // superiors. This format works with version 2 of the CLI.
    19  func FormatOneline(value interface{}) ([]byte, error) {
    20  	return formatOneline(value, func(out *bytes.Buffer, format, uName string, u unitStatus, level int) {
    21  		status := fmt.Sprintf(
    22  			"agent:%s, workload:%s",
    23  			u.JujuStatusInfo.Current,
    24  			u.WorkloadStatusInfo.Current,
    25  		)
    26  		fmt.Fprintf(out, format,
    27  			uName,
    28  			u.PublicAddress,
    29  			status,
    30  		)
    31  	})
    32  }
    33  
    34  type onelinePrintf func(out *bytes.Buffer, format, uName string, u unitStatus, level int)
    35  
    36  func formatOneline(value interface{}, printf onelinePrintf) ([]byte, error) {
    37  	fs, valueConverted := value.(formattedStatus)
    38  	if !valueConverted {
    39  		return nil, errors.Errorf("expected value of type %T, got %T", fs, value)
    40  	}
    41  	var out bytes.Buffer
    42  
    43  	pprint := func(uName string, u unitStatus, level int) {
    44  		var fmtPorts string
    45  		if len(u.OpenedPorts) > 0 {
    46  			fmtPorts = fmt.Sprintf(" %s", strings.Join(u.OpenedPorts, ", "))
    47  		}
    48  		format := indent("\n", level*2, "- %s: %s (%v)"+fmtPorts)
    49  		printf(&out, format, uName, u, level)
    50  	}
    51  
    52  	for _, svcName := range common.SortStringsNaturally(stringKeysFromMap(fs.Services)) {
    53  		svc := fs.Services[svcName]
    54  		for _, uName := range common.SortStringsNaturally(stringKeysFromMap(svc.Units)) {
    55  			unit := svc.Units[uName]
    56  			pprint(uName, unit, 0)
    57  			recurseUnits(unit, 1, pprint)
    58  		}
    59  	}
    60  
    61  	return out.Bytes(), nil
    62  }