config.go - fingered - Fingerd protocol daemon, allowing custom responses.
 (HTM) git clone git://jay.scot/fingered
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       config.go (1302B)
       ---
            1 package config
            2 
            3 import (
            4         "flag"
            5         "fmt"
            6         "log"
            7 )
            8 
            9 const (
           10         maxPort    = 65535
           11         minPort    = 1
           12         minThreads = 1
           13 )
           14 
           15 type Config struct {
           16         Threads int
           17         Port    int
           18         Dir     string
           19         Index   string
           20 }
           21 
           22 func defaultConfig() Config {
           23         return Config{
           24                 Threads: 10,
           25                 Port:    79,
           26                 Dir:     "/srv/fingered",
           27                 Index:   "default",
           28         }
           29 }
           30 
           31 func displayUsage() {
           32         flagSet := flag.CommandLine
           33         flag.Usage = func() {
           34                 fmt.Printf("Usage:\n")
           35                 flagSet.VisitAll(func(flag *flag.Flag) {
           36                         fmt.Printf("\t-%s: %s (default: %s)\n", flag.Name, flag.Usage, flag.DefValue)
           37                 })
           38         }
           39 }
           40 
           41 func addFlags(cfg *Config) {
           42         flag.IntVar(&cfg.Threads, "t", cfg.Threads, "Number of worker threads")
           43         flag.IntVar(&cfg.Port, "p", cfg.Port, "Port for incoming connections")
           44         flag.StringVar(&cfg.Dir, "d", cfg.Dir, "Directory containing user files")
           45         flag.StringVar(&cfg.Index, "f", cfg.Index, "Filename for empty requests")
           46 }
           47 
           48 func ParseFlags() Config {
           49         cfg := defaultConfig()
           50         addFlags(&cfg)
           51         displayUsage()
           52         flag.Parse()
           53 
           54         if cfg.Threads < minThreads {
           55                 log.Fatal("Invalid number of threads: must be greater than 0.")
           56         }
           57 
           58         if cfg.Port < minPort || cfg.Port > maxPort {
           59                 log.Fatal("Invalid port value: must be between 1 and 65535.")
           60         }
           61 
           62         if cfg.Dir == "" {
           63                 log.Fatal("Invalid path value: directory cannot be empty.")
           64         }
           65 
           66         return cfg
           67 }