Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Fix README.md, finish CLI tool
Browse files Browse the repository at this point in the history
Co-authored-by: John Burke <[email protected]>
  • Loading branch information
auguwu committed May 21, 2021
1 parent 36b91bf commit 004a406
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,6 @@ modules.xml

# Sonarlint plugin
.idea/sonarlint

# Rei cache
data/
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
> 🐳 **CLI script made in Go to serialize all documents in multiple collections to JSON and prints them out in a file**
## Installation
idk sometime in the future i guess.
### Prerequisites
Before running Rei, you must install **Go 1.16** or higher to use.

### Building
```sh
$ git clone https://github.com/NinoDiscord/Rei.git && cd Rei
$ go get && go build
```

You should get a `rei` binary if on UNIX or `rei.exe` on Windows.

## License
**Rei** is released under the **GPL-2.0** License.
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ func init() {
Short: "Starts the conversion process",
RunE: Execute,
}

logrus.Info(string(os.PathSeparator))
cwd, _ := os.Getwd()
flag := convertCmd.Flags()
flag.StringP("uri", "u", "", "The connection URI string")
flag.StringP("db", "d", "", "The database")
flag.StringP("db", "n", "", "The database")
flag.StringP("collections", "c", "", "List of collections to convert")
flag.StringP("directory", "d", cwd + string(os.PathSeparator) + "data", "A directory to place it in, it'll default to `$ROOT/data/<db>/<collection>.json`")
rootCmd.AddCommand(convertCmd)
}

func Execute(cmd *cobra.Command, args []string) error {
cwd, _ := os.Getwd()
collections, _ := cmd.Flags().GetString("collections")
uri, _ := cmd.Flags().GetString("uri")
db, _ := cmd.Flags().GetString("db")
dir, _ := cmd.Flags().GetString("directory")

if len(collections) == 0 {
return errors.New("missing collections array")
Expand All @@ -74,6 +80,27 @@ func Execute(cmd *cobra.Command, args []string) error {
return err
}

base := cwd + string(os.PathSeparator) + "data"
if dir != "" {
base = dir
}
_, err = os.Stat(base)
if os.IsNotExist(err) {
logrus.Infof("Create %s/", base)
err := os.Mkdir(base, 0644)
if err != nil {
logrus.Fatalf("Failed to create data directory: %v", err)
}
_, err = os.Stat(base + string(os.PathSeparator) + db)
if os.IsNotExist(err) {
logrus.Infof("Create %s/%s", base, db)
err := os.Mkdir(base + string(os.PathSeparator) + db, 0644)
if err != nil {
logrus.Fatalf("Failed to create the directory data/%s: %v", db, err)
}
}
}

mongoDb := client.Database(db)
cols := strings.Split(collections, ",")
logrus.Info(collections, cols)
Expand All @@ -82,7 +109,7 @@ func Execute(cmd *cobra.Command, args []string) error {
fmt.Printf("[%d/%d | %s] Now retrieving documents...\n", i + 1, len(cols), c)

collection := mongoDb.Collection(c)
if err := writer.WriteDocumentsToFile(c + ".json", collection); err != nil {
if err := writer.WriteDocumentsToFile(base + string(os.PathSeparator) + db + string(os.PathSeparator) + c + ".json", collection); err != nil {
return err
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func WriteDocumentsToFile(file string, collection *mongo.Collection) error {
}
results = append(results, val)
}

bytes, err := json.Marshal(results)
err = ioutil.WriteFile(file, bytes, 0644)

if err != nil {
logrus.Fatalf("Failed to write file %s: %v", file, err)
}
Expand Down

0 comments on commit 004a406

Please sign in to comment.