This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// ParseKey parse key by given format | ||
func ParseKey(flags *pflag.FlagSet, key string) ([]byte, error) { | ||
switch flags.Lookup("format").Value.String() { | ||
case "raw": | ||
return []byte(key), nil | ||
case "escaped": | ||
return unescapedKey(key) | ||
case "hex": | ||
key, err := hex.DecodeString(key) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return key, nil | ||
} | ||
return nil, errors.New("unknown format") | ||
} | ||
|
||
func unescapedKey(text string) ([]byte, error) { | ||
var buf []byte | ||
r := bytes.NewBuffer([]byte(text)) | ||
for { | ||
c, err := r.ReadByte() | ||
if err != nil { | ||
if err != io.EOF { | ||
return nil, errors.WithStack(err) | ||
} | ||
break | ||
} | ||
if c != '\\' { | ||
buf = append(buf, c) | ||
continue | ||
} | ||
n := r.Next(1) | ||
if len(n) == 0 { | ||
return nil, io.EOF | ||
} | ||
// See: https://golang.org/ref/spec#Rune_literals | ||
if idx := strings.IndexByte(`abfnrtv\'"`, n[0]); idx != -1 { | ||
buf = append(buf, []byte("\a\b\f\n\r\t\v\\'\"")[idx]) | ||
continue | ||
} | ||
|
||
switch n[0] { | ||
case 'x': | ||
fmt.Sscanf(string(r.Next(2)), "%02x", &c) | ||
buf = append(buf, c) | ||
default: | ||
n = append(n, r.Next(2)...) | ||
_, err := fmt.Sscanf(string(n), "%03o", &c) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
buf = append(buf, c) | ||
} | ||
} | ||
return buf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type testKeySuite struct{} | ||
|
||
var _ = Suite(&testKeySuite{}) | ||
|
||
func (r *testKeySuite) TestParseKey(c *C) { | ||
flagSet := &pflag.FlagSet{} | ||
flagSet.String("format", "raw", "") | ||
rawKey := "1234" | ||
parsedKey, err := ParseKey(flagSet, rawKey) | ||
c.Assert(err, IsNil) | ||
c.Assert(parsedKey, BytesEquals, []byte(rawKey)) | ||
|
||
flagSet = &pflag.FlagSet{} | ||
flagSet.String("format", "escaped", "") | ||
escapedKey := "\\a\\x1" | ||
parsedKey, err = ParseKey(flagSet, escapedKey) | ||
c.Assert(err, IsNil) | ||
c.Assert(parsedKey, BytesEquals, []byte("\a\x01")) | ||
|
||
flagSet = &pflag.FlagSet{} | ||
flagSet.String("format", "hex", "") | ||
hexKey := hex.EncodeToString([]byte("1234")) | ||
parsedKey, err = ParseKey(flagSet, hexKey) | ||
c.Assert(err, IsNil) | ||
c.Assert(parsedKey, BytesEquals, []byte("1234")) | ||
|
||
flagSet = &pflag.FlagSet{} | ||
flagSet.String("format", "notSupport", "") | ||
_, err = ParseKey(flagSet, rawKey) | ||
c.Assert(err, ErrorMatches, "*unknown format*") | ||
|
||
} |