-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] add multibase binary #42
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= | ||
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ= | ||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= | ||
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI= | ||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA= | ||
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4= | ||
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= | ||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= | ||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= | ||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= | ||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/multiformats/go-multibase" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func main() { | ||
app := &cli.App{ | ||
Name: "multibase", | ||
Usage: "base encoding and transcoding tool", | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "encode", | ||
ArgsUsage: "<base>", | ||
Usage: "encode data in multibase", | ||
Flags: []cli.Flag{ | ||
&cli.PathFlag{ | ||
Name: "input", | ||
Aliases: []string{"i"}, | ||
Usage: "the file that should be encoded", | ||
}, | ||
}, | ||
Action: func(context *cli.Context) error { | ||
if !context.Args().Present() || context.NArg() > 2 { | ||
return cli.ShowCommandHelp(context, "") | ||
} | ||
|
||
p := context.Path("input") | ||
if (p == "" && context.NArg() != 2) || (p != "" && context.NArg() != 1) { | ||
return cli.ShowCommandHelp(context, "") | ||
} | ||
|
||
base, err := multibase.EncoderByName(context.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if p != "" { | ||
fileData, err := ioutil.ReadFile(p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: stdin? I'd expect |
||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(base.Encode(fileData)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd lift this out and read the data (file/argument) into a common variable first. |
||
return nil | ||
} | ||
|
||
fmt.Println(base.Encode([]byte(context.Args().Get(1)))) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "decode", | ||
ArgsUsage: "<data>", | ||
Usage: "encode data in multibase", | ||
Flags: []cli.Flag{ | ||
&cli.PathFlag{ | ||
Name: "output", | ||
Aliases: []string{"o"}, | ||
Usage: "output decoded data to a file", | ||
}, | ||
}, | ||
Action: func(context *cli.Context) error { | ||
if context.NArg() != 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, |
||
return cli.ShowCommandHelp(context, "") | ||
} | ||
|
||
_, data, err := multibase.Decode(context.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p := context.Path("output") | ||
if p == "" { | ||
fmt.Printf(string(data)) | ||
return nil | ||
} | ||
return ioutil.WriteFile(p, data, os.ModePerm) | ||
}, | ||
}, | ||
{ | ||
Name: "transcode", | ||
ArgsUsage: "<new-base> <data>", | ||
Usage: "transcode multibase data", | ||
Action: func(context *cli.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
if context.NArg() != 2 { | ||
return cli.ShowCommandHelp(context, "") | ||
} | ||
|
||
newbase, err := multibase.EncoderByName(context.Args().Get(0)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, data, err := multibase.Decode(context.Args().Get(1)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(newbase.Encode(data)) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, maybe make the base a flag? I.e.,
--base=xyz
? That way, we can set a reasonable default base (e.g., base32) so users can just call$(multibase encode "foobar")
and get the result they expect.