runningwater 5 лет назад
Родитель
Сommit
8f19ccb68d
6 измененных файлов с 50 добавлено и 2 удалено
  1. 6 1
      READEME.md
  2. 23 0
      conf/conf.go
  3. 7 0
      conf/conf_test.go
  4. 10 0
      conf/config.yaml
  5. 4 1
      config.yaml
  6. BIN
      ini.xlsx

+ 6 - 1
READEME.md

@@ -19,4 +19,9 @@
   
   `go get -u github.com/jmoiron/sqlx v1.2.0`
   
-  `go get -u github.com/spf13/viper v1.7.1`
+  `go get -u github.com/spf13/viper v1.7.1`
+  
+  打包命令
+  
+  `CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build .`
+  `CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .`

+ 23 - 0
conf/conf.go

@@ -4,12 +4,14 @@ import (
     l4g "github.com/alecthomas/log4go"
     "github.com/spf13/viper"
     "os"
+    "path"
 )
 
 var cfg = Config{}
 
 type Config struct {
     Database Database `mapstructure:"database"` // 数据库配置
+    Report   Report   `mapstructure:"report"`   // 报表
 }
 
 type Database struct {
@@ -21,6 +23,11 @@ type Database struct {
     Charset string `mapstructure:"charset"`
 }
 
+type Report struct {
+    Template string `mapstructure:"template"`
+    Out      string `mapstructure:"out"`
+}
+
 func LoadConfig(logger *l4g.Logger) {
     path, err := os.Getwd()
     if err != nil {
@@ -44,5 +51,21 @@ func LoadConfig(logger *l4g.Logger) {
 
 // GetInfo 获取配置文件
 func GetInfo() Config {
+    templateFile := cfg.Report.Template
+    if !isExist(templateFile) {
+        cfg.Report.Template = path.Base(templateFile)
+    }
     return cfg
 }
+
+// 文件是否存在
+func isExist(fileAddr string) bool {
+    _, err := os.Stat(fileAddr)
+    if err != nil {
+        if os.IsExist(err) {
+            return true
+        }
+        return false
+    }
+    return true
+}

+ 7 - 0
conf/conf_test.go

@@ -2,12 +2,19 @@ package conf
 
 import (
     "fmt"
+    l4g "github.com/alecthomas/log4go"
     "testing"
 )
 
 func TestGetInfo(t *testing.T) {
+    log := &l4g.Logger{}
+    defer log.Close()
+
+    LoadConfig(log)
     fmt.Println("name: ", GetInfo().Database.Dbname)
     fmt.Println("host: ", GetInfo().Database.Host)
     fmt.Println("user: ", GetInfo().Database.User)
     fmt.Println("pwd: ", GetInfo().Database.Pwd)
+    fmt.Println("template: ", GetInfo().Report.Template)
+    fmt.Println("out: ", GetInfo().Report.Out)
 }

+ 10 - 0
conf/config.yaml

@@ -0,0 +1,10 @@
+database:
+  host: 47.110.138.172
+  user: root
+  dbname: newDB
+  pwd: jEER3Dc=
+  port: 3306
+  charset: utf8
+report:
+  template: ini.xlsx  # 模板名
+  out: report.xls

+ 4 - 1
config.yaml

@@ -4,4 +4,7 @@ database:
   dbname: newDB
   pwd: jEER3Dc=
   port: 3306
-  charset: utf8
+  charset: utf8
+report:
+  template: ini.xlsx  # 模板名
+  out: report.xls