From 693702350d4e36cde676f4440cdee53363d8259b Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 28 Apr 2017 14:48:23 +0200 Subject: [PATCH] refactor --- common/crypto/aes.go | 10 ++++------ common/crypto/chacha20_test.go | 5 ++--- common/net/ipnet_test.go | 5 ++--- testing/scenarios/common.go | 7 ++----- tools/genproto/main.go | 9 ++++----- tools/geoip/geoip_gen.go | 5 ++--- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/common/crypto/aes.go b/common/crypto/aes.go index 2c1a1bd3d..983a33711 100644 --- a/common/crypto/aes.go +++ b/common/crypto/aes.go @@ -3,15 +3,15 @@ package crypto import ( "crypto/aes" "crypto/cipher" + + "v2ray.com/core/common" ) // NewAesDecryptionStream creates a new AES encryption stream based on given key and IV. // Caller must ensure the length of key and IV is either 16, 24 or 32 bytes. func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream { aesBlock, err := aes.NewCipher(key) - if err != nil { - panic(err) - } + common.Must(err) return cipher.NewCFBDecrypter(aesBlock, iv) } @@ -19,8 +19,6 @@ func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream { // Caller must ensure the length of key and IV is either 16, 24 or 32 bytes. func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream { aesBlock, err := aes.NewCipher(key) - if err != nil { - panic(err) - } + common.Must(err) return cipher.NewCFBEncrypter(aesBlock, iv) } diff --git a/common/crypto/chacha20_test.go b/common/crypto/chacha20_test.go index 14d57ab5c..7bca32a91 100644 --- a/common/crypto/chacha20_test.go +++ b/common/crypto/chacha20_test.go @@ -5,15 +5,14 @@ import ( "encoding/hex" "testing" + "v2ray.com/core/common" . "v2ray.com/core/common/crypto" "v2ray.com/core/testing/assert" ) func mustDecodeHex(s string) []byte { b, err := hex.DecodeString(s) - if err != nil { - panic(err) - } + common.Must(err) return b } diff --git a/common/net/ipnet_test.go b/common/net/ipnet_test.go index 5a3258df7..9b11cd55f 100644 --- a/common/net/ipnet_test.go +++ b/common/net/ipnet_test.go @@ -4,15 +4,14 @@ import ( "net" "testing" + "v2ray.com/core/common" . "v2ray.com/core/common/net" "v2ray.com/core/testing/assert" ) func parseCIDR(str string) *net.IPNet { _, ipNet, err := net.ParseCIDR(str) - if err != nil { - panic(err) - } + common.Must(err) return ipNet } diff --git a/testing/scenarios/common.go b/testing/scenarios/common.go index 313e30530..290f467f6 100644 --- a/testing/scenarios/common.go +++ b/testing/scenarios/common.go @@ -81,17 +81,14 @@ var ( func genTestBinaryPath() { testBinaryPathGen.Do(func() { var tempDir string - err := retry.Timed(5, 100).On(func() error { + common.Must(retry.Timed(5, 100).On(func() error { dir, err := ioutil.TempDir("", "v2ray") if err != nil { return err } tempDir = dir return nil - }) - if err != nil { - panic(err) - } + })) file := filepath.Join(tempDir, "v2ray.test") if runtime.GOOS == "windows" { file += ".exe" diff --git a/tools/genproto/main.go b/tools/genproto/main.go index 3576ec593..276260f9c 100644 --- a/tools/genproto/main.go +++ b/tools/genproto/main.go @@ -11,6 +11,8 @@ import ( "path/filepath" "runtime" "strings" + + "v2ray.com/core/common" ) var protocMap = map[string]string{ @@ -64,7 +66,7 @@ func main() { } } - err := filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error { + common.Must(filepath.Walk(reporoot, func(path string, info os.FileInfo, err error) error { if err != nil { fmt.Println(err) return err @@ -90,8 +92,5 @@ func main() { } return nil - }) - if err != nil { - panic(err) - } + })) } diff --git a/tools/geoip/geoip_gen.go b/tools/geoip/geoip_gen.go index 241e5671f..0c56ddda5 100644 --- a/tools/geoip/geoip_gen.go +++ b/tools/geoip/geoip_gen.go @@ -14,6 +14,7 @@ import ( "strings" "v2ray.com/core/app/router" + "v2ray.com/core/common" "v2ray.com/core/common/errors" "v2ray.com/core/tools/geoip" @@ -26,9 +27,7 @@ const ( func main() { resp, err := http.Get(apnicFile) - if err != nil { - panic(err) - } + common.Must(err) if resp.StatusCode != 200 { panic(errors.New("unexpected status ", resp.StatusCode)) }