golang多仓库管理CLI工具插件mani的使用

Golang多仓库管理CLI工具插件mani的使用

概述

mani是一个帮助您管理多个仓库的CLI工具。当您处理微服务、多项目系统、多个库或只是一组仓库集合时,它非常有用,可以提供一个集中位置来拉取所有仓库并在它们之间运行命令。

demo

主要特性

  • 声明式配置
  • 使用单个命令克隆多个仓库
  • 在多个仓库上运行自定义或临时命令
  • 内置TUI界面
  • 灵活的过滤功能
  • 可自定义的主题
  • 自动补全支持
  • 便携,无依赖

安装

mani支持Linux和Mac,部分支持Windows。

安装方式

  1. 通过cURL安装(Linux & macOS):
curl -sfL https://raw.githubusercontent.com/alajmo/mani/main/install.sh | sh
  1. 通过Homebrew安装:
brew tap alajmo/mani
brew install mani
  1. 通过MacPorts安装:
sudo port install mani
  1. 通过Arch(AUR)安装:
yay -S mani
  1. 通过Nix安装:
nix-env -iA nixos.mani
  1. 通过Go安装:
go get -u github.com/alajmo/mani

自动补全

可以通过以下命令启用自动补全:

mani completion bash|zsh|fish|powershell

使用示例

初始化mani仓库

在包含您的git仓库的目录中运行以下命令:

$ mani init

这将生成两个文件:

  • mani.yaml: 包含项目和自定义任务
  • .gitignore: 包含在mani.yaml文件中指定的项目

常用命令

# 列出所有项目
$ mani list projects

# 并行计算每个项目中的文件数量
$ mani exec --all --output table --parallel 'find . -type f | wc -l'

# 启动TUI界面
$ mani tui

配置文件示例

以下是一个mani.yaml配置文件的示例:

# mani.yaml 示例配置

projects:
  - name: project1
    url: git@github.com:user/project1.git
    path: ~/code/project1
    tags: [backend, api]
  
  - name: project2
    url: git@github.com:user/project2.git
    path: ~/code/project2
    tags: [frontend, ui]

tasks:
  - name: status
    desc: Get git status of all projects
    cmd: git status -s
    tags: [git]

  - name: update
    desc: Pull latest changes for all projects
    cmd: git pull
    tags: [git]

运行自定义任务

# 运行名为"status"的任务
$ mani run status

# 只对标记为"backend"的项目运行任务
$ mani run status --tags backend

# 并行运行任务
$ mani run update --parallel

高级用法

过滤项目

# 只对名称包含"api"的项目运行命令
$ mani exec --name api 'git log -1 --oneline'

# 对路径包含"services"的项目运行命令
$ mani exec --path services 'git fetch'

变量替换

在命令中使用项目变量:

tasks:
  - name: clone
    desc: Clone project if not exists
    cmd: |
      if [ ! -d "{{.Path}}" ]; then
        git clone {{.Url}} {{.Path}}
      fi

总结

mani是一个强大的多仓库管理工具,特别适合需要同时处理多个Git仓库的开发人员。它提供了简洁的命令行界面和灵活的配置选项,可以大大提高多仓库工作流程的效率。


更多关于golang多仓库管理CLI工具插件mani的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang多仓库管理CLI工具插件mani的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


mani - Golang多仓库管理CLI工具

mani 是一个用Go编写的多仓库管理工具,它允许你同时操作多个Git仓库,非常适合管理微服务架构或包含多个子模块的项目。

安装mani

go install github.com/alajmo/mani@latest

或者使用brew安装:

brew install mani

基本使用

1. 初始化mani配置

首先创建一个mani.yaml配置文件:

# mani.yaml 示例
projects:
  - name: api-service
    url: git@github.com:yourorg/api-service.git
    path: ~/code/api-service
    tags: [backend, golang]
  
  - name: web-app
    url: git@github.com:yourorg/web-app.git
    path: ~/code/web-app
    tags: [frontend, react]
  
  - name: mobile-app
    url: git@github.com:yourorg/mobile-app.git
    path: ~/code/mobile-app
    tags: [mobile, flutter]

2. 常用命令

克隆所有仓库

mani clone

在所有仓库中执行命令

mani run -- git status

只对特定标签的仓库执行命令

mani run --tags backend -- go mod tidy

同步所有仓库

mani sync

高级功能

自定义任务

你可以在mani.yaml中定义自定义任务:

tasks:
  - name: update-deps
    description: Update dependencies in all projects
    cmd: |
      if [ -f "go.mod" ]; then
        go get -u ./...
        go mod tidy
      fi
    tags: [golang]

然后运行:

mani task update-deps

过滤项目

# 按名称过滤
mani run --projects api-service,web-app -- git pull

# 按标签过滤
mani run --tags frontend -- npm install

Go代码示例

如果你想在自己的Go程序中集成mani的功能,可以使用以下示例:

package main

import (
	"fmt"
	"os/exec"
)

func runManiCommand(args ...string) error {
	cmd := exec.Command("mani", args...)
	output, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("mani command failed: %v\nOutput: %s", err, string(output))
	}
	fmt.Println(string(output))
	return nil
}

func main() {
	// 示例:在所有Go项目中运行go mod tidy
	err := runManiCommand("run", "--tags", "golang", "--", "go", "mod", "tidy")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
	}
}

最佳实践

  1. 组织项目:使用标签(tags)对项目进行分类
  2. 定期同步:设置cron作业定期运行mani sync
  3. 批量操作:利用mani run执行重复性任务
  4. 自定义任务:将常用操作定义为任务
  5. CI/CD集成:在CI/CD流程中使用mani管理多仓库构建

mani通过简化多仓库管理,显著提高了开发效率,特别是对于微服务架构或大型单体仓库拆分为多个小仓库的情况。

回到顶部