// Package config 提供配置文件的加载和解析功能 package config import ( "os" "path/filepath" "git.kingecg.top/kingecg/installerbuilder/internal/errors" "github.com/spf13/viper" "gopkg.in/yaml.v3" ) // Config 表示安装包构建配置 type Config struct { // 基本信息 Name string `yaml:"name" mapstructure:"name"` // 安装包名称 Version string `yaml:"version" mapstructure:"version"` // 安装包版本 Description string `yaml:"description" mapstructure:"description"` // 安装包描述 Author string `yaml:"author" mapstructure:"author"` // 作者信息 License string `yaml:"license" mapstructure:"license"` // 许可证信息 // 构建设置 Build BuildConfig `yaml:"build" mapstructure:"build"` // 构建配置 // 安装包内容 Contents ContentsConfig `yaml:"contents" mapstructure:"contents"` // 安装包内容配置 // 平台特定配置 Platforms PlatformsConfig `yaml:"platforms" mapstructure:"platforms"` // 平台特定配置 } // BuildConfig 表示构建相关配置 type BuildConfig struct { OutputDir string `yaml:"outputDir" mapstructure:"outputDir"` // 输出目录 Targets []string `yaml:"targets" mapstructure:"targets"` // 目标平台列表 Formats []string `yaml:"formats" mapstructure:"formats"` // 输出格式列表 } // ContentsConfig 表示安装包内容配置 type ContentsConfig struct { Files []FileConfig `yaml:"files" mapstructure:"files"` // 文件列表 Directories []DirectoryConfig `yaml:"directories" mapstructure:"directories"` // 目录列表 Scripts []ScriptConfig `yaml:"scripts" mapstructure:"scripts"` // 脚本列表 Symlinks []SymlinkConfig `yaml:"symlinks" mapstructure:"symlinks"` // 符号链接列表 Resources []ResourceConfig `yaml:"resources" mapstructure:"resources"` // 资源列表 Permissions []PermissionConfig `yaml:"permissions" mapstructure:"permissions"` // 权限设置列表 } // FileConfig 表示文件配置 type FileConfig struct { Source string `yaml:"source" mapstructure:"source"` // 源文件路径 Destination string `yaml:"destination" mapstructure:"destination"` // 目标路径 Mode string `yaml:"mode" mapstructure:"mode"` // 文件权限模式 Owner string `yaml:"owner" mapstructure:"owner"` // 文件所有者 Group string `yaml:"group" mapstructure:"group"` // 文件所属组 } // DirectoryConfig 表示目录配置 type DirectoryConfig struct { Path string `yaml:"path" mapstructure:"path"` // 目录路径 Mode string `yaml:"mode" mapstructure:"mode"` // 目录权限模式 Owner string `yaml:"owner" mapstructure:"owner"` // 目录所有者 Group string `yaml:"group" mapstructure:"group"` // 目录所属组 } // ScriptConfig 表示脚本配置 type ScriptConfig struct { Type string `yaml:"type" mapstructure:"type"` // 脚本类型(preinstall, postinstall, preuninstall, postuninstall) Content string `yaml:"content" mapstructure:"content"` // 脚本内容 Source string `yaml:"source" mapstructure:"source"` // 脚本源文件路径 } // SymlinkConfig 表示符号链接配置 type SymlinkConfig struct { Source string `yaml:"source" mapstructure:"source"` // 源路径 Destination string `yaml:"destination" mapstructure:"destination"` // 目标路径 } // ResourceConfig 表示资源配置 type ResourceConfig struct { Type string `yaml:"type" mapstructure:"type"` // 资源类型 Source string `yaml:"source" mapstructure:"source"` // 源路径 Destination string `yaml:"destination" mapstructure:"destination"` // 目标路径 } // PermissionConfig 表示权限设置配置 type PermissionConfig struct { Path string `yaml:"path" mapstructure:"path"` // 路径 Mode string `yaml:"mode" mapstructure:"mode"` // 权限模式 Owner string `yaml:"owner" mapstructure:"owner"` // 所有者 Group string `yaml:"group" mapstructure:"group"` // 所属组 } // PlatformsConfig 表示平台特定配置 type PlatformsConfig struct { Windows WindowsConfig `yaml:"windows" mapstructure:"windows"` // Windows平台配置 Linux LinuxConfig `yaml:"linux" mapstructure:"linux"` // Linux平台配置 } // WindowsConfig 表示Windows平台配置 type WindowsConfig struct { Msi WindowsMsiConfig `yaml:"msi" mapstructure:"msi"` // MSI配置 } // WindowsMsiConfig 表示Windows MSI配置 type WindowsMsiConfig struct { UpgradeCode string `yaml:"upgradeCode" mapstructure:"upgradeCode"` // 升级代码 Manufacturer string `yaml:"manufacturer" mapstructure:"manufacturer"` // 制造商 ProductCode string `yaml:"productCode" mapstructure:"productCode"` // 产品代码 InstallDir string `yaml:"installDir" mapstructure:"installDir"` // 安装目录 ProgramMenuDir string `yaml:"programMenuDir" mapstructure:"programMenuDir"` // 开始菜单目录 Shortcuts []WindowsShortcutConfig `yaml:"shortcuts" mapstructure:"shortcuts"` // 快捷方式列表 } // WindowsShortcutConfig 表示Windows快捷方式配置 type WindowsShortcutConfig struct { Name string `yaml:"name" mapstructure:"name"` // 快捷方式名称 Target string `yaml:"target" mapstructure:"target"` // 目标路径 Description string `yaml:"description" mapstructure:"description"` // 描述 Icon string `yaml:"icon" mapstructure:"icon"` // 图标路径 WorkingDir string `yaml:"workingDir" mapstructure:"workingDir"` // 工作目录 } // LinuxConfig 表示Linux平台配置 type LinuxConfig struct { Deb LinuxDebConfig `yaml:"deb" mapstructure:"deb"` // DEB配置 Rpm LinuxRpmConfig `yaml:"rpm" mapstructure:"rpm"` // RPM配置 } // LinuxDebConfig 表示Linux DEB配置 type LinuxDebConfig struct { Section string `yaml:"section" mapstructure:"section"` // 软件包分类 Priority string `yaml:"priority" mapstructure:"priority"` // 优先级 Architecture string `yaml:"architecture" mapstructure:"architecture"` // 架构 Depends []string `yaml:"depends" mapstructure:"depends"` // 依赖项列表 Recommends []string `yaml:"recommends" mapstructure:"recommends"` // 推荐安装项列表 Suggests []string `yaml:"suggests" mapstructure:"suggests"` // 建议安装项列表 Conflicts []string `yaml:"conflicts" mapstructure:"conflicts"` // 冲突项列表 Replaces []string `yaml:"replaces" mapstructure:"replaces"` // 替换项列表 } // LinuxRpmConfig 表示Linux RPM配置 type LinuxRpmConfig struct { Group string `yaml:"group" mapstructure:"group"` // 软件包组 Vendor string `yaml:"vendor" mapstructure:"vendor"` // 供应商 URL string `yaml:"url" mapstructure:"url"` // URL Requires []string `yaml:"requires" mapstructure:"requires"` // 依赖项列表 Provides []string `yaml:"provides" mapstructure:"provides"` // 提供项列表 Conflicts []string `yaml:"conflicts" mapstructure:"conflicts"` // 冲突项列表 Obsoletes []string `yaml:"obsoletes" mapstructure:"obsoletes"` // 废弃项列表 AutoReqProv bool `yaml:"autoReqProv" mapstructure:"autoReqProv"` // 自动依赖检测 } // LoadConfig 从指定路径加载配置文件 func LoadConfig(path string) (*Config, error) { v := viper.New() v.SetConfigFile(path) if err := v.ReadInConfig(); err != nil { return nil, errors.WrapWithCode( err, errors.ErrConfigParse, "ConfigLoader", "读取配置文件失败", map[string]interface{}{"path": path}, ) } var config Config if err := v.Unmarshal(&config); err != nil { return nil, errors.WrapWithCode( err, errors.ErrConfigParse, "ConfigLoader", "解析配置文件失败", map[string]interface{}{"path": path}, ) } return &config, nil } // SaveConfig 将配置保存到指定路径 func SaveConfig(config *Config, path string) error { // 确保目录存在 dir := filepath.Dir(path) if err := os.MkdirAll(dir, 0755); err != nil { return errors.WrapWithCode( err, errors.ErrResourceCopy, "ConfigSaver", "创建目录失败", map[string]interface{}{"path": dir}, ) } // 将配置转换为YAML data, err := yaml.Marshal(config) if err != nil { return errors.WrapWithCode( err, errors.ErrConfigParse, "ConfigSaver", "序列化配置失败", nil, ) } // 写入文件 if err := os.WriteFile(path, data, 0644); err != nil { return errors.WrapWithCode( err, errors.ErrResourceCopy, "ConfigSaver", "写入配置文件失败", map[string]interface{}{"path": path}, ) } return nil } // DefaultConfig 返回默认配置 func DefaultConfig() *Config { return &Config{ Name: "my-application", Version: "1.0.0", Description: "My Application Description", Author: "Author Name", License: "MIT", Build: BuildConfig{ OutputDir: "dist", Targets: []string{"windows", "linux"}, Formats: []string{"msi", "deb", "rpm", "zip"}, }, Contents: ContentsConfig{ Files: []FileConfig{ { Source: "bin/myapp", Destination: "bin/myapp", Mode: "0755", }, { Source: "README.md", Destination: "doc/README.md", Mode: "0644", }, }, Directories: []DirectoryConfig{ { Path: "logs", Mode: "0755", }, { Path: "config", Mode: "0755", }, }, Scripts: []ScriptConfig{ { Type: "postinstall", Content: "#!/bin/sh\necho \"Installation completed successfully!\"", }, }, }, Platforms: PlatformsConfig{ Windows: WindowsConfig{ Msi: WindowsMsiConfig{ UpgradeCode: "12345678-1234-1234-1234-123456789012", Manufacturer: "My Company", InstallDir: "MyApplication", ProgramMenuDir: "MyApplication", Shortcuts: []WindowsShortcutConfig{ { Name: "My Application", Target: "[INSTALLDIR]bin\\myapp.exe", Description: "Launch My Application", Icon: "[INSTALLDIR]icons\\app.ico", }, }, }, }, Linux: LinuxConfig{ Deb: LinuxDebConfig{ Section: "utils", Priority: "optional", Architecture: "amd64", Depends: []string{"libc6"}, }, Rpm: LinuxRpmConfig{ Group: "Applications/System", Vendor: "My Company", URL: "https://example.com", Requires: []string{"glibc"}, AutoReqProv: true, }, }, }, } }