func main() { var ( mongo = flag.String("mongo", "localhost", "Address of mongo instance") ) db, err := models.MongoDB(*mongo) if err != nil { log.Fatal(err) } iter, err := db.Query(models.TaskKind).Execute() if err != nil { log.Fatal(err) } count := 0 t := models.NewTask() for iter.Next(t) { if task.IsComplete(t) { t.CompletedAt = t.UpdatedAt if err := db.Save(t); err != nil { log.Fatal(err) } count++ } } log.Print("Migrated %d tasks", count) if err := iter.Close(); err != nil { log.Fatal(err) } }
func main() { db, err := models.MongoDB("localhost") if err != nil { log.Fatal(err) } db.Query(models.UserKind) }
func (c *InitCommand) Run(args []string) int { if c.Config.DB == "" { c.Ui.Error("No database listed") return 1 } c.Ui.Info("Connecting to db...") db, err := models.MongoDB(c.Config.DB) if err != nil { c.Ui.Error(err.Error()) return 1 } c.Ui.Info("Connected") c.Ui.Output("Welcome to Elos!") c.Ui.Output("We first need to create you an elos user account") pass, err := c.Ui.Ask("What would you like your master password to be?") if err != nil { return 1 } newUser := models.NewUser() newUser.SetID(db.NewID()) newUser.CreatedAt = time.Now() newUser.Password = pass newUser.UpdatedAt = time.Now() if err = db.Save(newUser); err != nil { c.Ui.Error(fmt.Sprintf("Failure to save user: %s", err)) return 1 } c.Config.UserID = newUser.ID().String() err = WriteConfigFile(c.Config) if err != nil { c.Ui.Error("Failed to update user id info") return 1 } c.Ui.Info(fmt.Sprintf("User account created, your id is: %s", newUser.ID())) return 0 }
func init() { UI = &cli.BasicUi{Writer: os.Stdout, Reader: os.Stdin} user, err := user.Current() if err != nil { UI.Error(err.Error()) os.Exit(1) } configPath := path.Join(user.HomeDir, command.ConfigFileName) c, err := command.ParseConfigFile(configPath) if err != nil { UI.Error(err.Error()) os.Exit(1) } Configuration = c var db olddata.DB var databaseError error if Configuration.DirectDB { if Configuration.DB != "" { db, databaseError = models.MongoDB(Configuration.DB) } else { databaseError = fmt.Errorf("No database listed") } } else { db = &gaia.DB{ URL: Configuration.Host, Username: Configuration.PublicCredential, Password: Configuration.PrivateCredential, Client: new(http.Client), } } conn, err := grpc.Dial( "elos.pw:4444", grpc.WithPerRPCCredentials( auth.RawCredentials( Configuration.Credential.Public, Configuration.Credential.Private, ), ), grpc.WithInsecure(), ) if err != nil { log.Fatal(err) } // don't close connection because we'd lose connection should // move declaration of dbc higher in scope TODO(nclandolfi) dbc := data.NewDBClient(conn) Commands = map[string]cli.CommandFactory{ "habit": func() (cli.Command, error) { return &command.HabitCommand{ UI: UI, UserID: Configuration.UserID, DB: db, }, databaseError }, "people": func() (cli.Command, error) { return &command.PeopleCommand{ UI: UI, UserID: Configuration.UserID, DB: db, }, databaseError }, "setup": func() (cli.Command, error) { return &command.SetupCommand{ UI: UI, Config: Configuration, }, nil }, "tag": func() (cli.Command, error) { return &command.TagCommand{ UI: UI, UserID: Configuration.UserID, DB: db, }, databaseError }, "stream": func() (cli.Command, error) { return &command.StreamCommand{ UI: UI, UserID: Configuration.UserID, DB: db, }, databaseError }, "todo": func() (cli.Command, error) { return &command.TodoCommand{ UI: UI, UserID: Configuration.Credential.OwnerID, DB: data.DB(dbc), }, databaseError }, "cal2": func() (cli.Command, error) { return &command.Cal2Command{ UI: UI, UserID: Configuration.Credential.OwnerID, DBClient: dbc, }, nil }, "records": func() (cli.Command, error) { return &command.RecordsCommand{ UI: UI, UserID: Configuration.Credential.OwnerID, DBClient: dbc, }, nil }, } }
func main() { flag.Parse() var db data.DB var err error log.Printf("== Setting Up Database ==") log.Printf("\tDatabase Type: %s", *dbtype) switch *dbtype { case "mem": db = mem.NewDB() case "mongo": db, err = models.MongoDB(*dbaddr) if err != nil { log.Fatal(err) } log.Printf("\tConnected to mongo@%s", *dbaddr) default: log.Fatal("Unrecognized database type: '%s'", *dbtype) } log.Printf("== Set up Database ==") // DB CLIENT conn, err := grpc.Dial(":3334", grpc.WithInsecure()) if err != nil { log.Fatalf("failed to dial: %v", err) } defer conn.Close() adbc := access.NewDBClient(conn) // AUTH CLIENT conn, err = grpc.Dial(":3333", grpc.WithInsecure()) if err != nil { log.Fatalf("failed to dial: %v", err) } defer conn.Close() ac := auth.NewAuthClient(conn) // WEBUI SERVER lis, err := net.Listen("tcp", ":1113") if err != nil { log.Fatalf("failed to listen on :1113: %v", err) } g := grpc.NewServer() records.RegisterWebUIServer(g, records.Logged(records.NewWebUI(adbc, ac))) go g.Serve(lis) // WEB UI CLIENT conn, err = grpc.Dial(":1113", grpc.WithInsecure()) if err != nil { log.Fatalf("failed to dial: %v", err) } defer conn.Close() webuiclient := records.NewWebUIClient(conn) // calendar WEBUI SERVER lis, err = net.Listen("tcp", ":1114") if err != nil { log.Fatalf("failed to listen on :1114: %v", err) } g = grpc.NewServer() cal.RegisterWebUIServer(g, cal.NewWebUI(adbc, ac)) go g.Serve(lis) // cal WEB UI CLIENT conn, err = grpc.Dial(":1114", grpc.WithInsecure()) if err != nil { log.Fatalf("failed to dial: %v", err) } defer conn.Close() calwebui := cal.NewWebUIClient(conn) if _, _, err := user.Create(db, "u", "p"); err != nil { log.Fatal("user.Create error: %s", err) } background := context.Background() log.Printf("== Connecting to Twilio ==") twilioClient := twilio.NewClient(TwilioAccountSid, TwilioAuthToken, nil) log.Printf("== Connected to Twilio ==") log.Printf("== Starting SMS Command Sessions ==") smsMux := services.NewSMSMux() go smsMux.Start( background, db, services.SMSFromTwilio(twilioClient, TwilioFromNumber), ) log.Printf("== Started SMS Command Sessions ==") log.Printf("== Initiliazing Gaia Core ==") ga := gaia.New( context.Background(), new(gaia.Middleware), &gaia.Services{ AppFileSystem: http.Dir(*appdir), SMSCommandSessions: smsMux, DB: db, Logger: services.NewLogger(os.Stderr), WebUIClient: webuiclient, CalWebUIClient: calwebui, }, ) log.Printf("== Initiliazed Gaia Core ==") log.Printf("== Starting Agents ===") user.Map(db, func(db data.DB, u *models.User) error { go agents.LocationAgent(background, db, u) go agents.TaskAgent(background, db, u) go agents.WebSensorsAgent(background, db, u) return nil }) log.Printf("== Started Agents ===") log.Printf("== Starting HTTP Server ==") host := fmt.Sprintf("%s:%d", *addr, *port) log.Printf("\tServing on %s", host) if *certFile != "" && *keyFile != "" { if *port != 443 { log.Print("WARNING: serving HTTPS on a port that isn't 443") } if err = http.ListenAndServeTLS(host, *certFile, *keyFile, ga); err != nil { log.Fatal(err) } } else { log.Print("NOT SERVING SECURELY") if *port != 80 { log.Print("WARNING: serving HTTP on a port that isn't 80") } if err = http.ListenAndServe(host, ga); err != nil { log.Fatal(err) } } log.Printf("== Started HTTP Server ==") }