Skip to content

Commit 8215258

Browse files
author
Christos Kotsis
committed
[storage] include delete doc method
In this commit we included a delete doc method that removes a doc by a given index.
1 parent ce19b2b commit 8215258

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

db/storage.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ func (s *Storage) SetName(n string, i int) error {
108108
return nil
109109
}
110110

111+
// DeleteDoc will the document with the given index
112+
func (s *Storage) DeleteDoc(i int) error {
113+
if i > len(s.Data)-1 {
114+
return wrapErr(fmt.Errorf(libOutOfIndex), getFn())
115+
}
116+
117+
s.Data = append(s.Data[:i], s.Data[i+1:]...)
118+
119+
if s.AD == i {
120+
s.AD = 0
121+
}
122+
123+
return nil
124+
}
125+
111126
// Switch will change Active Document (AD) to the given index
112127
func (s *Storage) Switch(i int) error {
113128
if i > len(s.Data)-1 {
@@ -145,7 +160,7 @@ func (s *Storage) SwitchDoc(n string) error {
145160
}
146161

147162
// ImportDocs for importing documents
148-
func (s *Storage) ImportDocs(path string) error {
163+
func (s *Storage) ImportDocs(path string, o ...bool) error {
149164
impf, err := ioutil.ReadFile(path)
150165
if err != nil {
151166
return wrapErr(err, getFn())
@@ -155,6 +170,12 @@ func (s *Storage) ImportDocs(path string) error {
155170
var counter int
156171
var data interface{}
157172

173+
if len(o) > 0 {
174+
if o[0] {
175+
s.Data = nil
176+
}
177+
}
178+
158179
data = nil
159180
dec := yaml.NewDecoder(bytes.NewReader(impf))
160181
for {

docs/examples/kubernetes-labels-update.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ func main() {
1919
if err != nil {
2020
logger.Fatal(err)
2121
}
22-
err = state.ImportDocs("manifests/deployment.yaml")
22+
23+
// passing true is optional. When we pass it, we instruct the import method
24+
// overwrite the content of the db (local/db.yaml). If we do not pass true,
25+
// the local/db.yaml file will also have one additional document which is
26+
// an empty {} doc that is created during init.
27+
err = state.ImportDocs("manifests/deployment.yaml", true)
2328
if err != nil {
2429
logger.Fatal(err)
2530
}

0 commit comments

Comments
 (0)