pkg

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractProjectName added in v1.0.2

func ExtractProjectName(module string) string

ExtractProjectName 从 Go module 路径中提取项目名(路径最后一段非空片段)。

func ExtractServiceName

func ExtractServiceName(projectRootPath, currentDir string) (string, error)

ExtractServiceName 从 currentDir 中提取服务名称,前提是 currentDir 位于 projectRootPath 之下,并且路径结构符合 app/{service}/service/cmd/server。

func GoInstall

func GoInstall(ctx context.Context, paths ...string) error

GoInstall 使用 `go install` 安装指定路径的包。 若路径中不包含版本号,则默认使用 @latest。

func GoModTidy

func GoModTidy(ctx context.Context, rootPath string) error

GoModTidy 在目录下执行 `go mod tidy`。

func HasCmdAndConfigs

func HasCmdAndConfigs(dir string) (bool, bool, error)

HasCmdAndConfigs 检查 dir(为空则使用工作目录)下是否存在 cmd 和 configs 目录。 返回 (hasCmd, hasConfigs, error)。

func IsDirExists

func IsDirExists(dir string) bool

IsDirExists 检查路径是否已存在(三态语义:除“明确不存在”外的任何 stat 结果——含权限错误——都返回 true)。用于“存在即拒绝”守卫, 宁可误报存在也不放过。与 extract 包的两态 isDirExists 语义不同, 不可互换。

func IsFileExists

func IsFileExists(filePath string) bool

IsFileExists 检查路径是否为已存在的普通文件。 与 IsDirExists 不同:任何 stat 错误(含不存在与权限错误)一律返回 false。

func IsValidServiceName

func IsValidServiceName(projectRootPath, serviceName string) (bool, error)

IsValidServiceName 检查 serviceName 是否为 projectRootPath 下的有效服务名称,即 app/{serviceName}/service/cmd/server 存在,并且 app/{serviceName}/configs 目录存在。

func ListServiceNames added in v1.0.2

func ListServiceNames(projectRootPath string) ([]string, error)

ListServiceNames 枚举 projectRootPath 下全部有效服务名称 (app/<name>/service 同时具备 cmd/server 与 configs 者)。无有效服务时返回空切片。

func ModulePath

func ModulePath(filename string) (string, error)

ModulePath returns go module path.

func ReplaceInFile

func ReplaceInFile(path, old, new string) error

ReplaceInFile 读取文件 `path`,将所有 `old` 替换为 `new`,并原子写回。 如果文件内容没有变化,不会修改文件时间。

func ReplaceRegexInFile

func ReplaceRegexInFile(path, pattern, replacement string) error

ReplaceRegexInFile 使用正则 `pattern` 将匹配的部分替换为 `replacement` 并写回。 `pattern` 是普通的 Go 正则表达式。

func ReplaceTemplateInCurrentDir

func ReplaceTemplateInCurrentDir(rootDir, source, target string) (int, error)

ReplaceTemplateInCurrentDir 遍历当前目录及子目录,替换指定的模板字符串。 返回被修改的文件数量和错误(如果有)。

func SplitArgs

func SplitArgs(cmd *cobra.Command, args []string) (cmdArgs, programArgs []string)

func SplitFlagList added in v1.0.2

func SplitFlagList(list []string) []string

SplitFlagList 展开每个元素内的逗号分隔写法("-s a,b" 等价 "-s a -s b")。 供各子命令对 StringArray 旗标做统一展开。

Types

type GoCmd

type GoCmd struct {
	Dir     string        // 工作目录
	Env     []string      // 额外环境变量(追加到 os.Environ())
	Timeout time.Duration // 若 >0,为每次执行设置超时

	Stdin  io.Reader
	Stdout io.Writer // 若为 nil,Run 会使用 os.Stdout
	Stderr io.Writer // 若为 nil,Run 会使用 os.Stderr
}

GoCmd 封装 go 命令执行。

func NewGoCmd

func NewGoCmd(dir string) *GoCmd

NewGoCmd 返回一个默认的 GoCmd(无超时)。

func NewGoCmdWithTimeout

func NewGoCmdWithTimeout(dir string, timeout time.Duration) *GoCmd

NewGoCmdWithTimeout 返回一个带超时的 GoCmd。

func (*GoCmd) CombinedOutput

func (g *GoCmd) CombinedOutput(ctx context.Context, args ...string) ([]byte, error)

CombinedOutput 执行并返回 stdout+stderr。

func (*GoCmd) Output

func (g *GoCmd) Output(ctx context.Context, args ...string) ([]byte, error)

Output 执行并返回 stdout(不包含 stderr)。

func (*GoCmd) Run

func (g *GoCmd) Run(ctx context.Context, args ...string) error

Run 直接执行 go 命令,输出到 GoCmd 指定的 Stdout/Stderr(或终端)。

func (*GoCmd) RunUpwardUntilSucceeds

func (g *GoCmd) RunUpwardUntilSucceeds(ctx context.Context, startDir string, args ...string) ([]byte, error)

RunUpwardUntilSucceeds 从 startDir 开始向上遍历父目录,尝试执行 go 命令, 直到某一目录执行成功或到达根目录。返回最后一次成功的输出(combined)。

type ModuleInspector

type ModuleInspector struct {
	Root    string // 含有 `go.mod` 的目录
	ModPath string // go module path
}

ModuleInspector 保存项目根目录和模块名。

func NewModuleInspectorFromGo

func NewModuleInspectorFromGo(ctx context.Context, startDir string) (*ModuleInspector, error)

NewModuleInspectorFromGo 使用 `go list -m -json` 在 startDir(或当前目录)执行, 返回模块路径和项目根目录。若在 startDir 执行失败,会向上遍历父目录重试。

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

Repo is git repository manager.

func NewRepo

func NewRepo(url string, branch string) *Repo

NewRepo new a repository manager.

func (*Repo) Clone

func (r *Repo) Clone(ctx context.Context) error

Clone clones the repository to cache path.

func (*Repo) CopyTo

func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []string) error

CopyTo copies the repository to project path.

func (*Repo) CopyToV2

func (r *Repo) CopyToV2(ctx context.Context, to string, modPath string, ignores, replaces []string) error

CopyToV2 copies the repository to project path

func (*Repo) Path

func (r *Repo) Path() string

Path returns the repository cache path.

func (*Repo) Pull

func (r *Repo) Pull(ctx context.Context) error

Pull fetch the repository from remote url.

Jump to

Keyboard shortcuts

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