github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/flags.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  
    12  	"github.com/juju/juju/storage"
    13  )
    14  
    15  type storageFlag struct {
    16  	stores *map[string]storage.Constraints
    17  }
    18  
    19  // Set implements gnuflag.Value.Set.
    20  func (f storageFlag) Set(s string) error {
    21  	fields := strings.SplitN(s, "=", 2)
    22  	if len(fields) < 2 {
    23  		return errors.New("expected <store>=<constraints>")
    24  	}
    25  	cons, err := storage.ParseConstraints(fields[1])
    26  	if err != nil {
    27  		return errors.Annotate(err, "cannot parse disk constraints")
    28  	}
    29  	if *f.stores == nil {
    30  		*f.stores = make(map[string]storage.Constraints)
    31  	}
    32  	(*f.stores)[fields[0]] = cons
    33  	return nil
    34  }
    35  
    36  // Set implements gnuflag.Value.String.
    37  func (f storageFlag) String() string {
    38  	strs := make([]string, 0, len(*f.stores))
    39  	for store, cons := range *f.stores {
    40  		strs = append(strs, fmt.Sprintf("%s=%v", store, cons))
    41  	}
    42  	return strings.Join(strs, " ")
    43  }