Skip to content
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

feat: multibase transcode command #8403

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestCommands(t *testing.T) {
"/multibase",
"/multibase/decode",
"/multibase/encode",
"/multibase/transcode",
"/multibase/list",
"/name",
"/name/publish",
Expand Down
59 changes: 56 additions & 3 deletions core/commands/multibase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ var MbaseCmd = &cmds.Command{
Tagline: "Encode and decode files or stdin with multibase format",
},
Subcommands: map[string]*cmds.Command{
"encode": mbaseEncodeCmd,
"decode": mbaseDecodeCmd,
"list": basesCmd,
"encode": mbaseEncodeCmd,
"decode": mbaseDecodeCmd,
"transcode": mbaseTranscodeCmd,
"list": basesCmd,
},
Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
}
Expand Down Expand Up @@ -116,3 +117,55 @@ This command expects multibase inside of a file or via stdin:
return resp.Emit(reader)
},
}

var mbaseTranscodeCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Transcode multibase string between bases",
LongDescription: `
This command expects multibase inside of a file or via stdin.

By default it will use URL-safe base64url encoding,
but one can customize used base with -b:

> echo -n hello | ipfs multibase encode > file
> cat file
uaGVsbG8

> ipfs multibase transcode file -b base16 > transcoded_file
> cat transcoded_file
f68656c6c6f
`,
},
Arguments: []cmds.Argument{
cmds.FileArg("encoded_file", true, false, "encoded data to decode").EnableStdin(),
},
Options: []cmds.Option{
cmds.StringOption(mbaseOptionName, "multibase encoding").WithDefault("base64url"),
},
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
if err := req.ParseBodyArgs(); err != nil {
return err
}
encoderName, _ := req.Options[mbaseOptionName].(string)
encoder, err := mbase.EncoderByName(encoderName)
if err != nil {
return err
}
files := req.Files.Entries()
file, err := cmdenv.GetFileArg(files)
if err != nil {
return fmt.Errorf("failed to access file: %w", err)
}
encoded_data, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file contents: %w", err)
}
_, data, err := mbase.Decode(string(encoded_data))
if err != nil {
return fmt.Errorf("failed to decode multibase: %w", err)
}
encoded := encoder.Encode(data)
reader := strings.NewReader(encoded)
return resp.Emit(reader)
},
}
8 changes: 4 additions & 4 deletions docs/examples/go-ipfs-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.15

require (
github.com/ipfs/go-ipfs v0.9.1
github.com/ipfs/go-ipfs-config v0.14.0
github.com/ipfs/go-ipfs-config v0.16.0
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/interface-go-ipfs-core v0.4.0
github.com/libp2p/go-libp2p-core v0.8.6
github.com/multiformats/go-multiaddr v0.3.3
github.com/ipfs/interface-go-ipfs-core v0.5.1
github.com/libp2p/go-libp2p-core v0.9.0
github.com/multiformats/go-multiaddr v0.4.0
)

replace github.com/ipfs/go-ipfs => ./../../..
Loading