gopkg.in/juju/charm.v6-unstable@v6.0.0-20171026192109-50d0c219b496/resource/origin.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resource
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  )
     9  
    10  // These are the valid resource origins.
    11  const (
    12  	originUnknown Origin = iota
    13  	OriginUpload
    14  	OriginStore
    15  )
    16  
    17  var origins = map[Origin]string{
    18  	OriginUpload: "upload",
    19  	OriginStore:  "store",
    20  }
    21  
    22  // Origin identifies where a charm's resource comes from.
    23  type Origin int
    24  
    25  // ParseOrigin converts the provided string into an Origin.
    26  // If it is not a known origin then an error is returned.
    27  func ParseOrigin(value string) (Origin, error) {
    28  	for o, str := range origins {
    29  		if value == str {
    30  			return o, nil
    31  		}
    32  	}
    33  	return originUnknown, errors.Errorf("unknown origin %q", value)
    34  }
    35  
    36  // String returns the printable representation of the origin.
    37  func (o Origin) String() string {
    38  	return origins[o]
    39  }
    40  
    41  // Validate ensures that the origin is correct.
    42  func (o Origin) Validate() error {
    43  	// Ideally, only the (unavoidable) zero value would be invalid.
    44  	// However, typedef'ing int means that the use of int literals
    45  	// could result in invalid Type values other than the zero value.
    46  	if _, ok := origins[o]; !ok {
    47  		return errors.NewNotValid(nil, "unknown origin")
    48  	}
    49  	return nil
    50  }