@@ -5,31 +5,45 @@ import (
55 "gopkg.in/yaml.v3"
66 "os"
77 "path/filepath"
8- "strings"
98 "sync"
109)
1110
12- // ShowKConfig 获取k.yaml配置
13- var ShowKConfig = sync .OnceValue (func () * KConfig {
14- config := & KConfig {
11+ // ShowKubeteaConfig 获取k.yaml配置
12+ var ShowKubeteaConfig = sync .OnceValue (func () * KubeteaConfig {
13+ config := & KubeteaConfig {
1514 ClusterByLabel : "app" ,
16- ClusterFilters : []string {"*" },
1715 PodCacheLivetime : 10 ,
16+ Log : KubeteaConfigLog {
17+ Dir : "~/.kubetea/logs" ,
18+ FileTotalMax : 10 ,
19+ Level : logrus .InfoLevel ,
20+ },
21+ ClusterFilters : []string {"*" },
1822 }
1923
2024 // 文件件在才继续加载配置
21- configFilePath := Context .String ("config" )
22- if strings .HasPrefix (configFilePath , "~" ) {
23- homeDir , err := os .UserHomeDir ()
24- if err != nil {
25- logrus .Warnln (err )
25+ configFilePath := FixPath (Context .String ("config" ))
26+
27+ _ , errStat := os .Stat (configFilePath )
28+ if os .IsNotExist (errStat ) {
29+ configFileDir := filepath .Dir (configFilePath )
30+ if errMkdir := os .MkdirAll (configFileDir , os .ModePerm ); errMkdir != nil {
31+ logrus .Warnln (errMkdir )
2632 } else {
27- configFilePath = filepath .Join (homeDir , configFilePath [1 :])
33+ // 文件不存在,写入一份默认配置
34+ yamlData , err := yaml .Marshal (& config )
35+ if err != nil {
36+ logrus .Warnln (err )
37+ } else {
38+ errWrite := os .WriteFile (configFilePath , yamlData , 0664 )
39+ if errWrite != nil {
40+ logrus .WithFields (logrus.Fields {"path" : configFilePath }).Warnln (errWrite )
41+ }
42+ }
2843 }
29- }
30- _ , errStat := os .Stat (configFilePath )
31- if ! os .IsNotExist (errStat ) {
32- // 读取YAML文件内容
44+
45+ } else {
46+ // 文件存在,读取YAML文件内容
3347 yamlFile , err := os .ReadFile (configFilePath )
3448 if err != nil {
3549 logrus .WithFields (logrus.Fields {"config" : configFilePath }).Fatalf ("Failed to read YAML file: %v" , err )
@@ -45,8 +59,16 @@ var ShowKConfig = sync.OnceValue(func() *KConfig {
4559 return config
4660})
4761
48- type KConfig struct {
49- ClusterByLabel string `yaml:"cluster_by_label"`
50- ClusterFilters []string `yaml:"cluster_filters"`
51- PodCacheLivetime uint32 `yaml:"pod_cache_livetime_second"`
62+ // KubeteaConfig YAML配置定义
63+ type KubeteaConfig struct {
64+ ClusterByLabel string `yaml:"cluster_by_label"`
65+ PodCacheLivetime uint32 `yaml:"pod_cache_livetime_second"`
66+ Log KubeteaConfigLog `yaml:"log"`
67+ ClusterFilters []string `yaml:"cluster_filters"`
68+ }
69+
70+ type KubeteaConfigLog struct {
71+ Dir string `yaml:"dir"`
72+ FileTotalMax int `yaml:"file_total_max"`
73+ Level logrus.Level `yaml:"level"`
5274}
0 commit comments