main.go (3304B)
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 "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 version string 40 taldirdatahome string 41 taldirconfdir string 42 ) 43 44 func printHelp() { 45 fmt.Print("taler-directory-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 printKey(key *ini.Key, onlyValue bool) { 53 if onlyValue { 54 fmt.Printf("%s\n", key.String()) 55 return 56 } 57 fmt.Printf("%s = %s\n", key.Name(), key.String()) 58 } 59 60 func printCfgOptions(sec *ini.Section, option *string, onlyValue *bool) { 61 if len(*option) == 0 { 62 for _, key := range sec.Keys() { 63 printKey(key, *onlyValue) 64 } 65 return 66 } 67 if !sec.HasKey(*option) { 68 fmt.Printf("Section `%s' does not have option `%s'!\n", sec.Name(), *option) 69 os.Exit(1) 70 } 71 key := sec.Key(*option) 72 printKey(key, *onlyValue) 73 } 74 75 func printCfgSections(f *ini.File) { 76 for _, sec := range f.Sections() { 77 fmt.Println(sec.Name()) 78 } 79 } 80 81 func main() { 82 var cfg *ini.File 83 var err error 84 var cfgFlag = flag.String("c", "", "Configuration file to use") 85 getopt.Alias("c", "config") 86 var helpFlag = flag.Bool("h", false, "Print help") 87 getopt.Alias("h", "help") 88 var versionFlag = flag.Bool("v", false, "Print version") 89 getopt.Alias("v", "version") 90 91 getopt.Parse() 92 if *helpFlag { 93 printHelp() 94 return 95 } 96 if *versionFlag { 97 fullName, err := os.Executable() 98 if err != nil { 99 log.Panic(err) 100 } 101 fmt.Printf("%s %s", filepath.Base(fullName), version) 102 return 103 } 104 cfgfile := path.Join(taldirconfdir, "taldir.conf") 105 if len(*cfgFlag) != 0 { 106 cfg, err = ini.Load(*cfgFlag) 107 if err != nil { 108 fmt.Printf("Failed to read config: %v\n", err) 109 os.Exit(1) 110 } 111 } else { 112 // FIXME also try in datahome 113 cfg, err = ini.LooseLoad(cfgfile) 114 if err != nil { 115 fmt.Printf("Failed to read config: %v\n", err) 116 os.Exit(1) 117 } 118 } 119 psqlconn := cfg.Section("taldir-pq").Key("connection_string").MustString("postgres:///taler-directory") 120 segments := strings.Split(strings.Split(psqlconn, "?")[0], "/") 121 dbName := segments[len(segments)-1] 122 123 db, err := sql.Open("postgres", psqlconn) 124 if err != nil { 125 log.Panic(err) 126 } 127 defer db.Close() 128 err = talerutil.DBInit(db, taldirdatahome, dbName, "taler-directory") 129 if err != nil { 130 log.Fatalf("%v\n", err) 131 } 132 }