taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

main.go (3308B)


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