pkg

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractServiceName

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

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

func GoInstall

func GoInstall(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

func IsFileExists

func IsFileExists(filePath string) bool

func IsValidServiceName

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

IsValidServiceName 检查 serviceName 是否为 projectRootPath 下的有效服务名称,即 app/{serviceName}/service/cmd/server 存在,并且 app/{serviceName}/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 Tree

func Tree(path string, dir string)

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(args ...string) ([]byte, error)

CombinedOutput 执行并返回 stdout+stderr。

func (*GoCmd) Output

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

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

func (*GoCmd) Run

func (g *GoCmd) Run(args ...string) error

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

func (*GoCmd) RunUpwardUntilSucceeds

func (g *GoCmd) RunUpwardUntilSucceeds(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(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