/
cmd_base62.go
109 lines (93 loc) · 2.44 KB
/
cmd_base62.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
103
104
105
106
107
108
109
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"io"
"os"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/saltpack/encoding/basex"
)
func NewCmdBase62(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "base62",
Usage: "base62 operations",
Subcommands: []cli.Command{
NewCmdBase62Decode(cl, g),
NewCmdBase62Encode(cl, g),
},
}
}
type CmdBase62Decode struct {
libkb.Contextified
}
func NewBase62DecodeRunner(g *libkb.GlobalContext) *CmdBase62Decode {
return &CmdBase62Decode{
Contextified: libkb.NewContextified(g),
}
}
func NewCmdBase62Decode(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "decode",
Usage: "base62 decode",
Flags: []cli.Flag{},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewBase62DecodeRunner(g), "decode", c)
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
},
}
}
func (v *CmdBase62Decode) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 0 {
return errors.New("No arguments allowed")
}
return nil
}
func (v *CmdBase62Decode) Run() error {
_, err := io.Copy(os.Stdout, basex.NewDecoder(basex.Base62StdEncoding, os.Stdin))
return err
}
func (v *CmdBase62Decode) GetUsage() libkb.Usage {
return libkb.Usage{}
}
type CmdBase62Encode struct {
libkb.Contextified
}
func NewBase62EncodeRunner(g *libkb.GlobalContext) *CmdBase62Encode {
return &CmdBase62Encode{
Contextified: libkb.NewContextified(g),
}
}
func NewCmdBase62Encode(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "encode",
Usage: "base62 encode",
Flags: []cli.Flag{},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewBase62EncodeRunner(g), "encode", c)
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
},
}
}
func (v *CmdBase62Encode) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 0 {
return errors.New("No arguments allowed")
}
return nil
}
func (v *CmdBase62Encode) Run() error {
enc := basex.NewEncoder(basex.Base62StdEncoding, os.Stdout)
_, err := io.Copy(enc, os.Stdin)
if err != nil {
return err
}
err = enc.Close()
return err
}
func (v *CmdBase62Encode) GetUsage() libkb.Usage {
return libkb.Usage{}
}