github.com/metacubex/mihomo@v1.18.5/config/initial.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	C "github.com/metacubex/mihomo/constant"
     8  	"github.com/metacubex/mihomo/log"
     9  )
    10  
    11  // Init prepare necessary files
    12  func Init(dir string) error {
    13  	// initial homedir
    14  	if _, err := os.Stat(dir); os.IsNotExist(err) {
    15  		if err := os.MkdirAll(dir, 0o777); err != nil {
    16  			return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
    17  		}
    18  	}
    19  
    20  	// initial config.yaml
    21  	if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
    22  		log.Infoln("Can't find config, create a initial config file")
    23  		f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
    24  		if err != nil {
    25  			return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
    26  		}
    27  		f.Write([]byte(`mixed-port: 7890`))
    28  		f.Close()
    29  	}
    30  
    31  	return nil
    32  }