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