|
| 1 | +// ================================================================= |
| 2 | +// |
| 3 | +// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved |
| 4 | +// Released as open source under the MIT License. See LICENSE file. |
| 5 | +// |
| 6 | +// ================================================================= |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "log" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/aws/aws-sdk-go/service/s3" |
| 20 | + "github.com/pkg/errors" |
| 21 | +) |
| 22 | + |
| 23 | +import ( |
| 24 | + "github.com/spatialcurrent/go-dfl/dfl" |
| 25 | + "github.com/spatialcurrent/go-reader/reader" |
| 26 | + "github.com/spatialcurrent/go-simple-serializer/gss" |
| 27 | +) |
| 28 | + |
| 29 | +import ( |
| 30 | + "github.com/spatialcurrent/railgun/railgun" |
| 31 | +) |
| 32 | + |
| 33 | +var GO_RAILGUN_VERSION = "0.0.1" |
| 34 | +var GO_RAILGUN_COMPRESSION_ALGORITHMS = []string{"none", "gzip", "snappy"} |
| 35 | +var GO_RAILGUN_FORMATS = []string{"csv", "tsv", "hcl", "hcl2", "json", "jsonl", "properties", "toml", "yaml"} |
| 36 | + |
| 37 | +func printUsage() { |
| 38 | + fmt.Println("Usage: railgun -input_format INPUT_FORMAT -o OUTPUT_FORMAT [-input_uri INPUT_URI] [-input_compression [bzip2|gzip|snappy]] [-h HEADER] [-c COMMENT] [-object_path PATH] [-f FILTER] [-output_path OUTPUT_PATH] [-max MAX_COUNT]") |
| 39 | +} |
| 40 | + |
| 41 | +func main() { |
| 42 | + |
| 43 | + var input_uri string |
| 44 | + var input_compression string |
| 45 | + var input_format string |
| 46 | + var input_header_text string |
| 47 | + var input_comment string |
| 48 | + |
| 49 | + var object_path string |
| 50 | + var object_filter string |
| 51 | + |
| 52 | + var output_format string |
| 53 | + var output_path string |
| 54 | + |
| 55 | + var max_count int |
| 56 | + |
| 57 | + var version bool |
| 58 | + var help bool |
| 59 | + |
| 60 | + flag.StringVar(&input_uri, "input_uri", "stdin", "The input uri") |
| 61 | + flag.StringVar(&input_compression, "input_compression", "none", "The input compression: "+strings.Join(GO_RAILGUN_COMPRESSION_ALGORITHMS, ", ")) |
| 62 | + flag.StringVar(&input_format, "input_format", "", "The input format: "+strings.Join(GO_RAILGUN_FORMATS, ", ")) |
| 63 | + flag.StringVar(&input_header_text, "h", "", "The input header if the stdin input has no header.") |
| 64 | + flag.StringVar(&input_comment, "c", "", "The input comment character, e.g., #. Commented lines are not sent to output.") |
| 65 | + flag.StringVar(&object_path, "object_path", "", "The output path") |
| 66 | + flag.StringVar(&object_filter, "f", "", "The output filter") |
| 67 | + flag.StringVar(&output_format, "o", "", "The output format: "+strings.Join(GO_RAILGUN_FORMATS, ", ")) |
| 68 | + flag.StringVar(&output_path, "output_path", "", "The output path") |
| 69 | + flag.IntVar(&max_count, "max", -1, "The maximum number of objects to output") |
| 70 | + flag.BoolVar(&version, "version", false, "Prints version to stdout.") |
| 71 | + flag.BoolVar(&help, "help", false, "Print help.") |
| 72 | + |
| 73 | + flag.Parse() |
| 74 | + |
| 75 | + if help { |
| 76 | + printUsage() |
| 77 | + fmt.Println("Options:") |
| 78 | + flag.PrintDefaults() |
| 79 | + os.Exit(0) |
| 80 | + } else if len(os.Args) == 1 { |
| 81 | + fmt.Println("Error: Provided no arguments.") |
| 82 | + fmt.Println("Run \"ralgun -help\" for more information.") |
| 83 | + os.Exit(0) |
| 84 | + } else if len(os.Args) == 2 && os.Args[1] == "help" { |
| 85 | + printUsage() |
| 86 | + fmt.Println("Options:") |
| 87 | + flag.PrintDefaults() |
| 88 | + os.Exit(0) |
| 89 | + } |
| 90 | + |
| 91 | + if version { |
| 92 | + fmt.Println(GO_RAILGUN_VERSION) |
| 93 | + os.Exit(0) |
| 94 | + } |
| 95 | + |
| 96 | + if len(input_format) == 0 { |
| 97 | + fmt.Println("Error: Provided no -input_format.") |
| 98 | + fmt.Println("Run \"railgun -help\" for more information.") |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + |
| 102 | + if len(output_format) == 0 { |
| 103 | + fmt.Println("Error: Provided no -output_format.") |
| 104 | + fmt.Println("Run \"railgun -help\" for more information.") |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + |
| 108 | + var s3_client *s3.S3 |
| 109 | + input_reader, err := reader.OpenResource(input_uri, input_compression, false, s3_client) |
| 110 | + if err != nil { |
| 111 | + log.Fatal(errors.Wrap(err, "error opening resource from uri "+input_uri)) |
| 112 | + } |
| 113 | + input_bytes, err := input_reader.ReadAll() |
| 114 | + if err != nil { |
| 115 | + log.Fatal(errors.Wrap(err, "Error reading from resource")) |
| 116 | + } |
| 117 | + input_string := string(input_bytes) |
| 118 | + |
| 119 | + input_header := make([]string, 0) |
| 120 | + if len(input_header_text) > 0 { |
| 121 | + input_header = strings.Split(input_header_text, ",") |
| 122 | + } |
| 123 | + |
| 124 | + var root dfl.Node |
| 125 | + var funcs dfl.FunctionMap |
| 126 | + if len(object_filter) > 0 { |
| 127 | + n, err := dfl.Parse(object_filter) |
| 128 | + if err != nil { |
| 129 | + log.Fatal(errors.Wrap(err, "Error parsing filter expression.")) |
| 130 | + } |
| 131 | + root = n.Compile() |
| 132 | + funcs = dfl.NewFuntionMapWithDefaults() |
| 133 | + } |
| 134 | + |
| 135 | + input_object := gss.NewObject(input_string, input_format) |
| 136 | + err = gss.Deserialize(input_string, input_format, input_header, input_comment, &input_object) |
| 137 | + if err != nil { |
| 138 | + log.Fatal(errors.Wrap(err, "error deserializing input using format "+input_format)) |
| 139 | + } |
| 140 | + |
| 141 | + output, err := railgun.Process(input_object, object_path, root, funcs, max_count, output_path) |
| 142 | + if err != nil { |
| 143 | + log.Fatal(errors.Wrap(err, "error processing")) |
| 144 | + } |
| 145 | + |
| 146 | + output_string, err := gss.Serialize(output, output_format) |
| 147 | + if err != nil { |
| 148 | + log.Fatal(errors.Wrap(err, "Error converting")) |
| 149 | + } |
| 150 | + fmt.Println(output_string) |
| 151 | +} |
0 commit comments