This commit is contained in:
55
cmd/raw.go
Normal file
55
cmd/raw.go
Normal file
@ -0,0 +1,55 @@
|
||||
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
|
||||
)
|
||||
|
||||
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, nil)
|
||||
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.MarkFlagRequired("project-id")
|
||||
rawCmd.MarkFlagRequired("token")
|
||||
rawCmd.MarkFlagRequired("url")
|
||||
rawCmd.MarkFlagRequired("outputfile-path")
|
||||
rawCmd.MarkFlagRequired("getfile-path")
|
||||
}
|
30
cmd/root.go
Normal file
30
cmd/root.go
Normal file
@ -0,0 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "cdglab",
|
||||
Short: "cdglab is a tool for gitlab CI-CD.",
|
||||
Long: `cdglab is a basic tool for Gitlab CI/CD, used to encapsulate a series of RESTful API calls.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Execute the command cdglab -h to view the usage method.")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand()
|
||||
rootCmd.AddCommand(rawCmd)
|
||||
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user