|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/multiformats/go-multibase" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/urfave/cli/v2" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + app := &cli.App{ |
| 14 | + Name: "multibase", |
| 15 | + Usage: "base encoding and transcoding tool", |
| 16 | + Commands: []*cli.Command{ |
| 17 | + { |
| 18 | + Name: "encode", |
| 19 | + ArgsUsage: "<base>", |
| 20 | + Usage: "encode data in multibase", |
| 21 | + Flags: []cli.Flag{ |
| 22 | + &cli.PathFlag{ |
| 23 | + Name: "input", |
| 24 | + Aliases: []string{"i"}, |
| 25 | + Usage: "the file that should be encoded", |
| 26 | + }, |
| 27 | + }, |
| 28 | + Action: func(context *cli.Context) error { |
| 29 | + if !context.Args().Present() || context.NArg() > 2 { |
| 30 | + return cli.ShowCommandHelp(context, "") |
| 31 | + } |
| 32 | + |
| 33 | + p := context.Path("input") |
| 34 | + if (p == "" && context.NArg() != 2) || (p != "" && context.NArg() != 1) { |
| 35 | + return cli.ShowCommandHelp(context, "") |
| 36 | + } |
| 37 | + |
| 38 | + base, err := multibase.EncoderByName(context.Args().First()) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + if p != "" { |
| 44 | + fileData, err := ioutil.ReadFile(p) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + fmt.Println(base.Encode(fileData)) |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + fmt.Println(base.Encode([]byte(context.Args().Get(1)))) |
| 53 | + return nil |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "decode", |
| 58 | + ArgsUsage: "<data>", |
| 59 | + Usage: "encode data in multibase", |
| 60 | + Flags: []cli.Flag{ |
| 61 | + &cli.PathFlag{ |
| 62 | + Name: "output", |
| 63 | + Aliases: []string{"o"}, |
| 64 | + Usage: "output decoded data to a file", |
| 65 | + }, |
| 66 | + }, |
| 67 | + Action: func(context *cli.Context) error { |
| 68 | + if context.NArg() != 1 { |
| 69 | + return cli.ShowCommandHelp(context, "") |
| 70 | + } |
| 71 | + |
| 72 | + _, data, err := multibase.Decode(context.Args().First()) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + p := context.Path("output") |
| 78 | + if p == "" { |
| 79 | + fmt.Printf(string(data)) |
| 80 | + return nil |
| 81 | + } |
| 82 | + return ioutil.WriteFile(p, data, os.ModePerm) |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + Name: "transcode", |
| 87 | + ArgsUsage: "<new-base> <data>", |
| 88 | + Usage: "transcode multibase data", |
| 89 | + Action: func(context *cli.Context) error { |
| 90 | + if context.NArg() != 2 { |
| 91 | + return cli.ShowCommandHelp(context, "") |
| 92 | + } |
| 93 | + |
| 94 | + newbase, err := multibase.EncoderByName(context.Args().Get(0)) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + _, data, err := multibase.Decode(context.Args().Get(1)) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Println(newbase.Encode(data)) |
| 105 | + return nil |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + err := app.Run(os.Args) |
| 112 | + if err != nil { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | +} |
0 commit comments