-
Notifications
You must be signed in to change notification settings - Fork 7
/
userMap.go
63 lines (51 loc) · 1.5 KB
/
userMap.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
// Copyright 2020 Steve Jefferson. All rights reserved.
// Use of this source code is governed by a GPL-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/stevejefferson/trac2gitea/importer"
)
// readUserMap reads the user map from the provided file, if no file provided, import a default map using the provided importer
func readUserMap(mapFile string, dataImporter *importer.Importer) (map[string]string, error) {
if mapFile == "" {
return dataImporter.DefaultUserMap()
}
fd, err := os.Open(mapFile)
if err != nil {
return nil, err
}
defer fd.Close()
userMap := make(map[string]string)
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
userMapLine := scanner.Text()
equalsPos := strings.LastIndex(userMapLine, "=")
if equalsPos == -1 {
return nil, fmt.Errorf("badly formatted user map file %s: found line %s", mapFile, userMapLine)
}
tracUserName := strings.Trim(userMapLine[0:equalsPos], " ")
giteaUserName := strings.Trim(userMapLine[equalsPos+1:], " ")
userMap[tracUserName] = giteaUserName
}
if err := scanner.Err(); err != nil {
return nil, err
}
return userMap, nil
}
func writeUserMapToFile(mapFile string, userMap map[string]string) error {
fd, err := os.Create(mapFile)
if err != nil {
return err
}
defer fd.Close()
for tracUserName, giteaUserName := range userMap {
if _, err := fd.WriteString(tracUserName + " = " + giteaUserName + "\n"); err != nil {
return err
}
}
return nil
}