encoding

package
v0.0.0-...-b1c1fed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Command = &cli.Command{
		Name:      "encode",
		Usage:     "Read and write different encodings.",
		UsageText: "em encode [message]",
		Flags:     flagset.ExtractPrefix("em", encodeConfig),
		Aliases:   []string{"enc"},
		Action: func(ctx *cli.Context) error {
			writer := bufio.NewWriter(ctx.App.Writer)

			var reader io.Reader = bufio.NewReader(os.Stdin)
			if ctx.NArg() > 0 {
				reader = strings.NewReader(ctx.Args().Get(0))
			}

			decoder := reader
			switch encodeConfig.In {
			case "base64", "b64":
				decoder = base64.NewDecoder(base64.StdEncoding, reader)
			case "base64url", "b64url":
				decoder = base64.NewDecoder(base64.URLEncoding, reader)
			case "base32", "b32":
				decoder = base32.NewDecoder(base32.StdEncoding, reader)
			case "base32hex", "b32hex":
				decoder = base32.NewDecoder(base32.HexEncoding, reader)
			case "hex":
				decoder = hex.NewDecoder(reader)
			}

			var encoder io.Writer = writer
			switch encodeConfig.Out {
			case "base64", "b64":
				encoder = base64.NewEncoder(base64.StdEncoding, writer)
			case "base64url", "b64url":
				encoder = base64.NewEncoder(base64.URLEncoding, writer)
			case "base32", "b32":
				encoder = base32.NewEncoder(base32.StdEncoding, writer)
			case "base32hex", "b32hex":
				encoder = base32.NewEncoder(base32.HexEncoding, writer)
			case "hex":
				encoder = hex.NewEncoder(writer)
			case "phone":
				encoder = phone.NewEncoder(writer)
			}

			defer func() {
				defer writer.Flush()

				if readCloser, rcOK := decoder.(io.Closer); rcOK {
					_ = readCloser.Close()
				}

				if writeCloser, wcOK := encoder.(io.Closer); wcOK {
					_ = writeCloser.Close()
				}
			}()

			_, err := io.Copy(encoder, decoder)
			switch {
			case err == io.EOF:
			case err != nil:
				return err
			}

			return nil
		},
		HideHelpCommand: true,
	}
)

Functions

This section is empty.

Types

type EncodeConfig

type EncodeConfig struct {
	In  string `json:"in"  alias:"i" usage:"the input encoding"  default:"ascii"`
	Out string `json:"out" alias:"o" usage:"the output encoding" default:"ascii"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL