-
Notifications
You must be signed in to change notification settings - Fork 10
/
alias.go
50 lines (44 loc) · 856 Bytes
/
alias.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
// Alias holds name mapping
type Alias struct {
m map[string]string
}
// NewAlias is used to create new name mapping from given file
func NewAlias(file string) (*Alias, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
m := make(map[string]string)
reader := bufio.NewReader(f)
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
tokens := strings.Split(line, ":")
if len(tokens) != 2 {
return nil, fmt.Errorf("alias mapping has syntax error for line (%s)", line)
}
m[strings.TrimSpace(tokens[1])] = strings.TrimSpace(tokens[0])
}
return &Alias{m: m}, nil
}
func getAlias(a *Alias, input string) string {
if a == nil {
return input
}
name, ok := a.m[input]
if !ok {
return input
}
return name
}