github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/status/utils.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  	"fmt"
     8  	"reflect"
     9  
    10  	"github.com/juju/juju/cmd/juju/common"
    11  )
    12  
    13  // stringKeysFromMap takes a map with keys which are strings and returns
    14  // only the keys.
    15  func stringKeysFromMap(m interface{}) (keys []string) {
    16  	for _, k := range reflect.ValueOf(m).MapKeys() {
    17  		keys = append(keys, k.String())
    18  	}
    19  	return
    20  }
    21  
    22  // recurseUnits calls the given recurseMap function on the given unit
    23  // and its subordinates (recursively defined on the given unit).
    24  func recurseUnits(u unitStatus, il int, recurseMap func(string, unitStatus, int)) {
    25  	if len(u.Subordinates) == 0 {
    26  		return
    27  	}
    28  	for _, uName := range common.SortStringsNaturally(stringKeysFromMap(u.Subordinates)) {
    29  		unit := u.Subordinates[uName]
    30  		recurseMap(uName, unit, il)
    31  		recurseUnits(unit, il+1, recurseMap)
    32  	}
    33  }
    34  
    35  // indent prepends a format string with the given number of spaces.
    36  func indent(prepend string, level int, append string) string {
    37  	return fmt.Sprintf("%s%*s%s", prepend, level, "", append)
    38  }