-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
102 lines (86 loc) · 2.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"context"
"flag"
"io/ioutil"
"os"
"github.com/google/subcommands"
"github.com/kitsuyui/invisible/embedding"
"github.com/kitsuyui/invisible/simplenoise"
)
type addNoise struct {
frequency float64
maxSize int
}
func (*addNoise) Name() string { return "add-noise" }
func (*addNoise) Synopsis() string { return "read from stdin and write to stdout with noise." }
func (*addNoise) Usage() string {
return `add-noise:
Read from stdin and write to stdout with noise.
`
}
func (p *addNoise) SetFlags(f *flag.FlagSet) {
f.Float64Var(&p.frequency, "frequency", 0.5, "frequency for noise")
f.Float64Var(&p.frequency, "f", 0.5, "frequency for noise")
f.IntVar(&p.maxSize, "noise-size", 1, "max noise in once")
f.IntVar(&p.maxSize, "s", 1, "max noise in once")
}
func (p *addNoise) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
simplenoise.AddRandomNoise(p.frequency, p.maxSize, reader, writer)
return subcommands.ExitSuccess
}
type encode struct {
message string
}
func (*encode) Name() string { return "encode" }
func (*encode) Synopsis() string {
return "read from stdin and write to stdout with noise made from encoded message"
}
func (*encode) Usage() string {
return `encode:
Read from stdin and write to stdout with noise made from encoded message.
`
}
func (p *encode) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.message, "m", "Hello, World!", "message")
f.StringVar(&p.message, "message", "Hello, World!", "message")
}
func (p *encode) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
embedding.Embed(p.message, reader, writer, true)
return subcommands.ExitSuccess
}
type decode struct{}
func (*decode) Name() string { return "decode" }
func (*decode) Synopsis() string {
return "read from stdin and write to stdout decoded message"
}
func (*decode) Usage() string {
return `decode:
Read from stdin and write to stdout decoded message.
`
}
func (p *decode) SetFlags(f *flag.FlagSet) {}
func (p *decode) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
decoded := embedding.Extract(reader, bufio.NewWriter(ioutil.Discard))
writer.WriteString(decoded)
writer.Flush()
return subcommands.ExitSuccess
}
func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&addNoise{}, "")
subcommands.Register(&encode{}, "")
subcommands.Register(&decode{}, "")
flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
}