本文整理汇总了Golang中github.com/Unknwon/goconfig.SaveConfigFile函数的典型用法代码示例。如果您正苦于以下问题:Golang SaveConfigFile函数的具体用法?Golang SaveConfigFile怎么用?Golang SaveConfigFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SaveConfigFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SaveLocalNodes
func SaveLocalNodes() {
if err := goconfig.SaveConfigFile(LocalNodes,
path.Join(HomeDir+LocalNodesFile)); err != nil {
log.Error("", "Fail to save localnodes.list:")
log.Error("", "\t"+err.Error())
}
}
示例2: InitModels
func InitModels() {
if !com.IsFile(_CFG_PATH) {
os.Create(_CFG_PATH)
}
var err error
Cfg, err = goconfig.LoadConfigFile(_CFG_PATH)
if err == nil {
beego.Info("Initialize app.ini")
}
setGithubCredentials(Cfg.MustValue("github", "client_id"),
Cfg.MustValue("github", "client_secret"))
docLock = new(sync.RWMutex)
blogLock = new(sync.RWMutex)
initMaps()
// Start check ticker.
checkTicker = time.NewTicker(5 * time.Minute)
go checkTickerTimer(checkTicker.C)
// ATTENTION: you'd better comment following code when developing.
if needCheckUpdate() {
checkFileUpdates()
Cfg.SetValue("app", "update_check_time", strconv.Itoa(int(time.Now().Unix())))
goconfig.SaveConfigFile(Cfg, _CFG_PATH)
}
}
示例3: CreateIniFile
func CreateIniFile() error {
f, err := os.OpenFile("./server.ini", os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return err
}
defer f.Close()
config, err = goconfig.LoadConfigFile("./server.ini")
if err != nil {
return err
}
config.SetSectionComments("App", "")
config.SetSectionComments("User", "")
config.SetValue("App", "IP", "127.0.0.1")
config.SetValue("App", "Port", "8080")
config.SetValue("User", "username", "admin")
config.SetValue("User", "password", helper.MD5("admin"))
err = goconfig.SaveConfigFile(config, "./server.ini")
if err != nil {
return err
}
return nil
}
示例4: runGen
// scan a directory and gen a gopm file
func runGen(ctx *cli.Context) {
setup(ctx)
if !com.IsExist(".gopmfile") {
os.Create(".gopmfile")
}
gf, err := goconfig.LoadConfigFile(".gopmfile")
if err != nil {
log.Error("gen", "Cannot load gopmfile:")
log.Fatal("", "\t"+err.Error())
}
// Get dependencies.
imports := doc.GetAllImports([]string{workDir},
parseTarget(gf.MustValue("target", "path")), ctx.Bool("example"))
for _, p := range imports {
p = doc.GetProjectPath(p)
if strings.HasSuffix(workDir, p) {
continue
}
if value := gf.MustValue("deps", p); len(value) == 0 {
gf.SetValue("deps", p, "")
}
}
err = goconfig.SaveConfigFile(gf, ".gopmfile")
if err != nil {
log.Error("gen", "Fail to save gopmfile:")
log.Fatal("", "\t"+err.Error())
}
log.Success("SUCC", "gen", "Generate gopmfile successfully!")
}
示例5: main
func main() {
read, err := goconfig.LoadConfigFile("read.ini")
if err != nil {
fmt.Println(err)
}
goconfig.PrettyFormat = false
/*ini cfg配置文件读取*/
fmt.Println(read.GetSectionList()) // 获取选项名称
fmt.Println(read.GetKeyList("Cody")) // 获取Cody选项下的key
fmt.Println(read.GetSection("Cody")) // 以map形式获取Cody选项下的key和value的值
fmt.Println(read.GetSectionComments("work")) // 获取选项work上的以“#”和“;”开头的注解
fmt.Println(read.GetKeyComments("Cody", "sex")) // 获取选项Cody的sex键上的注解
fmt.Println(read.GetValue("Cody", "name")) // 获取选项Cody的name键的值
read.DeleteKey("Cody", "hell")
// /**/
// read.SetValue("Cody", "name", "cody.guo") // 设置选项Cody的name键的值为cody.guo
goconfig.SaveConfigFile(read, "read.ini") // 保存
// read.DeleteKey("work", "work") // 删除选项work中的work键
// goconfig.SaveConfigFile(read, "readWrite.ini") // 另存为readWrite.ini
// read.DeleteSection("work") // 删除选项work
// goconfig.SaveConfigFile(read, "read.ini") // 保存配置文件,会把 " : "的修改为" = "
}
示例6: SaveConfig
// SaveConfig saves configuration file.
// if configKey has been set, the file will be encrypted.
func SaveConfig() {
if len(configKey) == 0 {
err := goconfig.SaveConfigFile(configData, ConfigPath)
if err != nil {
log.Fatalf("Failed to save config file: %v", err)
}
err = os.Chmod(ConfigPath, 0600)
if err != nil {
ErrorLog(nil, "Failed to set permissions on config file: %v", err)
}
return
}
var buf bytes.Buffer
err := goconfig.SaveConfigData(configData, &buf)
if err != nil {
log.Fatalf("Failed to save config file: %v", err)
}
f, err := os.Create(ConfigPath)
if err != nil {
log.Fatalf("Failed to save config file: %v", err)
}
fmt.Fprintln(f, "# Encrypted rclone configuration File")
fmt.Fprintln(f, "")
fmt.Fprintln(f, "RCLONE_ENCRYPT_V0:")
// Generate new nonce and write it to the start of the ciphertext
var nonce [24]byte
n, _ := rand.Read(nonce[:])
if n != 24 {
log.Fatalf("nonce short read: %d", n)
}
enc := base64.NewEncoder(base64.StdEncoding, f)
_, err = enc.Write(nonce[:])
if err != nil {
log.Fatalf("Failed to write config file: %v", err)
}
var key [32]byte
copy(key[:], configKey[:32])
b := secretbox.Seal(nil, buf.Bytes(), &nonce, &key)
_, err = enc.Write(b)
if err != nil {
log.Fatalf("Failed to write config file: %v", err)
}
_ = enc.Close()
err = f.Close()
if err != nil {
log.Fatalf("Failed to close config file: %v", err)
}
err = os.Chmod(ConfigPath, 0600)
if err != nil {
ErrorLog(nil, "Failed to set permissions on config file: %v", err)
}
}
示例7: SaveConfig
// SaveConfig saves configuration file.
func SaveConfig() {
err := goconfig.SaveConfigFile(ConfigFile, ConfigPath)
if err != nil {
log.Fatalf("Failed to save config file: %v", err)
}
err = os.Chmod(ConfigPath, 0600)
if err != nil {
log.Printf("Failed to set permissions on config file: %v", err)
}
}
示例8: Write
func (p *ProxyConf) Write(proxy []Proxy) {
if len(proxy) == 0 {
return
}
p.load()
for _, pro := range proxy {
switch pro.pTtype {
case HTTP:
p.SetValue(HTTP, pro.ip, pro.port)
case HTTPS:
p.SetValue(HTTPS, pro.ip, pro.port)
}
}
goconfig.SaveConfigFile(p.ConfigFile, proxyFile)
}
示例9: main
func main() {
cfg, err := goconfig.LoadConfigFile("conf.ini")
if err != nil {
log.Fatalf("无法加载配置文件: %s", err)
}
value, err := cfg.GetValue(goconfig.DEFAULT_SECTION, "key_default")
if err != nil {
log.Fatalf("无法获取键值(%s): %s", "key_default", nil)
}
log.Printf("%s > %s: %s", goconfig.DEFAULT_SECTION, "key_default", value)
isInsert := cfg.SetValue(goconfig.DEFAULT_SECTION, "key_default", "这是新的值")
if err != nil {
log.Fatalf("无法设置键值(%s): %s", "key_default", err)
}
log.Printf("设置键值 %s 为插入操作: %v", "key_default", isInsert)
comment := cfg.GetSectionComments("super")
log.Printf("分区 %s 的注释: %s", "super", comment)
v := cfg.SetSectionComments("super", "# 这是新的分区注释")
log.Printf("分区 %s 的注释被插入或删除: %v", "super", v)
vInt, err := cfg.Int("must", "int")
if err != nil {
log.Fatalf("无法获取键值(%s): %s)", "int", err)
}
log.Printf("%s > %s: %v", "must", "int", vInt)
vBool := cfg.MustBool("must", "bool")
log.Printf("%s > %s: %v", "must", "bool", vBool)
vBool = cfg.MustBool("must", "bool404")
log.Printf("%s > %s: %v", "must", "bool", vBool)
ok := cfg.DeleteKey("must", "string")
log.Printf("删除键值 %s 是否成功: %v", "string", ok)
err = goconfig.SaveConfigFile(cfg, "conf_save.ini")
if err != nil {
log.Fatalf("无法保存配置文件: %s", err)
}
}
示例10: runGen
func runGen(ctx *cli.Context) {
setup(ctx)
if !com.IsExist(".gopmfile") {
os.Create(".gopmfile")
}
gf, err := goconfig.LoadConfigFile(".gopmfile")
if err != nil {
log.Error("gen", "Cannot load gopmfile:")
log.Fatal("", "\t"+err.Error())
}
targetPath := parseTarget(gf.MustValue("target", "path"))
// Get and set dependencies.
imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example"), false)
for _, p := range imports {
p = doc.GetProjectPath(p)
// Skip subpackage(s) of current project.
if isSubpackage(p, targetPath) {
continue
}
// Check if user specified the version.
if value := gf.MustValue("deps", p); len(value) == 0 {
gf.SetValue("deps", p, "")
}
}
// Get and set resources.
res := make([]string, 0, len(commonRes))
for _, cr := range commonRes {
if com.IsExist(cr) {
res = append(res, cr)
}
}
gf.SetValue("res", "include", strings.Join(res, "|"))
err = goconfig.SaveConfigFile(gf, ".gopmfile")
if err != nil {
log.Error("gen", "Fail to save gopmfile:")
log.Fatal("", "\t"+err.Error())
}
log.Success("SUCC", "gen", "Generate gopmfile successfully!")
}
示例11: runConfig
func runConfig(ctx *cli.Context) {
setup(ctx)
if len(ctx.Args()) == 0 {
log.Error("config", "Cannot start command:")
log.Fatal("", "\tNo section specified")
}
switch ctx.Args()[0] {
case "github":
if len(ctx.Args()) < 3 {
log.Error("config", "Cannot config section 'github'")
log.Fatal("", "\tNot enough arguments for client_id and client_secret")
}
doc.Cfg.SetValue("github", "client_id", ctx.Args()[1])
doc.Cfg.SetValue("github", "client_secret", ctx.Args()[2])
goconfig.SaveConfigFile(doc.Cfg, path.Join(doc.HomeDir, doc.GOPM_CONFIG_FILE))
}
log.Success("SUCC", "config", "Command executed successfully!")
}
示例12: main
func main() {
cfg, err := goconfig.LoadConfigFile("conf.ini")
if err != nil {
log.Fatalf("无法加载配置文件:%s", err)
}
value, err := cfg.GetValue(goconfig.DEFAULT_SECTION, "key_default")
if err != nil {
log.Fatalf("无法获取值:%s", err)
}
log.Printf("%s > %s:%s", goconfig.DEFAULT_SECTION, "key_default", value)
isInsert := cfg.SetValue(goconfig.DEFAULT_SECTION, "key_default", "这是新的值")
log.Printf("设在键值%s: %v", "key_default", isInsert)
comment := cfg.GetSectionComments("super")
log.Printf("分区%s的注释:%s", "supper", comment)
_ = cfg.SetSectionComments("super", "# 这是一个新的注释")
_, err = cfg.Int("must", "int")
err = goconfig.SaveConfigFile(cfg, "conf_save.ini")
if err != nil {
log.Fatalf("无法获取值:%s", err)
}
}
示例13: Update
func (p *ProxyConf) Update() {
p.Read()
ok := make(chan bool)
for _, proxy := range p.proxy {
ip := proxy.ip
prot := proxy.port
go func() {
_, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", ip, prot), 10*time.Second)
if err != nil {
fmt.Printf("代理服务器 [%s:%s] 不可用,已删除.\n", ip, prot)
p.DeleteKey(HTTP, ip)
p.DeleteKey(HTTPS, ip)
}
ok <- true
}()
}
for range p.proxy {
<-ok
}
goconfig.SaveConfigFile(p.ConfigFile, proxyFile)
p.Reload()
}
示例14: main
func main() {
// 读取证书文件
rootPEM, err := ioutil.ReadFile("cert.pem")
if err != nil {
log.Println("读取 cert.pem 出错:", err, "请检查文件是否存在")
return
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(rootPEM)
if !ok {
log.Println("证书分析失败,请检查证书文件是否正确")
return
}
// 加载配置文件
cfg, err := goconfig.LoadConfigFile("client.ini")
if err != nil {
log.Println("配置文件加载失败,自动重置配置文件:", err)
cfg, err = goconfig.LoadFromData([]byte{})
if err != nil {
log.Println(err)
return
}
}
var (
port, ok1 = cfg.MustValueSet("client", "port", "7071")
key, ok2 = cfg.MustValueSet("client", "key", "eGauUecvzS05U5DIsxAN4n2hadmRTZGBqNd2zsCkrvwEBbqoITj36mAMk4Unw6Pr")
serverHost, ok3 = cfg.MustValueSet("server", "host", "localhost")
serverPort, ok4 = cfg.MustValueSet("server", "port", "8081")
)
// 如果缺少配置则保存为默认配置
if ok1 || ok2 || ok3 || ok4 {
err = goconfig.SaveConfigFile(cfg, "client.ini")
if err != nil {
log.Println("配置文件保存失败:", err)
}
}
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Println(err)
return
}
defer ln.Close()
log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|")
log.Println("程序版本:" + VERSION)
log.Println("代理端口:" + port)
log.Println("Key:" + key)
log.Println("服务器地址:" + serverHost + ":" + serverPort)
log.Println("|>>>>>>>>>>>>>>>|<<<<<<<<<<<<<<<|")
s := &serve{
serverHost: serverHost,
serverPort: serverPort,
key: key,
conf: &tls.Config{
RootCAs: roots,
},
}
// 登录
if err = s.updateSession(); err != nil {
log.Println("与服务器连接失败:", err)
return
}
log.Println("登录成功,服务器连接完毕")
go s.updateSessionLoop()
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go s.handleConnection(conn)
}
}
示例15: SaveConfig
// SaveConfig saves configuration file.
func SaveConfig() error {
return goconfig.SaveConfigFile(Cfg, "conf/app.ini")
}