/
asset.go
142 lines (112 loc) 路 4.33 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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 pulumi
import (
"reflect"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"golang.org/x/net/context"
)
// AssetOrArchive represents either an Asset or an Archive.
type AssetOrArchive interface {
isAssetOrArchive()
}
// 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 {
AssetOrArchive
AssetInput
ToAssetOrArchiveOutput() AssetOrArchiveOutput
ToAssetOrArchiveOutputWithContext(ctx context.Context) AssetOrArchiveOutput
// 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
isAsset()
}
type asset struct {
path string
text string
uri string
invalid bool
}
// 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 }
func (a *asset) isAsset() {}
func (a *asset) isAssetOrArchive() {}
// Archive represents a collection of Assets.
type Archive interface {
AssetOrArchive
ArchiveInput
ToAssetOrArchiveOutput() AssetOrArchiveOutput
ToAssetOrArchiveOutputWithContext(ctx context.Context) AssetOrArchiveOutput
// 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
isArchive()
}
type archive struct {
assets map[string]interface{}
path string
uri string
invalid bool
}
// 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 }
func (a *archive) isArchive() {}
func (a *archive) isAssetOrArchive() {}