cmd

package
Version: v1.0.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Root is the root of the commands.
	Root = &cobra.Command{
		Use:           "csvtojson",
		SilenceErrors: true,
		SilenceUsage:  true,
		Short:         "csvtojson is a simple command line tool to parse CSV files and convert them to JSON",
		Long:          `csvtojson is a simple command line tool to parse CSV files and convert them to JSON. More documentation at https://go.krishnaiyer.dev/csvtojson`,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			err := manager.Unmarshal(config)
			if err != nil {
				panic(err)
			}
			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			logger, err := zephyrus.New(context.Background(), config.Debug)
			if err != nil {
				log.Fatal(err.Error())
			}
			defer logger.Clean()
			ctx := zephyrus.NewContextWithLogger(context.Background(), logger)

			raw, err := ioutil.ReadFile(config.CSVFile)
			if err != nil {
				logger.Fatal(err.Error())
			}
			loggerCtx := zephyrus.NewContextWithLogger(ctx, logger)
			csv, err := csv.New(loggerCtx, raw, config.Values)
			if err != nil {
				logger.Fatal(err.Error())
			}

			var marshaled []byte
			if config.YAML {
				marshaled, err = csv.MarshalYAML()
			} else {
				marshaled, err = csv.MarshalJSON()
			}
			if err != nil {
				logger.Fatal(err.Error())
			}

			file := os.Stdout
			if config.OutFile != "" {
				file, err = os.Create(config.OutFile)
				if err != nil {
					logger.Fatal(err.Error())
				}
			}
			_, err = file.Write(marshaled)
			if err != nil {
				logger.Fatal(err.Error())
			}
		},
	}
)

Functions

func Execute

func Execute()

Execute ...

func Version

func Version(root *cobra.Command) *cobra.Command

Version prints version information to the output stream.

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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