main.go (3387B)
1 // This file is part of mailbox, the Taler mailbox implementation. 2 // Copyright (C) 2025 Martin Schanzenbach 3 // 4 // 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 // 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 "flag" 23 "fmt" 24 "log" 25 "os" 26 "path" 27 "path/filepath" 28 29 "rsc.io/getopt" 30 31 "gopkg.in/ini.v1" 32 ) 33 34 var ( 35 version string 36 mailboxdatahome string 37 mailboxconfdir string 38 ) 39 40 func printHelp() { 41 fmt.Print("taler-mailbox-config\n\n") 42 getopt.PrintDefaults() 43 fmt.Print("\nReport bugs to gnunet-developers@gnu.org.\n", 44 "Home page: https://taler.net\n", 45 "General help using GNU software: http://www.gnu.org/gethelp/\n") 46 } 47 48 func printKey(key *ini.Key, onlyValue bool) { 49 if onlyValue { 50 fmt.Printf("%s\n", key.String()) 51 return 52 } 53 fmt.Printf("%s = %s\n", key.Name(), key.String()) 54 } 55 56 func printCfgOptions(sec *ini.Section, option *string, onlyValue *bool) { 57 if len(*option) == 0 { 58 for _, key := range sec.Keys() { 59 printKey(key, *onlyValue) 60 } 61 return 62 } 63 if !sec.HasKey(*option) { 64 fmt.Printf("Section `%s' does not have option `%s'!\n", sec.Name(), *option) 65 os.Exit(1) 66 } 67 key := sec.Key(*option) 68 printKey(key, *onlyValue) 69 } 70 71 func printCfgSections(f *ini.File) { 72 for _, sec := range f.Sections() { 73 fmt.Println(sec.Name()) 74 } 75 } 76 77 func main() { 78 var cfg *ini.File 79 var err error 80 var sectionFlag = flag.String("s", "", "Section to use") 81 getopt.Alias("s", "section") 82 var listSectionFlag = flag.Bool("S", false, "List all sections") 83 getopt.Alias("S", "list-sections") 84 var optionFlag = flag.String("o", "", "Option to output") 85 getopt.Alias("o", "option") 86 var onlyValueFlag = flag.Bool("O", false, "Output only value") 87 getopt.Alias("O", "only-value") 88 var cfgFlag = flag.String("c", "", "Configuration file to use") 89 getopt.Alias("c", "config") 90 var helpFlag = flag.Bool("h", false, "Print help") 91 getopt.Alias("h", "help") 92 var versionFlag = flag.Bool("v", false, "Print version") 93 getopt.Alias("v", "version") 94 95 getopt.Parse() 96 if *helpFlag { 97 printHelp() 98 return 99 } 100 if *versionFlag { 101 fullName, err := os.Executable() 102 if err != nil { 103 log.Panic(err) 104 } 105 fmt.Printf("%s %s", filepath.Base(fullName), version) 106 return 107 } 108 cfgfile := path.Join(mailboxconfdir, "taler-mailbox.conf") 109 if len(*cfgFlag) != 0 { 110 cfg, err = ini.Load(*cfgFlag) 111 if err != nil { 112 fmt.Printf("Failed to read config: %v\n", err) 113 os.Exit(1) 114 } 115 } else { 116 // FIXME also try in datahome 117 cfg, err = ini.LooseLoad(cfgfile) 118 if err != nil { 119 fmt.Printf("Failed to read config: %v\n", err) 120 os.Exit(1) 121 } 122 } 123 if *listSectionFlag { 124 printCfgSections(cfg) 125 return 126 } 127 if len(*sectionFlag) == 0 { 128 fmt.Println("No section given!") 129 os.Exit(1) 130 } 131 sec := cfg.Section(*sectionFlag) 132 printCfgOptions(sec, optionFlag, onlyValueFlag) 133 }