Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
takeshi.nakata committed Aug 20, 2020
1 parent 570f065 commit dca6c05
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/nakatamixi/maskedenv

go 1.14
Empty file added go.sum
Empty file.
37 changes: 37 additions & 0 deletions main.go
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)
}
59 changes: 59 additions & 0 deletions main_test.go
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)
}
}
}

0 comments on commit dca6c05

Please sign in to comment.