Repository navigation
Expand file tree
/
Copy pathsource.go
More file actions
115 lines (105 loc) · 2.47 KB
/
Copy pathsource.go
File metadata and controls
115 lines (105 loc) · 2.47 KB
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
package lxdops
import (
"errors"
"fmt"
"strings"
)
type ContainerSource struct {
Project string
Container string
Snapshot string
}
type DeviceSource struct {
Instance *Instance
Snapshot string
Clone bool
}
func (t *DeviceSource) IsDefined() bool {
return t.Instance != nil
}
func (t *DeviceSource) String() string {
return fmt.Sprintf("instance=%s snapshot=%s clone=%v", t.Instance.Name, t.Snapshot, t.Clone)
}
func (t *ContainerSource) parse(s string) {
i := strings.Index(s, "/")
pc := s
if i >= 0 {
t.Snapshot = s[i+1:]
pc = s[0:i]
}
i = strings.Index(pc, "_")
if i >= 0 {
t.Project = s[0:i]
t.Container = s[i+1:]
} else {
t.Container = pc
}
}
func (t *ContainerSource) String() string {
//return t.Project + "_" + t.Container + "/" + t.Snapshot
return fmt.Sprintf("project=%s container=%s snapshot=%s", t.Project, t.Container, t.Snapshot)
}
func (t *ContainerSource) IsDefined() bool {
return t.Container != ""
}
func (t *Instance) newDeviceSource() (*DeviceSource, error) {
config := t.Config
source := &DeviceSource{}
var name string
if config.DeviceOrigin != "" {
// check device-origin first
s, err := config.DeviceOrigin.Substitute(t.Properties)
if err != nil {
return nil, err
}
parts := strings.Split(s, "@")
if len(parts) != 2 || len(parts[1]) == 0 {
return nil, errors.New("missing device origin snapshot: " + s)
}
name = parts[0]
source.Snapshot = parts[1]
source.Clone = true
} else if config.DeviceTemplate != "" {
// device-template is used only if device-origin is missing
var err error
name, err = config.DeviceTemplate.Substitute(t.Properties)
if err != nil {
return nil, err
}
} else {
return source, nil
}
if name == "" && config.SourceConfig == "" {
return nil, errors.New("missing devices source name")
}
sourceConfig, err := t.GetSourceConfig()
if err != nil {
return nil, err
}
source.Instance, err = newInstance(t.GlobalProperties, sourceConfig, name, false)
if err != nil {
return nil, err
}
return source, nil
}
func (t *Instance) newContainerSource() (*ContainerSource, error) {
s, err := t.Config.Origin.Substitute(t.Properties)
if err != nil {
return nil, err
}
source := &ContainerSource{}
if s != "" {
source.parse(s)
if source.Project == "" {
sourceConfig, err := t.GetSourceConfig()
if err != nil {
return nil, err
}
source.Project = sourceConfig.Project
}
if source.Project == "" {
source.Project = t.Config.Project
}
}
return source, nil
}