~sinzui/ubuntu/vivid/juju-core/vivid-1.24.6

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/flags.go

  • Committer: Curtis Hovey
  • Date: 2015-09-30 14:14:54 UTC
  • mfrom: (1.1.34)
  • Revision ID: curtis@hovey.name-20150930141454-o3ldf23dzyjio6c0
Backport of 1.24.6 from wily. (LP: #1500916)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package main
 
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
}