-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
asset.go
110 lines (91 loc) 路 3.67 KB
/
asset.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
102
103
104
105
106
107
108
109
110
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package asset
import (
"reflect"
"github.com/pulumi/pulumi/pkg/util/contract"
)
// Asset represents a file that is managed in conjunction with Pulumi resources. An Asset may be backed by a number
// of sources, including local filesystem paths, in-memory blobs of text, or remote files referenced by a URL.
type Asset interface {
// Path returns the filesystem path, for file-based assets.
Path() string
// Text returns an in-memory blob of text, for string-based assets.
Text() string
// URI returns a URI, for remote network-based assets.
URI() string
}
type asset struct {
path string
text string
uri string
}
// NewFileAsset creates an asset backed by a file and specified by that file's path.
func NewFileAsset(path string) Asset {
return &asset{path: path}
}
// NewStringAsset creates an asset backed by a piece of in-memory text.
func NewStringAsset(text string) Asset {
return &asset{text: text}
}
// NewRemoteAsset creates an asset backed by a remote file and specified by that file's URL.
func NewRemoteAsset(uri string) Asset {
return &asset{uri: uri}
}
// Path returns the asset's file path, if this is a file asset, or an empty string otherwise.
func (a *asset) Path() string { return a.path }
// Text returns the asset's textual string, if this is a string asset, or an empty string otherwise.
func (a *asset) Text() string { return a.text }
// URI returns the asset's URL, if this is a remote asset, or an empty string otherwise.
func (a *asset) URI() string { return a.uri }
// Archive represents a collection of Assets.
type Archive interface {
// Assets returns a map of named assets or archives, for collections.
Assets() map[string]interface{}
// Path returns the filesystem path, for file-based archives.
Path() string
// URI returns a URI, for remote network-based archives.
URI() string
}
type archive struct {
assets map[string]interface{}
path string
uri string
}
// NewAssetArchive creates a new archive from an in-memory collection of named assets or other archives.
func NewAssetArchive(assets map[string]interface{}) Archive {
for k, a := range assets {
if _, ok := a.(Asset); !ok {
if _, ok2 := a.(Archive); !ok2 {
contract.Failf(
"expected asset map to contain Assets and/or Archives; %s is %v", k, reflect.TypeOf(a))
}
}
}
return &archive{assets: assets}
}
// NewFileArchive creates an archive backed by a file and specified by that file's path.
func NewFileArchive(path string) Archive {
return &archive{path: path}
}
// NewRemoteArchive creates an archive backed by a remote file and specified by that file's URL.
func NewRemoteArchive(uri string) Archive {
return &archive{uri: uri}
}
// Assets returns the archive's asset map, if this is a collection of archives/assets, or nil otherwise.
func (a *archive) Assets() map[string]interface{} { return a.assets }
// Path returns the archive's file path, if this is a file archive, or an empty string otherwise.
func (a *archive) Path() string { return a.path }
// URI returns the archive's URL, if this is a remote archive, or an empty string otherwise.
func (a *archive) URI() string { return a.uri }