main.go (2703B)
1 // This file is part of taler-mailbox, the Taler Directory implementation. 2 // Copyright (C) 2025 Martin Schanzenbach 3 // 4 // taler-mailbox is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Affero General Public License as published 6 // by the Free Software Foundation, either version 3 of the License, 7 // or (at your option) any later version. 8 // 9 // taler-mailbox is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 // 17 // SPDX-License-Identifier: AGPL3.0-or-later 18 19 package main 20 21 import ( 22 "database/sql" 23 "flag" 24 "fmt" 25 "log" 26 "os" 27 "path" 28 "path/filepath" 29 "strings" 30 31 _ "github.com/lib/pq" 32 talerutil "github.com/schanzen/taler-go/pkg/util" 33 "rsc.io/getopt" 34 35 "gopkg.in/ini.v1" 36 ) 37 38 var ( 39 mailboxdatahome string 40 mailboxconfdir string 41 version string 42 ) 43 44 func printHelp() { 45 fmt.Print("taler-mailbox-dbinit\n\n") 46 getopt.PrintDefaults() 47 fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n", 48 "Home page: https://taler.net\n", 49 "General help using GNU software: http://www.gnu.org/gethelp/\n") 50 } 51 52 func main() { 53 var cfg *ini.File 54 var err error 55 var cfgFlag = flag.String("c", "", "Configuration file to use") 56 getopt.Alias("c", "config") 57 var helpFlag = flag.Bool("h", false, "Print help") 58 getopt.Alias("h", "help") 59 var versionFlag = flag.Bool("v", false, "Print version") 60 getopt.Alias("v", "version") 61 62 getopt.Parse() 63 if *helpFlag { 64 printHelp() 65 return 66 } 67 if *versionFlag { 68 fullName, err := os.Executable() 69 if err != nil { 70 log.Panic(err) 71 } 72 fmt.Printf("%s %s", filepath.Base(fullName), version) 73 return 74 } 75 cfgfile := path.Join(mailboxconfdir, "mailbox.conf") 76 if len(*cfgFlag) != 0 { 77 cfg, err = ini.Load(*cfgFlag) 78 if err != nil { 79 fmt.Printf("Failed to read config: %v\n", err) 80 os.Exit(1) 81 } 82 } else { 83 // FIXME also try in datahome 84 cfg, err = ini.LooseLoad(cfgfile) 85 if err != nil { 86 fmt.Printf("Failed to read config: %v\n", err) 87 os.Exit(1) 88 } 89 } 90 psqlconn := cfg.Section("mailbox-pq").Key("connection_string").MustString("postgres:///taler-mailbox") 91 segments := strings.Split(strings.Split(psqlconn, "?")[0], "/") 92 dbName := segments[len(segments)-1] 93 94 db, err := sql.Open("postgres", psqlconn) 95 if err != nil { 96 log.Panic(err) 97 } 98 defer db.Close() 99 err = talerutil.DBInit(db, mailboxdatahome, dbName, "taler-mailbox") 100 if err != nil { 101 log.Fatalf("%v\n", err) 102 } 103 }