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 (2028B)


      1 package main
      2 
      3 import (
      4 	"cerca/constants"
      5 	"cerca/database"
      6 	"flag"
      7 	"fmt"
      8 	"os"
      9 )
     10 
     11 func inform(msg string, args ...interface{}) {
     12 	if len(args) > 0 {
     13 		fmt.Printf("%s\n", fmt.Sprintf(msg, args...))
     14 	} else {
     15 		fmt.Printf("%s\n", msg)
     16 	}
     17 }
     18 
     19 func complain(msg string, args ...interface{}) {
     20 	if len(args) > 0 {
     21 		inform(msg, args)
     22 	} else {
     23 		inform(msg)
     24 	}
     25 	os.Exit(0)
     26 }
     27 
     28 func main() {
     29 	var username string
     30 	var forumDomain string
     31 	var dbPath string
     32 	flag.StringVar(&forumDomain, "url", "https://forum.merveilles.town", "root url to forum, referenced in output")
     33 	flag.StringVar(&username, "username", "", "username who should be made admin")
     34 	flag.StringVar(&dbPath, "database", "./data/forum.db", "full path to the forum database; e.g. ./data/forum.db")
     35 	flag.Parse()
     36 
     37 	usage := `usage
     38 	add-admin --username <username to make admin> --url <rool url to forum> --database ./data/forum.db
     39 	add-admin --help for more information
     40   `
     41 
     42 	adminRoute := fmt.Sprintf("%s/admin", forumDomain)
     43 
     44 	if username == "" {
     45 		complain(usage)
     46 	}
     47 
     48 	// check if database exists! we dont wanna create a new db in this case ':)
     49 	if !database.CheckExists(dbPath) {
     50 		complain("couldn't find database at %s", dbPath)
     51 	}
     52 
     53 	db := database.InitDB(dbPath)
     54 
     55 	userid, err := db.GetUserID(username)
     56 	if err != nil {
     57 		complain("username %s not in database", username)
     58 	}
     59 	inform("Attempting to make %s (id %d) admin...", username, userid)
     60 	err = db.AddAdmin(userid)
     61 	if err != nil {
     62 		complain("Something went wrong: %s", err)
     63 	}
     64 
     65 	// log cmd actions just as admin web-actions are logged
     66 	systemUserid := db.GetSystemUserid()
     67 	err = db.AddModerationLog(systemUserid, userid, constants.MODLOG_ADMIN_MAKE)
     68 	if err != nil {
     69 		complain("adding mod log for adding new admin failed (%w)", err)
     70 	}
     71 
     72 	inform("Successfully added %s (id %d) as an admin", username, userid)
     73 	inform("Please visit %s for all your administration needs (changing usernames, resetting passwords, deleting user accounts)", adminRoute)
     74 	inform("Admin action has been logged to /moderations")
     75 }