-
Notifications
You must be signed in to change notification settings - Fork 11
/
groups.go
80 lines (74 loc) · 1.79 KB
/
groups.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
const (
allGroups = "ALL"
allBinaryGroups = "BINARIES"
)
var (
ErrNoGroups = errors.New("no groups found")
)
func scanGroups(groupsString string) error {
if groupsString == allBinaryGroups || groupsString == allGroups {
if verbose {
fmt.Println("Connecting to usenet server to get groups list")
}
conn, err := ConnectNNTP()
defer DisconnectNNTP(conn)
if err != nil {
fmt.Printf("Error while connecting to usenet server: %v\n", err)
return ErrNoGroups
}
filter := ""
if groupsString == allBinaryGroups {
filter = "alt.binaries.*"
}
groupsList, err := conn.List("ACTIVE", filter)
if err != nil {
fmt.Printf("Error while requesting list of groups: %v\n", err)
return ErrNoGroups
}
if verbose {
fmt.Println("Processing the groups")
}
for _, group := range groupsList {
groupData := strings.Split(string(group), " ")
groups = append(groups, groupData[0])
}
} else if _, err := os.Stat(groupsString); err == nil {
if verbose {
fmt.Printf("Reading groups file '%s'\n", groupsString)
}
err := readGroups(groupsString)
if err != nil {
fmt.Printf("Error while reading groups file '%s': %v\n", groupsString, err)
return ErrNoGroups
}
} else {
groups = strings.Split(groupsString, ",")
for i, group := range groups {
groups[i] = strings.Replace(strings.TrimSpace(group), "a.b.", "alt.binaries.", 1)
}
}
if len(groups) == 0 {
return ErrNoGroups
}
return nil
}
func readGroups(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
groups = append(groups, strings.Replace(strings.TrimSpace(scanner.Text()), "a.b.", "alt.binaries.", 1))
}
return scanner.Err()
}