-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:refactor config directory and update version to 0.1.1
- Loading branch information
Showing
8 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
adminpassword: initcool-https://blog.nmslwsnd.com | ||
palworldconfigfilepath: PalWorldSettings.ini | ||
port: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/charmbracelet/log" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct { | ||
PalWorldConfigFilePath string | ||
AdminPassword string | ||
Port int | ||
} | ||
|
||
// 初始化并生成默认配置 | ||
func InitDefaultConfig() { | ||
func InitDefaultConfig(configPath string, configFile string) { | ||
|
||
defaultConfig := Config{ | ||
PalWorldConfigFilePath: "/root/palworld/data/Config/LinuxServer/PalWorldSettings.ini", | ||
AdminPassword: "initcool-https://blog.nmslwsnd.com", | ||
Port: 8080, | ||
} | ||
if err := os.MkdirAll(configPath, 0755); err != nil { | ||
log.Error(err) | ||
} | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigFile(configFile) | ||
// 将默认配置写入配置文件 | ||
viper.Set("PalWorldConfigFilePath", defaultConfig.PalWorldConfigFilePath) | ||
viper.Set("AdminPassword", defaultConfig.AdminPassword) | ||
viper.Set("port", 8080) | ||
err := viper.WriteConfig() | ||
if err != nil { | ||
// 处理错误 | ||
fmt.Println("Error writing default config:", err) | ||
log.Error("Error writing default config:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
package global | ||
|
||
import "github.com/limitcool/palworld-admin/config" | ||
import ( | ||
"path/filepath" | ||
|
||
"github.com/decred/dcrd/dcrutil/v2" | ||
"github.com/limitcool/palworld-admin/config" | ||
) | ||
|
||
var Config config.Config | ||
var AppDir = dcrutil.AppDataDir("palworld-Admin", false) | ||
var ConfigPath = filepath.Join(AppDir, "config") | ||
var ConfigFile = filepath.Join(ConfigPath, "config.yaml") | ||
|
||
const VERSION = "0.1.0" | ||
const VERSION = "0.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func OpenOrCreateFile(configPath, configFileName string) (*os.File, error) { | ||
// 确保目录存在 | ||
if err := os.MkdirAll(configPath, 0755); err != nil { | ||
return nil, fmt.Errorf("无法创建目录: %v", err) | ||
} | ||
|
||
// 构建完整的文件路径 | ||
fullPath := filepath.Join(configPath, configFileName) | ||
|
||
// 打开或创建文件 | ||
file, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
return nil, fmt.Errorf("无法打开或创建文件: %v", err) | ||
} | ||
|
||
return file, nil | ||
} |