import_config.go 1.8 KB
Newer Older
X
Xin.Zh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

X
xieyinglin 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package dataimport

import (
	"encoding/json"
	"fmt"
	"path/filepath"
	"sync"

	"github.com/pelletier/go-toml"
)

var (
	cfg  Config
	once sync.Once
)

张金富 已提交
32
// Config include all scene import config
X
xieyinglin 已提交
33 34 35 36 37 38
type Config struct {
	UserCases map[string]CaseConfig
}

// CaseConfig include the sample data config and tdengine config
type CaseConfig struct {
张金富 已提交
39 40 41 42 43 44 45 46 47 48
	Format              string
	FilePath            string
	Separator           string
	StName              string
	SubTableName        string
	Timestamp           string
	TimestampType       string
	TimestampTypeFormat string
	Tags                []FieldInfo
	Fields              []FieldInfo
X
xieyinglin 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

// FieldInfo is field or tag info
type FieldInfo struct {
	Name string
	Type string
}

// LoadConfig will load the specified file config
func LoadConfig(filePath string) Config {
	once.Do(func() {
		filePath, err := filepath.Abs(filePath)
		if err != nil {
			panic(err)
		}
		fmt.Printf("parse toml file once. filePath: %s\n", filePath)
		tree, err := toml.LoadFile(filePath)
		if err != nil {
			panic(err)
		}

		bytes, err := json.Marshal(tree.ToMap())
		if err != nil {
			panic(err)
		}

		err = json.Unmarshal(bytes, &cfg.UserCases)
		if err != nil {
			panic(err)
		}
	})
	return cfg
}