Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/main/bns/lib/resolvconf:all-srcs",
"//app/service/main/bns/lib/shuffle:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["resolvconf_unix_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["resolvconf_unix.go"],
importpath = "go-common/app/service/main/bns/lib/resolvconf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,56 @@
// +build linux darwin
package resolvconf
import (
"bufio"
"io"
"os"
"strings"
)
const (
resolvConfPath = "/etc/resolv.conf"
)
// ParseResolvConf parse /etc/resolv.conf file and return nameservers
func ParseResolvConf() ([]string, error) {
fp, err := os.Open(resolvConfPath)
if err != nil {
return nil, err
}
defer fp.Close()
return parse(fp)
}
func parse(fp io.Reader) ([]string, error) {
var result []string
bufRd := bufio.NewReader(fp)
for {
line, err := bufRd.ReadString('\n')
if err != nil {
if err != io.EOF {
return nil, err
}
if line == "" {
break
}
}
line = strings.TrimSpace(line)
// ignore comment, comment startwith #
if strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
if fields[0] == "nameserver" {
result = append(result, fields[1:]...)
}
}
return result, nil
}

View File

@@ -0,0 +1,72 @@
// +build linux darwin
package resolvconf
import (
"bytes"
"io"
"reflect"
"testing"
)
const (
testdata1 = `domain localdomain
search localdomain
nameserver 192.168.6.2`
testdata2 = `#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
# scutil --dns
#
# SEE ALSO
# dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
nameserver 10.23.194.202
nameserver 10.23.194.203`
)
func Test_parse(t *testing.T) {
type args struct {
fp io.Reader
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{
name: "test1",
args: args{
bytes.NewBufferString(testdata1),
},
want: []string{"192.168.6.2"},
},
{
name: "test2",
args: args{
bytes.NewBufferString(testdata2),
},
want: []string{"10.23.194.202", "10.23.194.203"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parse(tt.args.fp)
if (err != nil) != tt.wantErr {
t.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parse() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -0,0 +1,8 @@
// +build windows
package resolvconf
// ParseResolvConf TODO: windows support
func ParseResolvConf() ([]string, error) {
return []string{}, nil
}

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["shuffle_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["shuffle.go"],
importpath = "go-common/app/service/main/bns/lib/shuffle",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,26 @@
package shuffle
import (
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Shuffler A type, typically a collection, that satisfies Shuffler
// can be shuffle by Shuffle func
type Shuffler interface {
Len() int
Swap(i, j int)
}
// Shuffle s
func Shuffle(s Shuffler) {
l := s.Len()
for i := l; i > 0; i-- {
j := rand.Intn(i)
s.Swap(l-i, j)
}
}

View File

@@ -0,0 +1,27 @@
package shuffle
import (
"strings"
"testing"
)
type List []string
func (l List) Len() int {
return len(l)
}
func (l List) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func TestShuffle(t *testing.T) {
l := List{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
old := strings.Join(l, "")
Shuffle(l)
new := strings.Join(l, "")
if old == new {
t.Errorf("shuffle error, %s == %s", old, new)
}
t.Log(new)
}