update

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package update 实现 openpe 的新版本检查与升级支撑(业务契约 docs/requirements/2026-08-25-version-and-update.md 方案 U):

  • 通过 Go module proxy 的 /@latest 端点查询最新发布版本(尊重用户 GOPROXY——国内镜像用户自动走自己的镜像,无 GitHub API 配额问题);
  • 把检查结果写入本地缓存文件并限频,hook 披露行只读缓存判断是否提醒, 增强关键路径永不发起网络请求;
  • 版本比较基于 semver,永不建议降级;无法判定(devel)时抑制提醒。

Index

Constants

View Source
const DefaultProxyBaseURL = "https://proxy.golang.org"

DefaultProxyBaseURL 是 GOPROXY 完全不可用(未设置且 go env 读取失败)时的 官方回退。

View Source
const ModulePath = "github.com/AoManoh/openpe"

ModulePath 是本项目的模块路径,@latest 查询与 go install 目标都由它派生。

Variables

This section is empty.

Functions

func IsNewer

func IsNewer(latest string, current string) bool

IsNewer 判断 latest 是否严格新于 current。任一方不是合法 semver(如归一 后的 "devel")返回 false——无法判定时既不提醒也不宣称"已是最新",交由 调用方按场景措辞。伪版本是合法的 semver 预发布,可正常参与比较。

func NoticeVersion

func NoticeVersion(state CheckState, ok bool, current string, now time.Time, interval time.Duration) (string, bool)

NoticeVersion 决定 hook 披露行是否提醒:缓存存在、仍新鲜、且缓存的 latest 严格新于当前版本时返回该版本。其余情况(无缓存、过期、devel 当前版本、 已是最新)一律静默——提醒缺席不是失败。

func ResolveProxyBaseURL

func ResolveProxyBaseURL() string

ResolveProxyBaseURL 解析实际要查询的 proxy 地址:环境变量 GOPROXY 优先, 未设置时读 `go env GOPROXY`(best-effort),再取列表中第一个可用条目; "off"/"direct" 不提供 /@latest 端点,跳过;全不可用回退官方 proxy。

func SaveState

func SaveState(path string, state CheckState) error

SaveState 原子写入缓存(临时文件 + rename),避免并发 hook 读到半截 JSON。

func StatePath

func StatePath(override string) (string, error)

StatePath 解析缓存文件路径。override 对应 OPENPE_CACHE_DIR/Delivery.CacheDir 的缓存根覆盖语义。

Types

type CheckState

type CheckState struct {
	CheckedAt time.Time `json:"checked_at"`
	Latest    string    `json:"latest"`
}

CheckState 是新版本检查的本地缓存:hook 披露行只读它,永不发网络请求。

func LoadState

func LoadState(path string) (CheckState, bool)

LoadState 读取缓存;不存在或损坏返回 ok=false——缓存缺席不是错误,只是 "尚无检查结果"(提醒静默,业务契约 U2.2)。

func (CheckState) Fresh

func (s CheckState) Fresh(now time.Time, interval time.Duration) bool

Fresh 判断缓存是否仍在限频窗口内:新鲜则后台刷新跳过(不发网络请求)。

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
}

Client 查询 module proxy。BaseURL 为空时按 GOPROXY 解析;HTTPClient 为空时 使用带超时的默认客户端。测试用 httptest 地址注入 BaseURL。

func (Client) Latest

func (c Client) Latest(ctx context.Context, modulePath string) (LatestInfo, error)

Latest 查询模块的最新发布版本。零 tag 的模块会得到 main 的伪版本——这也是 合法结果,由调用方的 semver 比较决定是否构成"新版本"。

type LatestInfo

type LatestInfo struct {
	Version string    `json:"Version"`
	Time    time.Time `json:"Time"`
}

LatestInfo 是 module proxy /@latest 端点的 JSON 结构。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL