Skip to content

Commit eed9bde

Browse files
committed
initial work
0 parents  commit eed9bde

9 files changed

Lines changed: 451 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin
2+
*.so
3+
*.h

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
sudo: false
2+
language: go
3+
4+
go:
5+
- 1.9
6+
- "1.10"
7+
- tip
8+
9+
os:
10+
- linux
11+
- osx
12+
13+
matrix:
14+
allow_failures:
15+
- go: tip
16+
fast_finish: true
17+
18+
script:
19+
- cd railgun
20+
- go fmt
21+
- cd ./../cmd/railgun
22+
- go fmt

AUTHORS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
railgun is maintained by Spatial Current, Inc.
2+
3+
Authors:
4+
5+
* Patrick Dufour (pjdufour)

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing to railgun
2+
3+
## Contributor License Agreement
4+
5+
Thank you for your interest in contributing. You will first need to agree to the license. Simply post the following paragraph, with "your name" and "your github account" substituted as needed, to the first issue [#1](https://github.com/spatialcurrent/railgun/issues/1). Otherwise, you can email us [here](mailto:opensource@spatialcurrent.io?subject=CLA).
6+
7+
I, **< YOUR NAME > (< YOUR GITHUB ACCOUNT >)**, agree to the license terms. My contributions to this repo are granted to **Spatial Current, Inc.** under a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable license and/or copyright is transferred.
8+
9+
## Versioning
10+
11+
This library is still in alpha (0.x.x), so versioning is not semantic yet.
12+
13+
## Authors
14+
15+
See [AUTHORS](https://github.com/spatialcurrent/railgun/blob/master/AUTHORS) for a list of contributors.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Spatial Current, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![Build Status](https://travis-ci.org/spatialcurrent/railgun.svg)](https://travis-ci.org/spatialcurrent/railgun) [![GoDoc](https://godoc.org/github.com/spatialcurrent/railgun?status.svg)](https://godoc.org/github.com/spatialcurrent/railgun)
2+
3+
# Railgun
4+
5+
# Description
6+
7+
**Railgun** is a simple and fast data processing tool. **Railgun** uses [go-simple-serializer](https://github.com/spatialcurrent/go-simple-serializer) (GSS) for reading/writing objects to standard formats. **Railgun** uses [go-dfl](https://github.com/spatialcurrent/go-dfl) for filtering.
8+
9+
GSS supports `bson`, `csv`, `tsv`, `hcl`, `hcl2`, `json`, `jsonl`, `properties`, `toml`, `yaml`. `hcl` and `hcl2` implementation is fragile and very much in `alpha`.
10+
11+
# Usage
12+
13+
**CLI**
14+
15+
You can use the command line tool to convert between formats.
16+
17+
```
18+
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]
19+
Options:
20+
-c string
21+
The input comment character, e.g., #. Commented lines are not sent to output.
22+
-f string
23+
The output filter
24+
-h string
25+
The input header if the stdin input has no header.
26+
-help
27+
Print help.
28+
-input_compression string
29+
The input compression: none, gzip, snappy (default "none")
30+
-input_format string
31+
The input format: csv, tsv, hcl, hcl2, json, jsonl, properties, toml, yaml
32+
-input_uri string
33+
The input uri (default "stdin")
34+
-max int
35+
The maximum number of objects to output (default -1)
36+
-o string
37+
The output format: csv, tsv, hcl, hcl2, json, jsonl, properties, toml, yaml
38+
-object_path string
39+
The output path
40+
-output_path string
41+
The output path
42+
-version
43+
Prints version to stdout.
44+
```
45+
46+
**Go**
47+
48+
You can import **railgun** as a library with:
49+
50+
```go
51+
import (
52+
"github.com/spatialcurrent/go-railgun/railgun"
53+
)
54+
```
55+
56+
The `Process` function is the core functions to use.
57+
58+
```go
59+
...
60+
output_object, err := railgun.Process(input_object, object_path, filter, funcs, max_count, output_path)
61+
...
62+
output_string, err := gss.Serialize(output_object, output_format)
63+
...
64+
```
65+
66+
# Releases
67+
68+
**Railgun** is currently in **alpha**. See releases at https://github.com/spatialcurrent/railgun/releases.
69+
70+
# Examples
71+
72+
TBD
73+
74+
# Building
75+
76+
**CLI**
77+
78+
The `build_cli.sh` script is used to build executables for Linux and Windows.
79+
80+
# Contributing
81+
82+
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/railgun/blob/master/CONTRIBUTING.md) for how to get started.
83+
84+
# License
85+
86+
This work is distributed under the **MIT License**. See **LICENSE** file.

cmd/railgun/main.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)