diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 84abeb42..c51562f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,6 @@ jobs: run: sudo apt-get install -y curl jq - name: Set up environment run: | - cp config/config.example.yml config/config.yml + cp config.example.yml config.yml - name: Run tests run: go test ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9c39829f..8a579787 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,7 @@ _cgo_export.* *.log *.sqlite *.db -config/config.yml +config.yml # 临时文件 tmp/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 4981615d..8aa6122b 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -51,5 +51,5 @@ archives: strip_binary_directory: true files: - LICENSE + - config.example.yml - storage/* - - config/* diff --git a/README.md b/README.md index cb24048d..72fdc5cc 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ 如果你无法重装系统,请以`root`用户登录服务器,执行以下命令卸载面板: ```shell -curl -fsLm 10 -o uninstall_panel.sh https://dl.cdn.haozi.net/panel/uninstall_panel.sh && bash uninstall_panel.sh +curl -fsLm 10 -o uninstall.sh https://dl.cdn.haozi.net/panel/uninstall.sh && bash uninstall.sh ``` 卸载面板前请务必备份好所有数据,提前卸载面板全部应用。卸载后数据将**无法恢复**! diff --git a/README_EN.md b/README_EN.md index 622f2670..bbd09720 100644 --- a/README_EN.md +++ b/README_EN.md @@ -72,7 +72,7 @@ Recommended to back up data and reinstall the system first, so that the system c If you are unable to reinstall the system, log in to the server as the `root` user and execute the following command to uninstall the panel: ```shell -curl -fsLm 10 -o uninstall_panel.sh https://dl.cdn.haozi.net/panel/uninstall_panel.sh && bash uninstall_panel.sh +curl -fsLm 10 -o uninstall.sh https://dl.cdn.haozi.net/panel/uninstall.sh && bash uninstall.sh ``` Before uninstalling the panel, please be sure to back up all data and uninstall all panel plugins in advance. The data will **not be recoverable** after uninstallation! diff --git a/config/config.example.yml b/config.example.yml similarity index 100% rename from config/config.example.yml rename to config.example.yml diff --git a/internal/bootstrap/conf.go b/internal/bootstrap/conf.go index c21f261a..eab79d84 100644 --- a/internal/bootstrap/conf.go +++ b/internal/bootstrap/conf.go @@ -2,9 +2,6 @@ package bootstrap import ( "log" - "os" - "path/filepath" - "strings" "time" "github.com/knadh/koanf/parsers/yaml" @@ -12,26 +9,17 @@ import ( "github.com/knadh/koanf/v2" "github.com/TheTNB/panel/internal/app" + "github.com/TheTNB/panel/pkg/io" ) func initConf() { - executable, err := os.Executable() - if err != nil { - log.Fatalf("failed to get executable: %v", err) - } - res, err := filepath.EvalSymlinks(filepath.Dir(executable)) - if err != nil { - log.Fatalf("failed to get executable path: %v", err) - } - if isTesting() || isAir() || isDirectlyRun() { - res, err = os.Getwd() - if err != nil { - log.Fatalf("failed to get working directory: %v", err) - } + config := "/usr/local/etc/panel/config.yml" + if !io.Exists(config) { + config = "config.yml" } app.Conf = koanf.New(".") - if err = app.Conf.Load(file.Provider(filepath.Join(res, "config/config.yml")), yaml.Parser()); err != nil { + if err := app.Conf.Load(file.Provider(config), yaml.Parser()); err != nil { log.Fatalf("failed to load config: %v", err) } } @@ -48,32 +36,3 @@ func initGlobal() { } time.Local = loc } - -// isTesting checks if the application is running in testing mode. -func isTesting() bool { - for _, arg := range os.Args { - if strings.Contains(arg, "-test.") { - return true - } - } - - return false -} - -// isAir checks if the application is running using Air. -func isAir() bool { - for _, arg := range os.Args { - if strings.Contains(filepath.ToSlash(arg), "/storage/temp") { - return true - } - } - - return false -} - -// isDirectlyRun checks if the application is running using go run. -func isDirectlyRun() bool { - executable, _ := os.Executable() - return strings.Contains(filepath.Base(executable), os.TempDir()) || - (strings.Contains(filepath.ToSlash(executable), "/var/folders") && strings.Contains(filepath.ToSlash(executable), "/T/go-build")) // macOS -} diff --git a/internal/data/setting.go b/internal/data/setting.go index d6b1fef4..ee02de45 100644 --- a/internal/data/setting.go +++ b/internal/data/setting.go @@ -126,7 +126,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P restartFlag := false config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return false, err } @@ -143,7 +143,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P if err != nil { return false, err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0644); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0644); err != nil { return false, err } if raw != string(encoded) { @@ -179,7 +179,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { color.Greenln("文件名: ", name) color.Greenln("前置检查...") - if io.Exists("/tmp/panel-storage.zip") || io.Exists("/tmp/panel-config.zip") { + if io.Exists("/tmp/panel-storage.zip") { return errors.New("检测到 /tmp 存在临时文件,可能是上次更新失败导致的,请排除后重试") } @@ -193,11 +193,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { color.Redln("备份面板数据失败:", err) return err } - if err := io.Compress([]string{filepath.Join(app.Root, "panel/config")}, "/tmp/panel-config.zip", io.Zip); err != nil { - color.Redln("备份面板配置失败:", err) - return err - } - if !io.Exists("/tmp/panel-storage.zip") || !io.Exists("/tmp/panel-config.zip") { + if !io.Exists("/tmp/panel-storage.zip") { return errors.New("已备份面板数据检查失败") } color.Greenln("备份完成") @@ -242,10 +238,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { if !io.Exists(filepath.Join(app.Root, "panel", "web")) { return errors.New("更新失败,可能是下载过程中出现了问题") } - if err = io.Mv(filepath.Join(app.Root, "panel", "cli"), "/usr/local/sbin/panel-cli"); err != nil { - color.Redln("移动面板命令行工具失败:", err) - return err - } color.Greenln("更新完成") color.Greenln("恢复面板数据...") @@ -253,10 +245,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { color.Redln("恢复面板数据失败:", err) return err } - if err = io.UnCompress("/tmp/panel-config.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil { - color.Redln("恢复面板配置失败:", err) - return err - } if !io.Exists(filepath.Join(app.Root, "panel/storage/app.db")) { return errors.New("恢复面板数据失败") } @@ -275,10 +263,14 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { color.Redln("写入面板版本号失败:", err) return err } + if err = io.Mv(filepath.Join(app.Root, "panel/cli"), "/usr/local/sbin/panel-cli"); err != nil { + color.Redln("移动面板命令行工具失败:", err) + return err + } color.Greenln("设置面板文件权限...") _ = io.Chmod("/usr/local/sbin/panel-cli", 0700) - _ = io.Chmod("/etc/systemd/system/panel.servic", 0700) + _ = io.Chmod("/etc/systemd/system/panel.service", 0700) _ = io.Chmod(filepath.Join(app.Root, "panel"), 0700) color.Greenln("设置完成") @@ -286,7 +278,6 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error { _, _ = shell.Execf("systemctl daemon-reload") _ = io.Remove("/tmp/panel-storage.zip") - _ = io.Remove("/tmp/panel-config.zip") return nil } diff --git a/internal/service/cli.go b/internal/service/cli.go index 15cf32c6..654fe4ae 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -210,7 +210,7 @@ func (s *CliService) UserPassword(ctx context.Context, cmd *cli.Command) error { func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -225,7 +225,7 @@ func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err } @@ -235,7 +235,7 @@ func (s *CliService) HTTPSOn(ctx context.Context, cmd *cli.Command) error { func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -250,7 +250,7 @@ func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err } @@ -260,7 +260,7 @@ func (s *CliService) HTTPSOff(ctx context.Context, cmd *cli.Command) error { func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -275,7 +275,7 @@ func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err } @@ -285,7 +285,7 @@ func (s *CliService) EntranceOn(ctx context.Context, cmd *cli.Command) error { func (s *CliService) EntranceOff(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -300,7 +300,7 @@ func (s *CliService) EntranceOff(ctx context.Context, cmd *cli.Command) error { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err } @@ -315,7 +315,7 @@ func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -330,7 +330,7 @@ func (s *CliService) Port(ctx context.Context, cmd *cli.Command) error { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err } @@ -515,7 +515,7 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error { config := new(types.PanelConfig) cm := yaml.CommentMap{} - raw, err := io.Read(filepath.Join(app.Root, "panel/config/config.yml")) + raw, err := io.Read("/usr/local/etc/panel/config.yml") if err != nil { return err } @@ -530,7 +530,7 @@ func (s *CliService) Init(ctx context.Context, cmd *cli.Command) error { if err != nil { return err } - if err = io.Write(filepath.Join(app.Root, "panel/config/config.yml"), string(encoded), 0700); err != nil { + if err = io.Write("/usr/local/etc/panel/config.yml", string(encoded), 0700); err != nil { return err }