Documentation
¶
Index ¶
- func ExtractProjectName(module string) string
- func ExtractServiceName(projectRootPath, currentDir string) (string, error)
- func GoInstall(ctx context.Context, paths ...string) error
- func GoModTidy(ctx context.Context, rootPath string) error
- func HasCmdAndConfigs(dir string) (bool, bool, error)
- func IsDirExists(dir string) bool
- func IsFileExists(filePath string) bool
- func IsValidServiceName(projectRootPath, serviceName string) (bool, error)
- func ListServiceNames(projectRootPath string) ([]string, error)
- func ModulePath(filename string) (string, error)
- func ParseVCSUrl(repo string) (*url.URL, error)
- func ReplaceInFile(path, old, new string) error
- func ReplaceRegexInFile(path, pattern, replacement string) error
- func ReplaceTemplateInCurrentDir(rootDir, source, target string) (int, error)
- func SplitArgs(cmd *cobra.Command, args []string) (cmdArgs, programArgs []string)
- func SplitFlagList(list []string) []string
- type GoCmd
- func (g *GoCmd) CombinedOutput(ctx context.Context, args ...string) ([]byte, error)
- func (g *GoCmd) Output(ctx context.Context, args ...string) ([]byte, error)
- func (g *GoCmd) Run(ctx context.Context, args ...string) error
- func (g *GoCmd) RunUpwardUntilSucceeds(ctx context.Context, startDir string, args ...string) ([]byte, error)
- func (g *GoCmd) RunUpwardUntilValid(ctx context.Context, startDir string, valid func(out []byte) bool, ...) ([]byte, error)
- type ModuleInspector
- type Repo
- func (r *Repo) Clone(ctx context.Context) error
- func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores []string) error
- func (r *Repo) CopyToV2(ctx context.Context, to string, modPath string, ignores, replaces []string) error
- func (r *Repo) Path() string
- func (r *Repo) Pull(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractProjectName ¶ added in v1.0.2
ExtractProjectName 从 Go module 路径中提取项目名(路径最后一段非空片段)。
func ExtractServiceName ¶
ExtractServiceName 从 currentDir 中提取服务名称,前提是 currentDir 位于 projectRootPath 之下,并且路径结构符合 app/{service}/service/cmd/server。
func HasCmdAndConfigs ¶
HasCmdAndConfigs 检查 dir(为空则使用工作目录)下是否存在 cmd 和 configs 目录。 返回 (hasCmd, hasConfigs, error)。
func IsDirExists ¶
IsDirExists 检查路径是否已存在(三态语义:除“明确不存在”外的任何 stat 结果——含权限错误——都返回 true)。用于“存在即拒绝”守卫, 宁可误报存在也不放过。与 extract 包的两态 isDirExists 语义不同, 不可互换。
func IsFileExists ¶
IsFileExists 检查路径是否为已存在的普通文件。 与 IsDirExists 不同:任何 stat 错误(含不存在与权限错误)一律返回 false。
func IsValidServiceName ¶
IsValidServiceName 检查 serviceName 是否为 projectRootPath 下的有效服务名称,即 app/{serviceName}/service/cmd/server 存在,并且 app/{serviceName}/configs 目录存在。
func ListServiceNames ¶ added in v1.0.2
ListServiceNames 枚举 projectRootPath 下全部有效服务名称 (app/<name>/service 同时具备 cmd/server 与 configs 者)。无有效服务时返回空切片。
func ModulePath ¶
ModulePath returns go module path.
func ParseVCSUrl ¶
ParseVCSUrl ref https://github.com/golang/go/blob/master/src/cmd/go/internal/vcs/vcs.go see https://go-review.googlesource.com/c/go/+/12226/ git url define https://git-scm.com/docs/git-clone#_git_urls
func ReplaceInFile ¶
ReplaceInFile 读取文件 `path`,将所有 `old` 替换为 `new`,并原子写回。 如果文件内容没有变化,不会修改文件时间。
func ReplaceRegexInFile ¶
ReplaceRegexInFile 使用正则 `pattern` 将匹配的部分替换为 `replacement` 并写回。 `pattern` 是普通的 Go 正则表达式。
func ReplaceTemplateInCurrentDir ¶
ReplaceTemplateInCurrentDir 遍历当前目录及子目录,替换指定的模板字符串。 返回被修改的文件数量和错误(如果有)。
func SplitFlagList ¶ added in v1.0.2
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 NewGoCmdWithTimeout ¶
NewGoCmdWithTimeout 返回一个带超时的 GoCmd。
func (*GoCmd) CombinedOutput ¶
CombinedOutput 执行并返回 stdout+stderr。
func (*GoCmd) RunUpwardUntilSucceeds ¶
func (g *GoCmd) RunUpwardUntilSucceeds(ctx context.Context, startDir string, args ...string) ([]byte, error)
RunUpwardUntilSucceeds 从 startDir 开始向上遍历父目录,尝试执行 go 命令, 直到某一目录执行成功或到达根目录。返回最后一次成功的输出(combined)。
func (*GoCmd) RunUpwardUntilValid ¶ added in v1.0.4
func (g *GoCmd) RunUpwardUntilValid(ctx context.Context, startDir string, valid func(out []byte) bool, args ...string) ([]byte, error)
RunUpwardUntilValid 从 startDir 向上遍历父目录执行 go 命令。 valid 为 nil 时,以进程退出码为准(等同 RunUpwardUntilSucceeds); 否则仅当退出码为 0 且 valid(out) 为真才算命中——用于甄别 `go list -m -json` 在无 go.mod 目录仍返回 exit 0 的伪主模块(Path=command-line-arguments)。 全部目录都不命中时返回描述性错误,而非伪结果。
type ModuleInspector ¶
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.