From 83cedde2df0ac80c02004213b76d98531fee7d3a Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 12 Apr 2022 14:56:06 -0700 Subject: [PATCH] Added -x flag to tile38-cli The new -x flag allows for passing large objects from STDIN. tile38-cli -x SET cities tempe OBJECT Server hostname (default: %s)\n", hostname) fmt.Fprintf(os.Stdout, " -p Server port (default: %d)\n", port) + fmt.Fprintf(os.Stdout, " -x Read last argument from STDIN.\n") fmt.Fprintf(os.Stdout, "\n") return false } @@ -117,6 +119,8 @@ func parseArgs() bool { output = "resp" case "--json": output = "json" + case "-x": + stdin = true case "-h": hostname = readArg(arg) case "-p": @@ -128,6 +132,20 @@ func parseArgs() bool { } } oneCommand = strings.Join(args, " ") + if stdin { + data, err := io.ReadAll(os.Stdin) + if err != nil { + println(err) + } + if !gjson.ValidBytes(data) { + fmt.Fprintf(os.Stderr, "Invalid STDIN: Not JSON\n") + return false + } + arg := strings.Replace(string(data), "\r", "", -1) + arg = strings.Replace(arg, "\n", "", -1) + arg = strings.Replace(arg, "'", "\\'", -1) + oneCommand += " '" + arg + "'" + } return true }