forked from eremetic-framework/eremetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
111 lines (97 loc) · 2.6 KB
/
database.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
package eremetic
import (
"encoding/json"
"errors"
"sync"
)
// Masking is the string used for masking environment variables.
const Masking = "*******"
// ApplyMask replaces masked environment variables with a masking string.
func ApplyMask(task *Task) {
for k := range task.MaskedEnvironment {
task.MaskedEnvironment[k] = Masking
}
}
// Encode encodes a task into a JSON byte array.
func Encode(task *Task) ([]byte, error) {
encoded, err := json.Marshal(task)
return []byte(encoded), err
}
// TaskDB defines the functions needed by the database abstraction layer
type TaskDB interface {
Clean() error
Close()
PutTask(task *Task) error
ReadTask(id string) (Task, error)
DeleteTask(id string) error
ReadUnmaskedTask(id string) (Task, error)
ListTasks(filter *TaskFilter) ([]*Task, error)
}
// DefaultTaskDB is a in-memory implementation of TaskDB.
type DefaultTaskDB struct {
mtx sync.RWMutex
tasks map[string]*Task
}
// NewDefaultTaskDB returns a new instance of TaskDB.
func NewDefaultTaskDB() *DefaultTaskDB {
return &DefaultTaskDB{
tasks: make(map[string]*Task),
}
}
// Clean removes all tasks from the database.
func (db *DefaultTaskDB) Clean() error {
db.tasks = make(map[string]*Task)
return nil
}
// Close closes the connection to the database.
func (db *DefaultTaskDB) Close() {
return
}
// PutTask adds a new task to the database.
func (db *DefaultTaskDB) PutTask(task *Task) error {
db.mtx.Lock()
defer db.mtx.Unlock()
db.tasks[task.ID] = task
return nil
}
// ReadTask returns a task with a given id, or an error if not found.
func (db *DefaultTaskDB) ReadTask(id string) (Task, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
if task, ok := db.tasks[id]; ok {
ApplyMask(task)
return *task, nil
}
return Task{}, errors.New("unknown task")
}
// DeleteTask removes the task with a given id, or an error if not found.
func (db *DefaultTaskDB) DeleteTask(id string) error {
db.mtx.RLock()
defer db.mtx.RUnlock()
if _, ok := db.tasks[id]; ok {
delete(db.tasks, id)
return nil
}
return errors.New("unknown task")
}
// ReadUnmaskedTask returns a task with all its environment variables unmasked.
func (db *DefaultTaskDB) ReadUnmaskedTask(id string) (Task, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
if task, ok := db.tasks[id]; ok {
return *task, nil
}
return Task{}, errors.New("unknown task")
}
// ListTasks returns all tasks based on the filter.
func (db *DefaultTaskDB) ListTasks(filter *TaskFilter) ([]*Task, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
res := []*Task{}
for _, t := range db.tasks {
if filter.Match(t) {
res = append(res, t)
}
}
return res, nil
}