-
Notifications
You must be signed in to change notification settings - Fork 0
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
takeshi.nakata
committed
Aug 20, 2020
1 parent
570f065
commit dca6c05
Showing
4 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/nakatamixi/maskedenv | ||
|
||
go 1.14 |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var ( | ||
headSize = flag.Int("h", 0, "unmask head size") | ||
tailSize = flag.Int("t", 0, "unmask tail size") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
for _, e := range os.Environ() { | ||
pair := strings.SplitN(e, "=", 2) | ||
fmt.Printf("%s=%s\n", pair[0], MaskedStr(pair[1], *headSize, *tailSize)) | ||
} | ||
} | ||
|
||
func MaskedStr(origin string, hs, ts int) string { | ||
head := origin | ||
tail := origin | ||
if len(origin) >= hs { | ||
head = origin[:hs] | ||
} | ||
if len(origin) >= ts { | ||
tail = origin[len(origin)-ts:] | ||
} | ||
masked := "" | ||
if len(origin)-hs-ts > 0 { | ||
masked = strings.Repeat("*", len(origin)-hs-ts) | ||
} | ||
return fmt.Sprintf("%s%s%s", head, masked, tail) | ||
} |
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,59 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestMaskedStr(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
origin string | ||
hs, ts int | ||
expected string | ||
}{ | ||
{ | ||
name: "empty origin", | ||
origin: "", hs: 0, ts: 0, | ||
expected: "", | ||
}, | ||
{ | ||
name: "full masked", | ||
origin: "ab", hs: 0, ts: 0, | ||
expected: "**", | ||
}, | ||
{ | ||
name: "head size within length", | ||
origin: "ab", hs: 1, ts: 0, | ||
expected: "a*", | ||
}, | ||
{ | ||
name: "tail size within length", | ||
origin: "ab", hs: 0, ts: 1, | ||
expected: "*b", | ||
}, | ||
{ | ||
name: "head size equal length", | ||
origin: "ab", hs: 2, ts: 0, | ||
expected: "ab", | ||
}, | ||
{ | ||
name: "tail size equal length", | ||
origin: "ab", hs: 0, ts: 2, | ||
expected: "ab", | ||
}, | ||
{ | ||
name: "head size over length", | ||
origin: "ab", hs: 3, ts: 0, | ||
expected: "ab", | ||
}, | ||
{ | ||
name: "tail size over length", | ||
origin: "ab", hs: 0, ts: 3, | ||
expected: "ab", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
actual := MaskedStr(tc.origin, tc.hs, tc.ts) | ||
if actual != tc.expected { | ||
t.Errorf("MaskedStr failed. case: %s, actual: %s", tc.name, actual) | ||
} | ||
} | ||
} |