mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 09:13:49 +08:00
refactor: 重构 tools.Read 函数
This commit is contained in:
@@ -159,7 +159,12 @@ func (r *CronController) Script(ctx http.Context) http.Response {
|
||||
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
|
||||
}
|
||||
|
||||
return Success(ctx, tools.Read(cron.Shell))
|
||||
shell, err := tools.Read(cron.Shell)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return Success(ctx, shell)
|
||||
}
|
||||
|
||||
// Update 更新计划任务
|
||||
|
||||
@@ -108,9 +108,9 @@ func (r *Fail2banController) List(ctx http.Context) http.Response {
|
||||
|
||||
page := ctx.Request().QueryInt("page", 1)
|
||||
limit := ctx.Request().QueryInt("limit", 10)
|
||||
raw := tools.Read("/etc/fail2ban/jail.local")
|
||||
if len(raw) == 0 {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, "Fail2ban 规则为空")
|
||||
raw, err := tools.Read("/etc/fail2ban/jail.local")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
|
||||
jailList := regexp.MustCompile(`\[(.*?)]`).FindAllStringSubmatch(raw, -1)
|
||||
@@ -200,7 +200,10 @@ func (r *Fail2banController) Add(ctx http.Context) http.Response {
|
||||
jailWebsiteMode := ctx.Request().Input("website_mode")
|
||||
jailWebsitePath := ctx.Request().Input("website_path")
|
||||
|
||||
raw := tools.Read("/etc/fail2ban/jail.local")
|
||||
raw, err := tools.Read("/etc/fail2ban/jail.local")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
if (strings.Contains(raw, "["+jailName+"]") && jailType == "service") || (strings.Contains(raw, "["+jailWebsiteName+"]"+"-cc") && jailType == "website" && jailWebsiteMode == "cc") || (strings.Contains(raw, "["+jailWebsiteName+"]"+"-path") && jailType == "website" && jailWebsiteMode == "path") {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, "规则已存在")
|
||||
}
|
||||
@@ -324,7 +327,10 @@ func (r *Fail2banController) Delete(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
jailName := ctx.Request().Input("name")
|
||||
raw := tools.Read("/etc/fail2ban/jail.local")
|
||||
raw, err := tools.Read("/etc/fail2ban/jail.local")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
if !strings.Contains(raw, "["+jailName+"]") {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, "规则不存在")
|
||||
}
|
||||
@@ -421,7 +427,10 @@ func (r *Fail2banController) SetWhiteList(ctx http.Context) http.Response {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, "缺少参数")
|
||||
}
|
||||
|
||||
raw := tools.Read("/etc/fail2ban/jail.local")
|
||||
raw, err := tools.Read("/etc/fail2ban/jail.local")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
// 正则替换
|
||||
reg := regexp.MustCompile(`ignoreip\s*=\s*.*\n`)
|
||||
if reg.MatchString(raw) {
|
||||
@@ -447,7 +456,10 @@ func (r *Fail2banController) GetWhiteList(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
raw := tools.Read("/etc/fail2ban/jail.local")
|
||||
raw, err := tools.Read("/etc/fail2ban/jail.local")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
reg := regexp.MustCompile(`ignoreip\s*=\s*(.*)\n`)
|
||||
if reg.MatchString(raw) {
|
||||
ignoreIp := reg.FindStringSubmatch(raw)[1]
|
||||
|
||||
@@ -104,9 +104,8 @@ func (r *Mysql57Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/mysql/conf/my.cnf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/mysql/conf/my.cnf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -104,9 +104,8 @@ func (r *Mysql80Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/mysql/conf/my.cnf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/mysql/conf/my.cnf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -100,8 +100,8 @@ func (r *OpenRestyController) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/openresty/conf/nginx.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/openresty/conf/nginx.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,11 @@ func (r *Php74Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,11 @@ func (r *Php80Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,11 @@ func (r *Php81Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,11 @@ func (r *Php82Controller) GetConfig(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,10 @@ func (r *PhpMyAdminController) Info(ctx http.Context) http.Response {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 phpMyAdmin 目录")
|
||||
}
|
||||
|
||||
conf := tools.Read("/www/server/vhost/phpmyadmin.conf")
|
||||
conf, err := tools.Read("/www/server/vhost/phpmyadmin.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
match := regexp.MustCompile(`listen\s+(\d+);`).FindStringSubmatch(conf)
|
||||
if len(match) == 0 {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 phpMyAdmin 端口")
|
||||
@@ -64,7 +67,10 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "端口不能为空")
|
||||
}
|
||||
|
||||
conf := tools.Read("/www/server/vhost/phpmyadmin.conf")
|
||||
conf, err := tools.Read("/www/server/vhost/phpmyadmin.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
conf = regexp.MustCompile(`listen\s+(\d+);`).ReplaceAllString(conf, "listen "+port+";")
|
||||
if err := tools.Write("/www/server/vhost/phpmyadmin.conf", conf, 0644); err != nil {
|
||||
facades.Log().Request(ctx.Request()).Tags("插件", "phpMyAdmin").With(map[string]any{
|
||||
@@ -89,8 +95,7 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
|
||||
}
|
||||
}
|
||||
|
||||
err := tools.ServiceReload("openresty")
|
||||
if err != nil {
|
||||
if err := tools.ServiceReload("openresty"); err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "重载OpenResty失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -103,8 +103,8 @@ func (r *Postgresql15Controller) GetConfig(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/postgresql/data/postgresql.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/postgresql/data/postgresql.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
|
||||
}
|
||||
|
||||
@@ -119,8 +119,8 @@ func (r *Postgresql15Controller) GetUserConfig(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/postgresql/data/pg_hba.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/postgresql/data/pg_hba.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -103,8 +103,8 @@ func (r *Postgresql16Controller) GetConfig(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/postgresql/data/postgresql.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/postgresql/data/postgresql.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
|
||||
}
|
||||
|
||||
@@ -119,8 +119,8 @@ func (r *Postgresql16Controller) GetUserConfig(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/postgresql/data/pg_hba.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/postgresql/data/pg_hba.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -81,8 +81,8 @@ func (r *RedisController) GetConfig(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
config := tools.Read("/www/server/redis/redis.conf")
|
||||
if len(config) == 0 {
|
||||
config, err := tools.Read("/www/server/redis/redis.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "获取Redis配置失败")
|
||||
}
|
||||
|
||||
|
||||
@@ -135,10 +135,15 @@ func (r *SupervisorController) Config(ctx http.Context) http.Response {
|
||||
}
|
||||
|
||||
var config string
|
||||
var err error
|
||||
if tools.IsRHEL() {
|
||||
config = tools.Read(`/etc/supervisord.conf`)
|
||||
config, err = tools.Read(`/etc/supervisord.conf`)
|
||||
} else {
|
||||
config = tools.Read(`/etc/supervisor/supervisord.conf`)
|
||||
config, err = tools.Read(`/etc/supervisor/supervisord.conf`)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
@@ -342,10 +347,15 @@ func (r *SupervisorController) ProcessConfig(ctx http.Context) http.Response {
|
||||
|
||||
process := ctx.Request().Query("process")
|
||||
var config string
|
||||
var err error
|
||||
if tools.IsRHEL() {
|
||||
config = tools.Read(`/etc/supervisord.d/` + process + `.conf`)
|
||||
config, err = tools.Read(`/etc/supervisord.d/` + process + `.conf`)
|
||||
} else {
|
||||
config = tools.Read(`/etc/supervisor/conf.d/` + process + `.conf`)
|
||||
config, err = tools.Read(`/etc/supervisor/conf.d/` + process + `.conf`)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, config)
|
||||
|
||||
@@ -25,7 +25,10 @@ func (r *ToolBoxController) GetDNS(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
raw := tools.Read("/etc/resolv.conf")
|
||||
raw, err := tools.Read("/etc/resolv.conf")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
match := regexp.MustCompile(`nameserver\s+(\S+)`).FindAllStringSubmatch(raw, -1)
|
||||
if len(match) == 0 {
|
||||
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 DNS 信息")
|
||||
@@ -228,7 +231,12 @@ func (r *ToolBoxController) GetHosts(ctx http.Context) http.Response {
|
||||
return check
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, tools.Read("/etc/hosts"))
|
||||
hosts, err := tools.Read("/etc/hosts")
|
||||
if err != nil {
|
||||
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
|
||||
}
|
||||
|
||||
return controllers.Success(ctx, hosts)
|
||||
}
|
||||
|
||||
// SetHosts 设置 hosts 信息
|
||||
|
||||
@@ -342,7 +342,10 @@ func (r *SafeController) GetPingStatus(ctx http.Context) http.Response {
|
||||
return Success(ctx, false)
|
||||
}
|
||||
} else {
|
||||
config := tools.Read("/etc/ufw/before.rules")
|
||||
config, err := tools.Read("/etc/ufw/before.rules")
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if strings.Contains(config, "-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT") {
|
||||
return Success(ctx, true)
|
||||
} else {
|
||||
|
||||
@@ -162,8 +162,14 @@ func (r *WebsiteController) GetDefaultConfig(ctx http.Context) http.Response {
|
||||
if check != nil {
|
||||
return check
|
||||
}
|
||||
index := tools.Read("/www/server/openresty/html/index.html")
|
||||
stop := tools.Read("/www/server/openresty/html/stop.html")
|
||||
index, err := tools.Read("/www/server/openresty/html/index.html")
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
stop, err := tools.Read("/www/server/openresty/html/stop.html")
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return Success(ctx, http.Json{
|
||||
"index": index,
|
||||
@@ -642,17 +648,18 @@ func (r *WebsiteController) Status(ctx http.Context) http.Response {
|
||||
|
||||
website := models.Website{}
|
||||
if err := facades.Orm().Query().Where("id", idRequest.ID).Get(&website); err != nil {
|
||||
facades.Log().Info("[面板][WebsiteController] 获取网站信息失败 ", err)
|
||||
return ErrorSystem(ctx)
|
||||
}
|
||||
|
||||
website.Status = ctx.Request().InputBool("status")
|
||||
if err := facades.Orm().Query().Save(&website); err != nil {
|
||||
facades.Log().Info("[面板][WebsiteController] 保存网站配置失败 ", err)
|
||||
return ErrorSystem(ctx)
|
||||
}
|
||||
|
||||
raw := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
raw, err := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
// 运行目录
|
||||
rootConfig := tools.Cut(raw, "# root标记位开始\n", "# root标记位结束")
|
||||
|
||||
@@ -325,7 +325,10 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
|
||||
}
|
||||
|
||||
// 原文
|
||||
raw := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
raw, err := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(raw) != strings.TrimSpace(config.Raw) {
|
||||
if err := tools.Write("/www/server/vhost/"+website.Name+".conf", config.Raw, 0644); err != nil {
|
||||
return err
|
||||
@@ -512,7 +515,7 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := tools.Exec("systemctl reload openresty")
|
||||
_, err = tools.Exec("systemctl reload openresty")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -548,7 +551,10 @@ func (r *WebsiteImpl) GetConfig(id uint) (WebsiteSetting, error) {
|
||||
return WebsiteSetting{}, err
|
||||
}
|
||||
|
||||
config := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
config, err := tools.Read("/www/server/vhost/" + website.Name + ".conf")
|
||||
if err != nil {
|
||||
return WebsiteSetting{}, err
|
||||
}
|
||||
|
||||
var setting WebsiteSetting
|
||||
setting.Name = website.Name
|
||||
@@ -582,7 +588,10 @@ func (r *WebsiteImpl) GetConfig(id uint) (WebsiteSetting, error) {
|
||||
}
|
||||
|
||||
if tools.Exists(setting.Root + "/.user.ini") {
|
||||
userIni := tools.Read(setting.Path + "/.user.ini")
|
||||
userIni, err := tools.Read(setting.Path + "/.user.ini")
|
||||
if err != nil {
|
||||
return WebsiteSetting{}, err
|
||||
}
|
||||
if strings.Contains(userIni, "open_basedir") {
|
||||
setting.OpenBasedir = true
|
||||
} else {
|
||||
@@ -592,15 +601,20 @@ func (r *WebsiteImpl) GetConfig(id uint) (WebsiteSetting, error) {
|
||||
setting.OpenBasedir = false
|
||||
}
|
||||
|
||||
setting.SslCertificate = tools.Read("/www/server/vhost/ssl/" + website.Name + ".pem")
|
||||
setting.SslCertificateKey = tools.Read("/www/server/vhost/ssl/" + website.Name + ".key")
|
||||
cert, err := tools.Read("/www/server/vhost/ssl/" + website.Name + ".pem")
|
||||
if err == nil {
|
||||
setting.SslCertificate = cert
|
||||
}
|
||||
key, err := tools.Read("/www/server/vhost/ssl/" + website.Name + ".key")
|
||||
if err == nil {
|
||||
setting.SslCertificateKey = key
|
||||
}
|
||||
if setting.Ssl {
|
||||
ssl := tools.Cut(config, "# ssl标记位开始", "# ssl标记位结束")
|
||||
setting.HttpRedirect = strings.Contains(ssl, "# http重定向标记位")
|
||||
setting.Hsts = strings.Contains(ssl, "# hsts标记位")
|
||||
|
||||
certData := tools.Read("/www/server/vhost/ssl/" + website.Name + ".pem")
|
||||
block, _ := pem.Decode([]byte(certData))
|
||||
block, _ := pem.Decode([]byte(cert))
|
||||
if block != nil {
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err == nil {
|
||||
@@ -631,7 +645,10 @@ func (r *WebsiteImpl) GetConfig(id uint) (WebsiteSetting, error) {
|
||||
setting.WafCache = match[1]
|
||||
}
|
||||
|
||||
setting.Rewrite = tools.Read("/www/server/vhost/rewrite/" + website.Name + ".conf")
|
||||
rewrite, err := tools.Read("/www/server/vhost/rewrite/" + website.Name + ".conf")
|
||||
if err == nil {
|
||||
setting.Rewrite = rewrite
|
||||
}
|
||||
log, err := tools.Exec(`tail -n 100 '/www/wwwlogs/` + website.Name + `.log'`)
|
||||
setting.Log = log
|
||||
|
||||
|
||||
@@ -68,5 +68,10 @@ func NewSSHClient(conf *SSHClientConfig) (*ssh.Client, error) {
|
||||
}
|
||||
|
||||
func getKey(keyPath string) (ssh.Signer, error) {
|
||||
return ssh.ParsePrivateKey([]byte(tools.Read(keyPath)))
|
||||
key, err := tools.Read(keyPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ssh.ParsePrivateKey([]byte(key))
|
||||
}
|
||||
|
||||
@@ -25,14 +25,9 @@ func Write(path string, data string, permission os.FileMode) error {
|
||||
}
|
||||
|
||||
// Read 读取文件
|
||||
// TODO 重构带 error 返回
|
||||
func Read(path string) string {
|
||||
func Read(path string) (string, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(data)
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
// Remove 删除文件/目录
|
||||
|
||||
@@ -35,7 +35,9 @@ func (s *SystemHelperTestSuite) TestRead() {
|
||||
err := os.WriteFile(filePath, []byte("test data"), 0644)
|
||||
s.Nil(err)
|
||||
|
||||
s.Equal("test data", Read(filePath))
|
||||
data, err := Read(filePath)
|
||||
s.Nil(err)
|
||||
s.Equal("test data", data)
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestRemove() {
|
||||
|
||||
Reference in New Issue
Block a user