-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
flags=-ldflags="-s -w" | ||
# flags=-ldflags="-s -w -extldflags -static" | ||
TAG := $(shell git tag | sed -e "s,v,,g" | sort -r | head -n 1) | ||
|
||
all: build | ||
|
||
build: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg; go build -o mongotools ${flags} | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
|
||
build_all: build_darwin_amd64 build_darwin_arm64 build_amd64 build_arm64 build_power8 build_windows | ||
|
||
build_darwin_amd64: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools_darwin; GOOS=darwin go build -o mongotools ${flags} | ||
mv mongotools mongotools_darwin_amd64 | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
|
||
build_darwin_arm64: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools_darwin; GOARCH=arm64 GOOS=darwin go build -o mongotools ${flags} | ||
mv mongotools mongotools_darwin_arm64 | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
|
||
build_amd64: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools_linux; GOOS=linux go build -o mongotools ${flags} | ||
mv mongotools mongotools_amd64 | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
|
||
build_power8: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools_power8; GOARCH=ppc64le GOOS=linux go build -o mongotools ${flags} | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
mv mongotools mongotools_power8 | ||
|
||
build_arm64: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools_arm64; GOARCH=arm64 GOOS=linux go build -o mongotools ${flags} | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
mv mongotools mongotools_arm64 | ||
|
||
build_windows: | ||
ifdef TAG | ||
sed -i -e "s,{{VERSION}},$(TAG),g" main.go | ||
endif | ||
go clean; rm -rf pkg mongotools.exe; GOARCH=amd64 GOOS=windows go build -o mongotools.exe ${flags} | ||
ifdef TAG | ||
sed -i -e "s,$(TAG),{{VERSION}},g" main.go | ||
endif | ||
|
||
install: | ||
go install | ||
|
||
clean: | ||
go clean; rm -rf pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module github.com/vkuznet/ChessDataManagement/mongotools | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
go.mongodb.org/mongo-driver v1.13.1 | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 | ||
) | ||
|
||
require ( | ||
github.com/golang/snappy v0.0.1 // indirect | ||
github.com/klauspost/compress v1.13.6 // indirect | ||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect | ||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
github.com/xdg-go/scram v1.1.2 // indirect | ||
github.com/xdg-go/stringprep v1.0.4 // indirect | ||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect | ||
golang.org/x/text v0.7.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= | ||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= | ||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= | ||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= | ||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= | ||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= | ||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= | ||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= | ||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= | ||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= | ||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= | ||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= | ||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= | ||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= | ||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= | ||
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= | ||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= | ||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= | ||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
func info() string { | ||
goVersion := runtime.Version() | ||
tstamp := time.Now() | ||
return fmt.Sprintf("git={{VERSION}} go=%s date=%s", goVersion, tstamp) | ||
} | ||
|
||
func main() { | ||
var fname string | ||
flag.StringVar(&fname, "fname", "", "file name to read or write based on the action") | ||
var action string | ||
flag.StringVar(&action, "action", "", "action: export or import") | ||
var uri string | ||
flag.StringVar(&uri, "uri", "", "uri: export or import") | ||
var dbname string | ||
flag.StringVar(&dbname, "dbname", "", "database name") | ||
var collname string | ||
flag.StringVar(&collname, "collname", "", "collection name") | ||
var version bool | ||
flag.BoolVar(&version, "version", false, "Show version") | ||
flag.Parse() | ||
if version { | ||
fmt.Println("mongotools version:", info()) | ||
return | ||
} | ||
log.SetFlags(log.LstdFlags | log.Lshortfile) | ||
process(uri, dbname, collname, action, fname) | ||
} | ||
|
||
type Record map[string]any | ||
|
||
func importData(client *mongo.Client, dbname, collname, fname string) { | ||
log.Printf("import data using %s.%s from %s", dbname, collname, fname) | ||
c := client.Database(dbname).Collection(collname) | ||
var records []Record | ||
// read data from fname | ||
file, err := os.Open(fname) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
data, err := io.ReadAll(file) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = json.Unmarshal(data, &records) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, rec := range records { | ||
if _, err := c.InsertOne(context.TODO(), &rec); err != nil { | ||
log.Printf("Fail to insert record %v, error %v\n", rec, err) | ||
} | ||
} | ||
log.Printf("Successfully imported %d records from %s\n", len(records), fname) | ||
} | ||
|
||
func exportData(client *mongo.Client, dbname, collname, fname string) { | ||
log.Printf("export data using %s.%s to %s\n", dbname, collname, fname) | ||
c := client.Database(dbname).Collection(collname) | ||
var records []Record | ||
ctx := context.TODO() | ||
spec := bson.M{} | ||
opts := options.Find() | ||
cur, err := c.Find(ctx, spec, opts) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cur.All(ctx, &records) | ||
log.Printf("found %d records\n", len(records)) | ||
data, err := json.Marshal(records) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = os.WriteFile(fname, data, 0644) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("Successfully exported %d records to %s\n", len(records), fname) | ||
} | ||
|
||
func process(uri, dbname, collname, action, fname string) { | ||
client, err := mongo.NewClient(options.Client().ApplyURI(uri)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | ||
err = client.Connect(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if action == "import" { | ||
importData(client, dbname, collname, fname) | ||
} else if action == "export" { | ||
exportData(client, dbname, collname, fname) | ||
} | ||
} |