Skip to content

Commit

Permalink
Merge pull request #6 from OkieOth/add_go_funcs
Browse files Browse the repository at this point in the history
Add go funcs
  • Loading branch information
OkieOth authored Sep 16, 2024
2 parents 3012aac + 657faed commit b2a21b4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 94 deletions.
2 changes: 1 addition & 1 deletion buildImage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scriptPos=${0%/*}


imageBase=ghcr.io/okieoth/mschemaguesser
imageTag=`cat $scriptPos/version.txt | grep -P '\d+\.\d+\.\d+'`
imageTag=`cat $scriptPos/cmd/schemaguesser/cmd/version.go | grep "const Version =" | sed -e 's-.*= "--' -e 's-".*--'`

imageName="$imageBase:$imageTag"

Expand Down
59 changes: 40 additions & 19 deletions cmd/schemaguesser/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"

"okieoth/schemaguesser/internal/pkg/mongoHelper"

Expand All @@ -27,7 +28,14 @@ var databasesCmd = &cobra.Command{
Short: "Names of databases available in the connected server",
Long: "Prints a list of available databases i the connected server",
Run: func(cmd *cobra.Command, args1 []string) {
dbs := mongoHelper.ReadDatabasesOrPanic()
client, err := mongoHelper.Connect(mongoHelper.ConStr)
if err != nil {
msg := fmt.Sprintf("Failed to connect to db: %v", err)
panic(msg)
}
defer mongoHelper.CloseConnection(client)

dbs := mongoHelper.ReadDatabasesOrPanic(client)
for _, s := range *dbs {
fmt.Println(s)
}
Expand All @@ -39,10 +47,17 @@ var collectionsCmd = &cobra.Command{
Short: "Collections available in the specified database",
Long: "Provides information about existing collections in the specified database",
Run: func(cmd *cobra.Command, args []string) {
client, err := mongoHelper.Connect(mongoHelper.ConStr)
if err != nil {
msg := fmt.Sprintf("Failed to connect to db: %v", err)
panic(msg)
}
defer mongoHelper.CloseConnection(client)

if databaseName == "all" {
printAllCollections()
printAllCollections(client)
} else {
printOneCollection(databaseName, false)
printOneCollection(client, databaseName, false)
}
},
}
Expand All @@ -52,14 +67,20 @@ var indexesCmd = &cobra.Command{
Short: "Indexes to a given collection",
Long: "Provides information about indexes of a given collection",
Run: func(cmd *cobra.Command, args []string) {
client, err := mongoHelper.Connect(mongoHelper.ConStr)
if err != nil {
msg := fmt.Sprintf("Failed to connect to db: %v", err)
panic(msg)
}
defer mongoHelper.CloseConnection(client)

if databaseName == "all" {
printIndexesForAllDatabases()
printIndexesForAllDatabases(client)
} else {
if collectionName == "all" {
printIndexesForAllCollections(databaseName)
printIndexesForAllCollections(client, databaseName)
} else {
printIndexesForOneCollection(databaseName, collectionName, false)
printIndexesForOneCollection(client, databaseName, collectionName, false)
}
}
},
Expand All @@ -76,8 +97,8 @@ func init() {
indexesCmd.Flags().StringVar(&collectionName, "collection", "all", "Name of the collection to show the indexes. If 'all', then the collections of all databases are printed.")
}

func printOneCollection(dbName string, verbose bool) {
collections := mongoHelper.ReadCollectionsOrPanic(dbName)
func printOneCollection(client *mongo.Client, dbName string, verbose bool) {
collections := mongoHelper.ReadCollectionsOrPanic(client, dbName)
for _, s := range *collections {
if verbose {
fmt.Printf("Database: %s, Collection: %s\n", dbName, s)
Expand All @@ -87,15 +108,15 @@ func printOneCollection(dbName string, verbose bool) {
}
}

func printAllCollections() {
dbs := mongoHelper.ReadDatabasesOrPanic()
func printAllCollections(client *mongo.Client) {
dbs := mongoHelper.ReadDatabasesOrPanic(client)
for _, db := range *dbs {
printOneCollection(db, true)
printOneCollection(client, db, true)
}
}

func printIndexesForOneCollection(dbName string, collName string, verbose bool) {
indexes, err := mongoHelper.ListIndexes(mongoHelper.ConStr, dbName, collName)
func printIndexesForOneCollection(client *mongo.Client, dbName string, collName string, verbose bool) {
indexes, err := mongoHelper.ListIndexes(client, dbName, collName)
if err != nil {
msg := fmt.Sprintf("Error while reading indexes for collection (%s.%s): \n%v\n", dbName, collName, err)
panic(msg)
Expand All @@ -109,16 +130,16 @@ func printIndexesForOneCollection(dbName string, collName string, verbose bool)
}
}

func printIndexesForAllCollections(dbName string) {
collections := mongoHelper.ReadCollectionsOrPanic(dbName)
func printIndexesForAllCollections(client *mongo.Client, dbName string) {
collections := mongoHelper.ReadCollectionsOrPanic(client, dbName)
for _, coll := range *collections {
printIndexesForOneCollection(dbName, coll, true)
printIndexesForOneCollection(client, dbName, coll, true)
}
}

func printIndexesForAllDatabases() {
dbs := mongoHelper.ReadDatabasesOrPanic()
func printIndexesForAllDatabases(client *mongo.Client) {
dbs := mongoHelper.ReadDatabasesOrPanic(client)
for _, db := range *dbs {
printIndexesForAllCollections(db)
printIndexesForAllCollections(client, db)
}
}
1 change: 1 addition & 0 deletions cmd/schemaguesser/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(schemaCmd)
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(versionCmd)

rootCmd.PersistentFlags().StringVar(&mongoHelper.ConStr, "con_str", "mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:{MONGO_PORT}/admin", "Connection string to mongodb")
}
Expand Down
45 changes: 33 additions & 12 deletions cmd/schemaguesser/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"sync"

"okieoth/schemaguesser/internal/pkg/mongoHelper"
"okieoth/schemaguesser/internal/pkg/schema"
Expand All @@ -22,13 +24,20 @@ var schemaCmd = &cobra.Command{
Short: "functions around the schemas",
Long: "With this command you can create schemas out of mongodb collection",
Run: func(cmd *cobra.Command, args []string) {
client, err := mongoHelper.Connect(mongoHelper.ConStr)
if err != nil {
msg := fmt.Sprintf("Failed to connect to db: %v", err)
panic(msg)
}
defer mongoHelper.CloseConnection(client)

if dbName == "all" {
printSchemasForAllDatabases()
printSchemasForAllDatabases(client)
} else {
if colName == "all" {
printSchemasForAllCollections(dbName)
printSchemasForAllCollections(client, dbName)
} else {
printSchemaForOneCollection(dbName, colName, false)
printSchemaForOneCollection(client, dbName, colName, false)
}
}

Expand All @@ -45,15 +54,15 @@ func init() {
schemaCmd.Flags().Int32Var(&itemCount, "item_count", 100, "Number of collection entries used to build the schema")
}

func printSchemaForOneCollection(dbName string, collName string, doRecover bool) {
func printSchemaForOneCollection(client *mongo.Client, dbName string, collName string, doRecover bool) {
defer func() {
if doRecover {
if r := recover(); r != nil {
fmt.Printf("Recovered while handling collection (db: %s, collection: %s): %v", dbName, collName, r)
}
}
}()
bsonRaw, err := mongoHelper.QueryCollection(mongoHelper.ConStr, dbName, collName, int(itemCount))
bsonRaw, err := mongoHelper.QueryCollection(client, dbName, collName, int(itemCount))
if err != nil {
msg := fmt.Sprintf("Error while reading data for collection (%s.%s): \n%v\n", dbName, collName, err)
panic(msg)
Expand All @@ -66,7 +75,7 @@ func printSchemaForOneCollection(dbName string, collName string, doRecover bool)
fmt.Printf("Error while processing bson for schema: %v", err)
}
}
if len(bsonRaw) > 0 {
if len(bsonRaw) > 0 {
schema.ReduceTypes(&mainType, &otherComplexTypes)
//schema.GuessDicts(&otherComplexTypes)
schema.PrintSchema(dbName, collName, &mainType, &otherComplexTypes, outputDir)
Expand All @@ -75,16 +84,28 @@ func printSchemaForOneCollection(dbName string, collName string, doRecover bool)
}
}

func printSchemasForAllCollections(dbName string) {
collections := mongoHelper.ReadCollectionsOrPanic(dbName)
func printSchemasForAllCollections(client *mongo.Client, dbName string) {
collections := mongoHelper.ReadCollectionsOrPanic(client, dbName)
var wg sync.WaitGroup
for _, coll := range *collections {
printSchemaForOneCollection(dbName, coll, true)
wg.Add(1)
go func() {
defer wg.Done()
printSchemaForOneCollection(client, dbName, coll, true)
}()
}
wg.Wait()
}

func printSchemasForAllDatabases() {
dbs := mongoHelper.ReadDatabasesOrPanic()
func printSchemasForAllDatabases(client *mongo.Client) {
dbs := mongoHelper.ReadDatabasesOrPanic(client)
var wg sync.WaitGroup
for _, db := range *dbs {
printSchemasForAllCollections(db)
wg.Add(1)
go func() {
defer wg.Done()
printSchemasForAllCollections(client, db)
}()
}
wg.Wait()
}
18 changes: 18 additions & 0 deletions cmd/schemaguesser/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

const Version = "2.0.0"

var versionCmd = &cobra.Command{
Use: "version",
Short: "Shows the version of the program",
Long: "Shows the version of the program",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(Version)
},
}
77 changes: 17 additions & 60 deletions internal/pkg/mongoHelper/mongoHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ func Connect(conStr string) (*mongo.Client, error) {
return mongo.Connect(context.Background(), options.Client().ApplyURI(conStr2Use))
}

func CloseConnection(client *mongo.Client) {
if client == nil {
return
}
if err := client.Disconnect(context.Background()); err != nil {
println("Error while disconnect: %v", err)
}
}

func Dummy() {
fmt.Println("'Dummy' is called")
}

func ListDatabases(conStr string) ([]string, error) {
func ListDatabases(client *mongo.Client) ([]string, error) {
var ret []string
client, err := Connect(conStr)
if err != nil {
return ret, err
}

defer func() {
if client == nil {
return
}
if err = client.Disconnect(context.Background()); err != nil {
println("Error while disconnect: %v", err)
}
}()

cursor, err := client.ListDatabases(context.Background(), bson.M{})
if err != nil {
Expand All @@ -52,21 +48,8 @@ func ListDatabases(conStr string) ([]string, error) {
return ret, nil
}

func ListCollections(conStr string, databaseName string) ([]string, error) {
func ListCollections(client *mongo.Client, databaseName string) ([]string, error) {
var ret []string
client, err := Connect(conStr)
if err != nil {
return ret, err
}

defer func() {
if client == nil {
return
}
if err = client.Disconnect(context.Background()); err != nil {
println("Error while disconnect: %v", err)
}
}()

db := client.Database(databaseName)
cursor, err := db.ListCollectionNames(context.Background(), bson.M{})
Expand All @@ -80,21 +63,8 @@ func ListCollections(conStr string, databaseName string) ([]string, error) {
return ret, nil
}

func ListIndexes(conStr string, databaseName string, collectionName string) ([]string, error) {
func ListIndexes(client *mongo.Client, databaseName string, collectionName string) ([]string, error) {
var ret []string
client, err := Connect(conStr)
if err != nil {
return ret, err
}

defer func() {
if client == nil {
return
}
if err = client.Disconnect(context.Background()); err != nil {
println("Error while disconnect: %v", err)
}
}()

db := client.Database(databaseName)
collection := db.Collection(collectionName)
Expand All @@ -113,21 +83,8 @@ func ListIndexes(conStr string, databaseName string, collectionName string) ([]s
return ret, nil
}

func QueryCollection(conStr string, databaseName string, collectionName string, itemCount int) ([]bson.Raw, error) {
func QueryCollection(client *mongo.Client, databaseName string, collectionName string, itemCount int) ([]bson.Raw, error) {
var ret []bson.Raw
client, err := Connect(conStr)
if err != nil {
return ret, err
}

defer func() {
if client == nil {
return
}
if err = client.Disconnect(context.Background()); err != nil {
println("Error while disconnect: %v", err)
}
}()

db := client.Database(databaseName)
collection := db.Collection(collectionName)
Expand All @@ -147,17 +104,17 @@ func QueryCollection(conStr string, databaseName string, collectionName string,
return ret, nil
}

func ReadCollectionsOrPanic(dbName string) *[]string {
collections, err := ListCollections(ConStr, dbName)
func ReadCollectionsOrPanic(client *mongo.Client, dbName string) *[]string {
collections, err := ListCollections(client, dbName)
if err != nil {
msg := fmt.Sprintf("Error while reading collections for database (%s): \n%v\n", dbName, err)
panic(msg)
}
return &collections
}

func ReadDatabasesOrPanic() *[]string {
dbs, err := ListDatabases(ConStr)
func ReadDatabasesOrPanic(client *mongo.Client) *[]string {
dbs, err := ListDatabases(client)
if err != nil {
msg := fmt.Sprintf("Error while reading existing databases: \n%v\n", err)
panic(msg)
Expand Down
9 changes: 8 additions & 1 deletion internal/pkg/mongoHelper/mongoHelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import (
var conStr = "mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:{MONGO_PORT}/admin"

func TestListDbs_IT(t *testing.T) {
dbs, err := ListDatabases(conStr)
client, err := Connect(conStr)
defer CloseConnection(client)

if err != nil {
t.Errorf("Failed to get client: %v", err)
return
}
dbs, err := ListDatabases(client)

if err != nil {
t.Errorf("Failed to list databases: %v", err)
Expand Down
1 change: 0 additions & 1 deletion version.txt

This file was deleted.

0 comments on commit b2a21b4

Please sign in to comment.