Esempio n. 1
0
func main() {
	app := cli.NewApp()
	app.Name = "fieldextract"
	app.Usage = "Retrieves the fields of data structures & prints them to stdout"
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:        "debug",
			Usage:       "debug mode",
			Destination: &debugMode,
		},
		cli.StringFlag{
			Name:        "fields",
			Usage:       "Fields to extract",
			Destination: &fieldsArg,
		},
		cli.StringFlag{
			Name:        "format",
			Usage:       "Format of data structure",
			Value:       "json",
			Destination: &formatArg,
		},
		cli.StringFlag{
			Name:        "separator",
			Usage:       "Output separator",
			Value:       "\t",
			Destination: &separatorArg,
		},
		cli.BoolFlag{
			Name:        "withnames",
			Usage:       "Output field names",
			Destination: &withNamesArg,
		},
		cli.BoolFlag{
			Name:        "trimquotes",
			Usage:       "trim quotes",
			Destination: &trimQuotesArg,
		},
		cli.BoolFlag{
			Name:        "skipempty",
			Usage:       "skip rows with empty fields",
			Destination: &skipEmptyArg,
		},
	}

	app.Action = func(c *cli.Context) {

		// this func's called for each row in stdinы
		process := func(row []byte) error {

			debug(string(row))

			res, err := extractFields(string(row), fieldsArg)
			if err != nil {
				log.Printf("ERROR: %s\n", err.Error())
			} else {
				fmt.Println(res)
			}

			return nil
		}

		err := iostreams.ProcessStdin(process)
		if err != nil {
			log.Panicln(err.Error())
		}
	}

	app.Run(os.Args)
}
Esempio n. 2
0
func main() {
	app := cli.NewApp()
	app.Name = "fieldextract"
	app.Usage = "Retrieves the fields of data structures & prints them to stdout"
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:        "debug",
			Usage:       "debug mode",
			EnvVar:      "SEQREQUEST_DEBUG",
			Destination: &debugMode,
		},
		cli.StringFlag{
			Name:        "url",
			Usage:       "request url",
			Destination: &urlArg,
		},
		cli.StringFlag{
			Name:        "data",
			Usage:       "parameters",
			Destination: &dataArg,
		},
		cli.StringFlag{
			Name:        "method",
			Usage:       "method",
			Value:       "GET",
			Destination: &methodArg,
		},
		cli.StringFlag{
			Name:        "separator",
			Usage:       "Output separator",
			Value:       "\t",
			Destination: &separatorArg,
		},
		cli.BoolFlag{
			Name:        "through",
			Usage:       "translate stdin to stdout",
			Destination: &throughMode,
		},
		cli.BoolFlag{
			Name:        "fake",
			Usage:       "fake mode (without http requests)",
			Destination: &fakeMode,
		},
	}

	app.Action = func(c *cli.Context) {

		// this func's called for each stdin's row
		process := func(row []byte) error {

			debug(string(row))

			reqParams := strings.Split(string(row), separatorArg)

			urlString := urlArg
			dataString := dataArg
			for i, param := range reqParams {
				paramTpl := fmt.Sprintf("{%d}", i+1)
				urlString = strings.Replace(urlString, paramTpl, param, -1)
				dataString = strings.Replace(dataString, paramTpl, param, -1)
			}

			debug("Url: %s", urlString)
			debug("Data: %s", dataString)

			if fakeMode {
				return nil
			}

			err := sendRequest(urlString, dataString)
			if err != nil {
				log.Println(err.Error())
			}

			return nil
		}

		iostreams.ThroughMode = throughMode

		err := iostreams.ProcessStdin(process)
		if err != nil {
			log.Panicln(err.Error())
		}
	}

	app.Run(os.Args)
}