golang自动为go.mod依赖库点赞的插件库gothanks的使用

Golang自动为go.mod依赖库点赞的插件库gothanks的使用

概述

gothanks工具截图

GoThanks是一个自动为您的Go模块依赖库点赞(GitHub加星)的工具,主要功能包括:

  • 为Go官方仓库(github.com/golang/go)加星
  • 读取您的go.mod文件并为所有依赖的GitHub仓库加星

这是向您使用的模块维护者和Go贡献者表示感谢的一种简单方式。

安装

# 使用go get安装
$ go get -u github.com/psampaz/gothanks

使用说明

您需要一个有效的GitHub访问令牌(具有public_repo权限)。

可以将令牌作为参数传递给GoThanks或存储在名为GITHUB_TOKEN的环境变量中。

在您的go.mod文件所在目录运行:

# 方式1:直接传递token参数
$ ./gothanks -github-token=xxxxxx

# 方式2:使用环境变量
$ export GITHUB_TOKEN=xxxxx
$ ./gothanks

Docker方式

# 方式1:直接传递token参数
docker run -it --rm -v $(pwd):/home psampaz/gothanks:latest -github-token=xxxxxx

# 方式2:使用环境变量
docker run -it --rm -v $(pwd):/home --env GITHUB_TOKEN=xxxxxx psampaz/gothanks:latest

示例

$ ./gothanks
Welcome to GoThanks :)

You are about to star you beloved dependencies.

Press y to continue or n to abort
y

Sending your love..

Repository github.com/golang/go is already starred!
Repository github.com/golang/net is already starred!
Sending a star to github.com/google/go-github
Sending a star to github.com/stretchr/testify
Sending a star to github.com/sirupsen/logrus

Thank you!

对于非交互式环境,可以使用-y标志:

$ ./gothanks -y
Welcome to GoThanks :)

Sending your love..

Repository github.com/golang/go is already starred!
Repository github.com/google/go-github is already starred!
Repository github.com/sirkon/goproxy is already starred!
Repository github.com/golang/crypto is already starred!
Repository github.com/golang/net is already starred!
Repository github.com/golang/oauth2 is already starred!

Thank you!

技术来源

GoThanks是Nicolas Grekas (nicolas-grekas)为PHP实现的symfony/thanks的Go版本移植。

原始创意来自Doug Tangren (softprops)为Rust实现的版本。


更多关于golang自动为go.mod依赖库点赞的插件库gothanks的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang自动为go.mod依赖库点赞的插件库gothanks的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用 gothanks 为 Go 依赖库点赞

gothanks 是一个 Go 语言的工具,它可以自动为你项目 go.mod 文件中列出的所有依赖库在 GitHub 上点赞(star)。这是一个表达对开源项目感谢的好方式。

安装 gothanks

go install github.com/psampaz/gothanks@latest

使用说明

基本使用

在你的 Go 项目目录下运行:

gothanks

这会扫描当前目录下的 go.mod 文件,并为你列出的所有依赖库点赞。

选项参数

  • -dry-run: 只显示将要点赞的仓库,不实际执行点赞
  • -exclude: 排除特定的仓库(支持正则表达式)
  • -token: 指定 GitHub 个人访问令牌
gothanks -dry-run
gothanks -exclude "github.com/user/repo"
gothanks -token your_github_token

获取 GitHub 访问令牌

  1. 登录 GitHub
  2. 访问 Personal Access Tokens
  3. 点击 “Generate new token”
  4. 选择 “public_repo” 权限
  5. 生成并复制令牌

示例代码

如果你想在自己的 Go 程序中集成 gothanks 的功能,可以参考以下代码:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/google/go-github/v50/github"
	"golang.org/x/oauth2"
)

func starRepos(token string, repos []string) error {
	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(ctx, ts)
	client := github.NewClient(tc)

	for _, repo := range repos {
		owner, name, err := parseRepo(repo)
		if err != nil {
			return fmt.Errorf("error parsing repo %s: %v", repo, err)
		}

		_, err = client.Activity.Star(ctx, owner, name)
		if err != nil {
			return fmt.Errorf("error starring %s: %v", repo, err)
		}
		fmt.Printf("Starred %s/%s\n", owner, name)
	}

	return nil
}

func parseRepo(repo string) (string, string, error) {
	// 这里需要实现解析仓库URL或owner/repo格式的逻辑
	// 例如从 "github.com/owner/repo" 中提取 owner 和 repo
	return "owner", "repo", nil
}

func main() {
	token := "your_github_token"
	repos := []string{
		"github.com/gin-gonic/gin",
		"github.com/spf13/cobra",
		// 添加更多仓库
	}

	if err := starRepos(token, repos); err != nil {
		log.Fatal(err)
	}
}

注意事项

  1. 使用 GitHub API 有速率限制,gothanks 会自动处理这一点
  2. 确保你的 token 有正确的权限
  3. 不要滥用此工具,建议只为你真正使用并感谢的库点赞
  4. 对于私有仓库,需要额外权限

gothanks 是一个简单但实用的工具,它帮助开发者以自动化的方式表达对开源社区的感谢。通过为依赖库点赞,你不仅表达了感谢,也帮助这些项目获得更多关注。

回到顶部