Documentation
¶
Index ¶
- Variables
- func Compare(a, b string) (int, error)
- func ExtractArchive(src, dest string, opts ...ArchiveOption) error
- func NeedsUpgrade(current, latest string, stable bool) bool
- func RunInstaller(path string, args ...string) error
- func VerifyChecksum(path, sum string) error
- type Applier
- type ArchiveOption
- type ArchiveStrategy
- type Checker
- type DownloadOption
- type Downloader
- type DownloaderOption
- type InstallerStrategy
- type Logger
- type Notifier
- type Observer
- type Option
- type Release
- type Reporter
- type ReporterOption
- type SelfReplaceStrategy
- type Source
- type SourceOption
- type UpdateEvent
- type Updater
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSkip 在 WithFileHook 中返回,表示跳过当前归档条目(用于保留用户配置)。 ErrSkip = errors.New("versionup: skip this archive entry") // ErrNotImplemented 表示某策略尚未实现(如 SelfReplaceStrategy 需平台相关逻辑)。 ErrNotImplemented = errors.New("versionup: not implemented") )
应用层相关错误。
Functions ¶
func Compare ¶
Compare 比较两个语义化版本号 a 与 b(允许前缀 "v"/"V",忽略构建元数据 "+xxx")。 返回 -1(a<b)、0(相等)、1(a>b)。 预发布版本(如 1.5.0-rc1)按 SemVer 规则低于同核心版本的正式版。
func ExtractArchive ¶
func ExtractArchive(src, dest string, opts ...ArchiveOption) error
ExtractArchive 解压 src 到 dest 目录,按扩展名识别格式: zip / tar / tar.gz(.tgz) / tar.bz2(.tbz/.tbz2)。 tar.xz(.txz) 因标准库无 xz 解码器,返回 ErrNotImplemented(需外部依赖)。
func NeedsUpgrade ¶
NeedsUpgrade 判断从 current 升级到 latest 是否必要。 仅当 latest > current 且 stable 为真时返回 true。
func RunInstaller ¶
RunInstaller 启动下载到的安装器(.exe/.msi/.pkg/.deb 等)并等待其退出。 提权、是否等待完成由调用方决定;本函数仅负责启动进程。
func VerifyChecksum ¶
VerifyChecksum 校验文件 sha256 是否与 sum("sha256:..." 或纯 hex)一致。
Types ¶
type Applier ¶
type Applier interface {
Apply(ctx context.Context, pkgPath string, ev UpdateEvent) error
}
Applier 应用(安装)下载到的更新包。具体策略由项目选择并注入, 库不固化平台相关的「替换/重启/提权/配置迁移/回滚」逻辑。
type ArchiveOption ¶
type ArchiveOption func(*archiveConfig)
ArchiveOption 配置解压行为。
func WithFileHook ¶
func WithFileHook(fn func(rel string, r io.Reader) error) ArchiveOption
WithFileHook 注册每个普通文件条目的回调,在写入前调用。 回调收到相对路径与条目内容读取器:
- 返回 ErrSkip 跳过该条目(如保留现有用户配置、不覆盖);
- 返回其他错误则中止解压;
- 返回 nil 表示由库写入;此时回调【不得】消费读取器。
func WithStripComponents ¶
func WithStripComponents(n int) ArchiveOption
WithStripComponents 跳过路径前 n 个组件(类似 tar --strip-components)。
type ArchiveStrategy ¶
type ArchiveStrategy struct {
Dest string // 解压目标目录
StripComponents int // 跳过压缩包内前 N 层目录
Preserve func(rel string) bool // 返回 true 表示保留现有文件(跳过不覆盖)
}
ArchiveStrategy 处理「压缩包」形态:解压到 Dest,并通过 Preserve 保留用户配置(不覆盖)。
func (ArchiveStrategy) Apply ¶
func (s ArchiveStrategy) Apply(ctx context.Context, pkg string, ev UpdateEvent) error
Apply 解压压缩包,按 Preserve 跳过用户配置文件。
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker 周期检查器:按间隔向 Source 询问最新版本,发现更新则分发事件并上报。
type DownloadOption ¶
type DownloadOption func(*downloadConfig)
DownloadOption 下载选项。
func WithChecksum ¶
func WithChecksum(sum string) DownloadOption
WithChecksum 设置下载后校验的 sha256 值("sha256:..." 或纯 hex)。
func WithProgress ¶
func WithProgress(fn func(done, total int64)) DownloadOption
WithProgress 设置下载进度回调(done/total,total 未知时为 -1)。
type Downloader ¶
type Downloader interface {
Download(ctx context.Context, url, dest string, opts ...DownloadOption) error
}
Downloader 下载器:把版本包下载到本地。
func NewHTTPDownloader ¶
func NewHTTPDownloader(opts ...DownloaderOption) Downloader
NewHTTPDownloader 创建默认 HTTP 下载器。
type DownloaderOption ¶
type DownloaderOption func(*httpDownloader)
DownloaderOption 配置下载器。
func WithDownloadClient ¶
func WithDownloadClient(c *http.Client) DownloaderOption
WithDownloadClient 设置自定义 *http.Client。
type InstallerStrategy ¶
type InstallerStrategy struct {
Args []string
}
InstallerStrategy 处理「安装器」形态:直接启动安装器,后续由其自身完成。
func (InstallerStrategy) Apply ¶
func (s InstallerStrategy) Apply(ctx context.Context, pkg string, ev UpdateEvent) error
Apply 启动安装器。
type Notifier ¶
type Notifier interface {
Enable() // 开启事件通知
Disable() // 关闭事件通知
Enabled() bool // 查询当前是否开启
Subscribe(obs Observer) // 订阅升级通知
NotifyAt(t time.Time, e UpdateEvent) // 指定时间投递一次通知
Dispatch(e UpdateEvent) // 若开启则广播给订阅者
}
Notifier 通知管理器:控制通知开关与定时。
type Option ¶
type Option func(*Updater)
Option 配置 Updater。
func WithAutoApply ¶
WithAutoApply 控制「发现更新后是否自动调用 Applier.Apply」,默认 false(仅通知)。
func WithNotifier ¶
WithNotifier 设置通知管理器(可选,默认 NewNotifier 且开启)。
type Release ¶
type Release struct {
ToolID string `json:"tool_id"`
Version string `json:"version"` // 语义化版本,如 v1.2.3
Stable bool `json:"stable"` // 是否稳定版
DownloadURL string `json:"download_url"` // 安装包下载地址
ReleaseNotes string `json:"release_notes"` // 更新说明
PublishedAt time.Time `json:"published_at"`
Checksum string `json:"checksum"` // 可选,sha256 hex(可带 "sha256:" 前缀)
}
Release 服务端返回的一个稳定发布版本。
type Reporter ¶
type Reporter interface {
Report(ctx context.Context, event UpdateEvent) error
}
Reporter 版本升级事件上报接口。
func NewHTTPReporter ¶
func NewHTTPReporter(endpoint string, opts ...ReporterOption) Reporter
NewHTTPReporter 创建默认 HTTP 上报器:POST 事件 JSON 到 endpoint。 上报失败仅返回错误,由调用方决定是否忽略(检查循环不因此中断)。
type ReporterOption ¶
type ReporterOption func(*httpReporter)
ReporterOption 配置上报器。
func WithReporterClient ¶
func WithReporterClient(c *http.Client) ReporterOption
WithReporterClient 设置自定义 *http.Client。
type SelfReplaceStrategy ¶
type SelfReplaceStrategy struct {
CurrentExe string
}
SelfReplaceStrategy 处理「单可执行文件」形态:平台相关(Windows 文件锁需改名/重启), 核心库不实现。推荐参考 go-update / Squirrel 等成熟方案,或项目侧按目标平台实现:
- 下载新 exe 到临时名;2) 将当前 exe 重命名为 .old;
- 新 exe 落到原路径;4) 启动新 exe 并退出自己; 若仍被锁则用 MoveFileEx 的 MOVEFILE_DELAY_UNTIL_REBOOT 延迟到重启替换。
func (SelfReplaceStrategy) Apply ¶
func (s SelfReplaceStrategy) Apply(ctx context.Context, pkg string, ev UpdateEvent) error
Apply 返回 ErrNotImplemented 占位。
type Source ¶
Source 版本源:拉取某工具最新稳定版。
func NewHTTPSource ¶
func NewHTTPSource(baseURL string, opts ...SourceOption) Source
NewHTTPSource 创建基于 HTTP 端点的版本源。 端点约定:GET {baseURL}/version?tool={toolID} 返回 Release JSON(见文档 §7)。
func NewManifestSource ¶
func NewManifestSource(rawURL string, opts ...SourceOption) Source
NewManifestSource 创建基于静态清单文件的版本源。
type SourceOption ¶
type SourceOption func(*sourceConfig)
SourceOption 配置版本源(如自定义 HTTP Client)。
func WithSourceClient ¶
func WithSourceClient(c *http.Client) SourceOption
WithSourceClient 设置自定义 *http.Client。
func WithSourceUserAgent ¶
func WithSourceUserAgent(ua string) SourceOption
WithSourceUserAgent 设置 User-Agent。
type UpdateEvent ¶
type UpdateEvent struct {
ToolID string
CurrentVersion string
LatestVersion string
DownloadURL string
ReleaseNotes string
PublishedAt time.Time
DetectedAt time.Time
}
UpdateEvent 版本升级事件。当比对发现「有新稳定版本」时由库构造并分发。
type Updater ¶
type Updater struct {
ToolID string
Current string
Source Source
Reporter Reporter
Notifier Notifier
Checker *Checker
// contains filtered or unexported fields
}
Updater 版本升级器:组合 Source / Reporter / Notifier / Checker,提供周期检查与即时检查。
func (*Updater) CheckNow ¶
func (u *Updater) CheckNow(ctx context.Context) (*UpdateEvent, error)
CheckNow 立即执行一次检查。