Skip to content

Instantly share code, notes, and snippets.

@Hasstrup
Last active December 29, 2018 21:32
Show Gist options
  • Save Hasstrup/d99634523e82cc663179d0485fa0189b to your computer and use it in GitHub Desktop.
Save Hasstrup/d99634523e82cc663179d0485fa0189b to your computer and use it in GitHub Desktop.
package main
import (
"context"
"flag"
"log"
"github.com/mongodb/mongo-go-driver/mongo"
)
var (
cmd = flag.String("cmd", "", "list or add?")
address = flag.String("address", "", "mongodb address to connect to")
database = flag.String("db", "", "The name of the database to connect to")
collection = flag.String("collection", "", "The collection (in the db) to connect to")
key = flag.String("field", "", "The field you'd like to place an index on")
unique = flag.Bool("unique", false, "Would you like the index to be unique?")
value = flag.Int("type", 1, "would you like the index to be ascending (1) or descending (-1)?")
)
func main() {
flag.Parse()
switch {
case (*cmd != "add" && *cmd != "list"):
log.Fatalf("The first argument has to be 'add' or 'list :)")
case *cmd == "add" && *key == "":
log.Fatalf("Please pass in the name of the field to place the index :)")
}
ConnectToTheMongoDB(*address)
}
func ConnectToTheMongoDB(address string) {
_, err := mongo.Connect(context.Background(), address)
if err != nil {
panic(err.Error())
}
log.Println("Successfully connected to the address provided")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment