forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charm.go
101 lines (88 loc) · 2.8 KB
/
charm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"fmt"
"os"
"strings"
"github.com/juju/loggo"
)
var logger = loggo.GetLogger("juju.charm")
// The Charm interface is implemented by any type that
// may be handled as a charm.
type Charm interface {
Meta() *Meta
Config() *Config
Metrics() *Metrics
Actions() *Actions
Revision() int
}
// ReadCharm reads a Charm from path, which can point to either a charm archive or a
// charm directory.
func ReadCharm(path string) (charm Charm, err error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if info.IsDir() {
charm, err = ReadCharmDir(path)
} else {
charm, err = ReadCharmArchive(path)
}
if err != nil {
return nil, err
}
return charm, nil
}
// SeriesForCharm takes a requested series and a list of series supported by a
// charm and returns the series which is relevant.
// If the requested series is empty, then the first supported series is used,
// otherwise the requested series is validated against the supported series.
func SeriesForCharm(requestedSeries string, supportedSeries []string) (string, error) {
// Old charm with no supported series.
if len(supportedSeries) == 0 {
if requestedSeries == "" {
return "", missingSeriesError
}
return requestedSeries, nil
}
// Use the charm default.
if requestedSeries == "" {
return supportedSeries[0], nil
}
for _, s := range supportedSeries {
if s == requestedSeries {
return requestedSeries, nil
}
}
return "", &unsupportedSeriesError{requestedSeries, supportedSeries}
}
// missingSeriesError is used to denote that SeriesForCharm could not determine
// a series because a legacy charm did not declare any.
var missingSeriesError = fmt.Errorf("series not specified and charm does not define any")
// IsMissingSeriesError returns true if err is an missingSeriesError.
func IsMissingSeriesError(err error) bool {
return err == missingSeriesError
}
// UnsupportedSeriesError represents an error indicating that the requested series
// is not supported by the charm.
type unsupportedSeriesError struct {
requestedSeries string
supportedSeries []string
}
func (e *unsupportedSeriesError) Error() string {
return fmt.Sprintf(
"series %q not supported by charm, supported series are: %s",
e.requestedSeries, strings.Join(e.supportedSeries, ","),
)
}
// NewUnsupportedSeriesError returns an error indicating that the requested series
// is not supported by a charm.
func NewUnsupportedSeriesError(requestedSeries string, supportedSeries []string) error {
return &unsupportedSeriesError{requestedSeries, supportedSeries}
}
// IsUnsupportedSeriesError returns true if err is an UnsupportedSeriesError.
func IsUnsupportedSeriesError(err error) bool {
_, ok := err.(*unsupportedSeriesError)
return ok
}