1package db
2
3import "fmt"
4
5type ContainerMetadata struct {
6	Type ContainerType
7
8	StepName string
9	Attempt  string
10
11	WorkingDirectory string
12	User             string
13
14	PipelineID int
15	JobID      int
16	BuildID    int
17
18	PipelineName string
19	JobName      string
20	BuildName    string
21}
22
23type ContainerType string
24
25const (
26	ContainerTypeCheck ContainerType = "check"
27	ContainerTypeGet   ContainerType = "get"
28	ContainerTypePut   ContainerType = "put"
29	ContainerTypeTask  ContainerType = "task"
30)
31
32func ContainerTypeFromString(containerType string) (ContainerType, error) {
33	switch containerType {
34	case "check":
35		return ContainerTypeCheck, nil
36	case "get":
37		return ContainerTypeGet, nil
38	case "put":
39		return ContainerTypePut, nil
40	case "task":
41		return ContainerTypeTask, nil
42	default:
43		return "", fmt.Errorf("Unrecognized containerType: %s", containerType)
44	}
45}
46
47func (metadata ContainerMetadata) SQLMap() map[string]interface{} {
48	m := map[string]interface{}{}
49
50	if metadata.Type != "" {
51		m["meta_type"] = string(metadata.Type)
52	}
53
54	if metadata.StepName != "" {
55		m["meta_step_name"] = metadata.StepName
56	}
57
58	if metadata.Attempt != "" {
59		m["meta_attempt"] = metadata.Attempt
60	}
61
62	if metadata.WorkingDirectory != "" {
63		m["meta_working_directory"] = metadata.WorkingDirectory
64	}
65
66	if metadata.User != "" {
67		m["meta_process_user"] = metadata.User
68	}
69
70	if metadata.PipelineID != 0 {
71		m["meta_pipeline_id"] = metadata.PipelineID
72	}
73
74	if metadata.JobID != 0 {
75		m["meta_job_id"] = metadata.JobID
76	}
77
78	if metadata.BuildID != 0 {
79		m["meta_build_id"] = metadata.BuildID
80	}
81
82	if metadata.PipelineName != "" {
83		m["meta_pipeline_name"] = metadata.PipelineName
84	}
85
86	if metadata.JobName != "" {
87		m["meta_job_name"] = metadata.JobName
88	}
89
90	if metadata.BuildName != "" {
91		m["meta_build_name"] = metadata.BuildName
92	}
93
94	return m
95}
96
97var containerMetadataColumns = []string{
98	"meta_type",
99	"meta_step_name",
100	"meta_attempt",
101	"meta_working_directory",
102	"meta_process_user",
103	"meta_pipeline_id",
104	"meta_job_id",
105	"meta_build_id",
106	"meta_pipeline_name",
107	"meta_job_name",
108	"meta_build_name",
109}
110
111func (metadata *ContainerMetadata) ScanTargets() []interface{} {
112	return []interface{}{
113		&metadata.Type,
114		&metadata.StepName,
115		&metadata.Attempt,
116		&metadata.WorkingDirectory,
117		&metadata.User,
118		&metadata.PipelineID,
119		&metadata.JobID,
120		&metadata.BuildID,
121		&metadata.PipelineName,
122		&metadata.JobName,
123		&metadata.BuildName,
124	}
125}
126