cerca

lean forum software (pmc local branch)
git clone http://git.permacomputing.net/repos/cerca.git # read-only access
Log | Files | Refs | README | LICENSE

main.go (1784B)


      1 package main
      2 
      3 import (
      4 	"cerca/database"
      5 	"flag"
      6 	"fmt"
      7 	"os"
      8 )
      9 
     10 func inform(msg string, args ...interface{}) {
     11 	if len(args) > 0 {
     12 		fmt.Printf("%s\n", fmt.Sprintf(msg, args...))
     13 	} else {
     14 		fmt.Printf("%s\n", msg)
     15 	}
     16 }
     17 
     18 func complain(msg string, args ...interface{}) {
     19 	if len(args) > 0 {
     20 		inform(msg, args)
     21 	} else {
     22 		inform(msg)
     23 	}
     24 	os.Exit(0)
     25 }
     26 
     27 func main() {
     28 	migrations := map[string]func(string) error{
     29 		"2024-01-password-hash-migration":  database.Migration20240116_PwhashChange,
     30 		"2024-02-thread-private-migration": database.Migration20240720_ThreadPrivateChange,
     31 	}
     32 
     33 	var dbPath, migration string
     34 	var listMigrations bool
     35 	flag.BoolVar(&listMigrations, "list", false, "list possible migrations")
     36 	flag.StringVar(&migration, "migration", "", "name of the migration you want to perform on the database")
     37 	flag.StringVar(&dbPath, "database", "./data/forum.db", "full path to the forum database; e.g. ./data/forum.db")
     38 	flag.Parse()
     39 
     40 	usage := `usage
     41 	migration-tool --list 
     42 	migration-tool --migration <name of migration>
     43   `
     44 
     45 	if listMigrations {
     46 		inform("Possible migrations:")
     47 		for key := range migrations {
     48 			fmt.Println("\t", key)
     49 		}
     50 		os.Exit(0)
     51 	}
     52 
     53 	if migration == "" {
     54 		complain(usage)
     55 	} else if _, ok := migrations[migration]; !ok {
     56 		complain(fmt.Sprintf("chosen migration »%s» does not match one of the available migrations. see migrations with flag --list", migration))
     57 	}
     58 
     59 	// check if database exists! we dont wanna create a new db in this case ':)
     60 	if !database.CheckExists(dbPath) {
     61 		complain("couldn't find database at %s", dbPath)
     62 	}
     63 
     64 	// perform migration
     65 	err := migrations[migration](dbPath)
     66 	if err == nil {
     67 		inform(fmt.Sprintf("Migration »%s» completed", migration))
     68 	} else {
     69 		complain("migration terminated early due to error")
     70 	}
     71 }