main.go (3014B)
1 // This file is part of taldir, the Taler Directory implementation. 2 // Copyright (C) 2025 Martin Schanzenbach 3 // 4 // Taldir 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 // Taldir 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 "strings" 29 30 _ "github.com/lib/pq" 31 talerutil "github.com/schanzen/taler-go/pkg/util" 32 "rsc.io/getopt" 33 34 "gopkg.in/ini.v1" 35 ) 36 37 var ( 38 taldirdatahome string 39 taldirconfdir string 40 ) 41 42 func printHelp() { 43 fmt.Print("taler-directory-dbinit\n\n") 44 getopt.PrintDefaults() 45 fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n", 46 "Home page: https://taler.net\n", 47 "General help using GNU software: http://www.gnu.org/gethelp/\n") 48 } 49 50 func printKey(key *ini.Key, onlyValue bool) { 51 if onlyValue { 52 fmt.Printf("%s\n", key.String()) 53 return 54 } 55 fmt.Printf("%s = %s\n", key.Name(), key.String()) 56 } 57 58 func printCfgOptions(sec *ini.Section, option *string, onlyValue *bool) { 59 if len(*option) == 0 { 60 for _, key := range sec.Keys() { 61 printKey(key, *onlyValue) 62 } 63 return 64 } 65 if !sec.HasKey(*option) { 66 fmt.Printf("Section `%s' does not have option `%s'!\n", sec.Name(), *option) 67 os.Exit(1) 68 } 69 key := sec.Key(*option) 70 printKey(key, *onlyValue) 71 } 72 73 func printCfgSections(f *ini.File) { 74 for _, sec := range f.Sections() { 75 fmt.Println(sec.Name()) 76 } 77 } 78 79 func main() { 80 var cfg *ini.File 81 var err error 82 var cfgFlag = flag.String("c", "", "Configuration file to use") 83 getopt.Alias("c", "config") 84 var helpFlag = flag.Bool("h", false, "Print help") 85 getopt.Alias("h", "help") 86 87 getopt.Parse() 88 if *helpFlag { 89 printHelp() 90 return 91 } 92 cfgfile := path.Join(taldirconfdir, "taldir.conf") 93 if len(*cfgFlag) != 0 { 94 cfg, err = ini.Load(*cfgFlag) 95 if err != nil { 96 fmt.Printf("Failed to read config: %v\n", err) 97 os.Exit(1) 98 } 99 } else { 100 // FIXME also try in datahome 101 cfg, err = ini.LooseLoad(cfgfile) 102 if err != nil { 103 fmt.Printf("Failed to read config: %v\n", err) 104 os.Exit(1) 105 } 106 } 107 psqlconn := cfg.Section("taldir-pq").Key("connection_string").MustString("postgres:///taler-directory") 108 segments := strings.Split(strings.Split(psqlconn, "?")[0], "/") 109 dbName := segments[len(segments)-1] 110 111 db, err := sql.Open("postgres", psqlconn) 112 if err != nil { 113 log.Panic(err) 114 } 115 defer db.Close() 116 err = talerutil.DBInit(db, taldirdatahome, dbName, "taler-directory") 117 if err != nil { 118 log.Fatalf("%v\n", err) 119 } 120 }