cdglab/cmd/raw.go
cdryzun 60d4290703
All checks were successful
Release / release (push) Successful in 1m34s
add branch arg
2023-12-23 21:28:11 +08:00

60 lines
1.7 KiB
Go

package cmd
import (
"io/ioutil"
"log"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
)
var (
pid string
token string
url string
getFilePath string
outputfilePath string
branch string
)
var rawCmd = &cobra.Command{
Use: "raw",
Short: "Get the content of the specified file in the repository.",
Long: `Get the content of the specified file in the repository and save it locally.`,
Run: func(cmd *cobra.Command, args []string) {
// 创建一个GitLab客户端实例
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(url))
if err != nil {
log.Fatal(err)
}
// 获取仓库中指定文件的内容
file, _, err := client.RepositoryFiles.GetRawFile(pid, getFilePath, &gitlab.GetRawFileOptions{Ref: gitlab.String(branch)})
if err != nil {
log.Fatal(err)
}
// 将内容写入文件
err = ioutil.WriteFile(outputfilePath, []byte(file), 0644)
if err != nil {
log.Fatal(err)
}
},
}
func init() {
rawCmd.Flags().StringVarP(&pid, "project-id", "p", "1", "The project id.")
rawCmd.Flags().StringVarP(&token, "token", "t", "", "The token of gitlab.")
rawCmd.Flags().StringVarP(&url, "url", "u", "", "The url of gitlab.")
rawCmd.Flags().StringVarP(&getFilePath, "getfile-path", "g", "", "The getfile path.")
rawCmd.Flags().StringVarP(&outputfilePath, "outputfile-path", "o", "", "The outputfile path.")
rawCmd.Flags().StringVarP(&branch, "branch", "b", "main", "The project branch of gitlab.")
rawCmd.MarkFlagRequired("project-id")
rawCmd.MarkFlagRequired("token")
rawCmd.MarkFlagRequired("url")
rawCmd.MarkFlagRequired("outputfile-path")
rawCmd.MarkFlagRequired("getfile-path")
rawCmd.MarkFlagRequired("branch")
}