github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/master/config/config.go (about) 1 // Copyright 2015 unirita Inc. 2 // Created 2015/04/10 honda 3 4 package config 5 6 import ( 7 "fmt" 8 "io" 9 "os" 10 "strings" 11 12 "github.com/BurntSushi/toml" 13 14 "github.com/unirita/cuto/util" 15 ) 16 17 type config struct { 18 Job jobSection 19 Dir dirSection 20 DB dbSection 21 Log logSection 22 } 23 24 // 設定ファイルのjobセクション 25 type jobSection struct { 26 DefaultNode string `toml:"default_node"` 27 DefaultPort int `toml:"default_port"` 28 DefaultTimeoutMin int `toml:"default_timeout_min"` 29 ConnectionTimeoutSec int `toml:"connection_timeout_sec"` 30 TimeTrackingSpanMin int `toml:"time_tracking_span_min"` 31 AttemptLimit int `toml:"attempt_limit"` 32 } 33 34 // 設定ファイルのdirセクション 35 type dirSection struct { 36 JobnetDir string `toml:"jobnet_dir"` 37 LogDir string `toml:"log_dir"` 38 } 39 40 // 設定ファイルのdbセクション 41 type dbSection struct { 42 DBFile string `toml:"db_file"` 43 } 44 45 // 設定ファイルのlogセクション 46 type logSection struct { 47 OutputLevel string `toml:"output_level"` 48 MaxSizeKB int `toml:"max_size_kb"` 49 MaxGeneration int `toml:"max_generation"` 50 TimeoutSec int `toml:"timeout_sec"` 51 } 52 53 const tag_CUTOROOT = "<CUTOROOT>" 54 55 var Dir = new(dirSection) 56 var Job = new(jobSection) 57 var DB = new(dbSection) 58 var Log = new(logSection) 59 60 // 設定ファイルをロードする。 61 // 62 // 引数: filePath ロードする設定ファイルのパス 63 // 64 // 戻り値: エラー情報 65 func Load(filePath string) error { 66 f, err := os.Open(filePath) 67 if err != nil { 68 return err 69 } 70 return loadReader(f) 71 } 72 73 func loadReader(reader io.Reader) error { 74 c := new(config) 75 c.Job.AttemptLimit = 1 76 if _, err := toml.DecodeReader(reader, c); err != nil { 77 return err 78 } 79 80 replaceCutoroot(c) 81 82 Dir = &c.Dir 83 Job = &c.Job 84 DB = &c.DB 85 Log = &c.Log 86 return nil 87 } 88 89 func replaceCutoroot(c *config) { 90 c.Dir.JobnetDir = strings.Replace(c.Dir.JobnetDir, tag_CUTOROOT, util.GetRootPath(), -1) 91 c.Dir.LogDir = strings.Replace(c.Dir.LogDir, tag_CUTOROOT, util.GetRootPath(), -1) 92 c.DB.DBFile = strings.Replace(c.DB.DBFile, tag_CUTOROOT, util.GetRootPath(), -1) 93 } 94 95 // 設定値のエラー検出を行う。 96 // 97 // return : エラー情報 98 func DetectError() error { 99 if Job.DefaultPort < 0 || 65535 < Job.DefaultPort { 100 return fmt.Errorf("job.default_port(%d) must be within the range 0 and 65535.", Job.DefaultPort) 101 } 102 if Job.DefaultTimeoutMin < 0 { 103 return fmt.Errorf("job.default_timeout_min(%d) must not be minus value.", Job.DefaultTimeoutMin) 104 } 105 if Job.ConnectionTimeoutSec <= 0 { 106 return fmt.Errorf("job.connection_timeout_sec(%d) must not be 0 or less.", Job.ConnectionTimeoutSec) 107 } 108 if Job.TimeTrackingSpanMin < 0 { 109 return fmt.Errorf("job.time_tracking_span_min(%d) must not be minus value.", Job.TimeTrackingSpanMin) 110 } 111 if Job.AttemptLimit <= 0 { 112 return fmt.Errorf("job.attempt_limit(%d) must not be 0 or less.", Job.AttemptLimit) 113 } 114 if Log.MaxSizeKB <= 0 { 115 return fmt.Errorf("log.max_size_kb(%d) must not be 0 or less.", Log.MaxSizeKB) 116 } 117 if Log.MaxGeneration <= 0 { 118 return fmt.Errorf("log.max_generation(%d) must not be 0 or less.", Log.MaxGeneration) 119 } 120 121 return nil 122 }