package main import ( "fmt" "log" "os" "path/filepath" "regexp" "strings" "time" "code.osinet.fr/fgm/kurz/web/i18n" "github.com/spf13/cobra" "github.com/spf13/viper" "golang.org/x/text/language" ) var cmd = &cobra.Command{ Use: "kurzd", Short: "kurzd is the Kurz daemon and back-office command", Long: `kurzd is the actual engine for Kurz. Use it as the server for your Kurz clients. Configure it by copying dist.config.yml to ~/.kurz/config.yml and editing the copy.`, } var verbose bool func initConfig() { viper.SetConfigName("config") viper.AddConfigPath(".") viper.AddConfigPath("$HOME/.kurz") err := viper.ReadInConfig() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(2) } } func init() { cobra.OnInitialize(initConfig) verbose = *cmd.PersistentFlags().BoolP("verbose", "v", false, "Add -v for verbose operation") setupI18n() } func setupI18n() { cwd, err := os.Getwd() if err != nil { panic(err) } t0 := time.Now() log.Printf("Finding message catalogs below %s\n", cwd) tags := []string{language.English.String(), language.French.String()} // Build the regex matching message file names. re := regexp.MustCompile("messages\\.(?:" + strings.Join(tags, "|") + ")\\.yaml") scanned := 0 catalogCount := 0 walkErr := filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error { scanned++ if err != nil { panic(err) } if info.IsDir() { return nil } if !re.MatchString(info.Name()) { return nil } _, err = i18n.Bundle.LoadMessageFile(path) catalogCount++ return err }) if walkErr != nil { panic(walkErr) } t1 := time.Now() diff := t1.Sub(t0) log.Printf("%d items scanned, %d catalogs loaded in %d msec\n", scanned, catalogCount, diff/time.Millisecond) }