-
Notifications
You must be signed in to change notification settings - Fork 0
/
unregister.go
73 lines (57 loc) · 1.58 KB
/
unregister.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
package gitage
import (
"bufio"
"bytes"
"context"
"os"
"path/filepath"
"github.com/go-git/go-billy/v5"
"github.com/joanlopez/gitage/internal/fs"
"github.com/joanlopez/gitage/internal/log"
)
// Unregister docs (TODO)
// - path MUST be an absolute path.
func Unregister(ctx context.Context, f billy.Filesystem, path string, recipients ...string) error {
gitageDir := dir(path)
info, err := f.Stat(gitageDir)
if (err != nil && os.IsNotExist(err)) || !info.IsDir() {
log.For(ctx).Printf("%s directory not found...\nAre you in a Gitage repository?\n", gitageDir)
return nil
}
recipientsFilepath := filepath.Join(gitageDir, "recipients")
info, err = f.Stat(recipientsFilepath)
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
log.For(ctx).Printf("%s file not found...\nAre you in a Gitage repository?\n", gitageDir)
return nil
}
if err != nil {
return err
}
log.For(ctx).Println("Unregistering recipients...")
contents, err := fs.Read(f, recipientsFilepath)
if err != nil {
return err
}
var result []byte
fileScanner := bufio.NewScanner(bytes.NewBuffer(contents))
for fileScanner.Scan() {
next := fileScanner.Text()
if !present(next, recipients...) {
result = append(result, []byte(next)...)
result = append(result, []byte("\n")...)
}
}
if err := fs.Create(f, recipientsFilepath, result); err != nil {
return err
}
log.For(ctx).Println("Recipients unregistered with success!")
return nil
}
func present(r string, recipients ...string) bool {
for _, recipient := range recipients {
if r == recipient {
return true
}
}
return false
}