11package config
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
7+ "strings"
68
79 "github.com/BurntSushi/toml"
810
@@ -212,7 +214,8 @@ type DockerConfig struct {
212214
213215// LoadConfig 从配置文件加载配置
214216func LoadConfig (filePath string ) (* Config , error ) {
215- if ! FileExists (filePath ) {
217+ exist , filePath2read := FileExists (filePath )
218+ if ! exist {
216219 // 楔入配置文件
217220 err := DefaultConfig ().WriteConfig (filePath )
218221 if err != nil {
@@ -221,15 +224,15 @@ func LoadConfig(filePath string) (*Config, error) {
221224 return DefaultConfig (), nil
222225 }
223226 var config Config
224- ext := filepath .Ext (filePath )
227+ ext := filepath .Ext (filePath2read )
225228 if ext == ".wanf" {
226- if err := wanf .DecodeFile (filePath , & config ); err != nil {
229+ if err := wanf .DecodeFile (filePath2read , & config ); err != nil {
227230 return nil , err
228231 }
229232 return & config , nil
230233 }
231234
232- if _ , err := toml .DecodeFile (filePath , & config ); err != nil {
235+ if _ , err := toml .DecodeFile (filePath2read , & config ); err != nil {
233236 return nil , err
234237 }
235238 return & config , nil
@@ -257,9 +260,37 @@ func (c *Config) WriteConfig(filePath string) error {
257260}
258261
259262// FileExists 检测文件是否存在
260- func FileExists (filename string ) bool {
263+ func FileExists (filename string ) ( bool , string ) {
261264 _ , err := os .Stat (filename )
262- return ! os .IsNotExist (err )
265+ if err == nil {
266+ return true , filename
267+ }
268+ if os .IsNotExist (err ) {
269+ // 获取文件名(不包含路径)
270+ base := filepath .Base (filename )
271+ dir := filepath .Dir (filename )
272+
273+ // 获取扩展名
274+ fileNameBody := strings .TrimSuffix (base , filepath .Ext (base ))
275+
276+ // 重新组合路径, 扩展名改为.wanf, 确认是否存在
277+ wanfFilename := filepath .Join (dir , fileNameBody + ".wanf" )
278+
279+ _ , err = os .Stat (wanfFilename )
280+ if err == nil {
281+ // .wanf 文件存在
282+ fmt .Printf ("\n Found .wanf file: %s\n " , wanfFilename )
283+ return true , wanfFilename
284+ } else if os .IsNotExist (err ) {
285+ // .wanf 文件不存在
286+ return false , ""
287+ } else {
288+ // 其他错误
289+ return false , ""
290+ }
291+ } else {
292+ return false , filename
293+ }
263294}
264295
265296// DefaultConfig 返回默认配置结构体
0 commit comments