This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
resource.go
79 lines (61 loc) · 1.54 KB
/
resource.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
package system
import (
"fmt"
"strings"
xfs "github.com/saitho/golang-extended-fs/v2"
)
type Type string
const (
TypeFile Type = "file"
TypeContainer Type = "container"
)
type Operation string
const (
OperationCreate Operation = "create"
)
type ApplyResourceFuncType func() error
type RollbackResourceFuncType func() error
type ResourceGroup struct {
Name string
Resources []Resource
ApplyResourceFunc ApplyResourceFuncType
RollbackResourceFunc RollbackResourceFuncType
}
type Resource struct {
Type Type
Operation Operation
// name of the resource (e.g. file name, container name)
Name string
// contents of resource, if any
Content string
// name of the associated service
ServiceName string
// for container resources
Ports []string
ImageName string
}
func (r Resource) GetOperationLabel(invertOperation bool) string {
operation := "UNKNOWN"
if r.Operation == OperationCreate {
operation = "CREATE"
if r.Type == TypeFile {
// check if file exists
hasFile, _ := xfs.HasFile("ssh://" + r.Name)
if hasFile {
operation = "UPDATE"
}
}
}
if invertOperation {
if operation == "CREATE" {
operation = "DELETE"
}
}
return operation
}
func (r Resource) ToString(invertOperation bool) string {
if r.Type == TypeContainer {
return fmt.Sprintf("[%s] Container %s (service=%s, image=%s, ports=%s)", r.GetOperationLabel(invertOperation), r.Name, r.ServiceName, r.ImageName, strings.Join(r.Ports, ", "))
}
return fmt.Sprintf("[%s] %s %s", r.GetOperationLabel(invertOperation), r.Type, r.Name)
}