mirror of
https://github.com/grdl/git-get.git
synced 2026-02-16 16:40:17 +00:00
Split get and list commands into separate binaries
This commit is contained in:
@@ -3,6 +3,18 @@ before:
|
|||||||
- go mod download
|
- go mod download
|
||||||
|
|
||||||
builds:
|
builds:
|
||||||
|
- id: git-get
|
||||||
|
main: ./cmd/get/main.go
|
||||||
|
binary: git-get
|
||||||
|
ldflags:
|
||||||
|
- -s -w
|
||||||
|
- -X git-get/pkg/cfg.version={{.Version}}
|
||||||
|
- -X git-get/pkg/cfg.commit={{.Commit}}
|
||||||
|
- -X git-get/pkg/cfg.date={{.Date}}
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- darwin
|
||||||
|
- windows
|
||||||
- id: git-list
|
- id: git-list
|
||||||
main: ./cmd/list/main.go
|
main: ./cmd/list/main.go
|
||||||
binary: git-list
|
binary: git-list
|
||||||
@@ -19,6 +31,7 @@ builds:
|
|||||||
archives:
|
archives:
|
||||||
- id: archive
|
- id: archive
|
||||||
builds:
|
builds:
|
||||||
|
- git-get
|
||||||
- git-list
|
- git-list
|
||||||
replacements:
|
replacements:
|
||||||
darwin: macOS
|
darwin: macOS
|
||||||
|
|||||||
81
cmd/get/main.go
Normal file
81
cmd/get/main.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git-get/pkg/cfg"
|
||||||
|
"git-get/pkg/git"
|
||||||
|
"git-get/pkg/path"
|
||||||
|
"os"
|
||||||
|
pathpkg "path"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cmd = &cobra.Command{
|
||||||
|
Use: "git-get <repo>",
|
||||||
|
Short: "git get",
|
||||||
|
Run: Run,
|
||||||
|
Args: cobra.MaximumNArgs(1), // TODO: add custom validator
|
||||||
|
Version: cfg.Version(),
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
|
||||||
|
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
|
||||||
|
cmd.PersistentFlags().StringP(cfg.KeyBundle, "u", "", "Bundle file path")
|
||||||
|
|
||||||
|
cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", cfg.DefBranch, "Branch (or tag) to checkout after cloning")
|
||||||
|
|
||||||
|
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
|
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
|
viper.BindPFlag(cfg.KeyBundle, cmd.PersistentFlags().Lookup(cfg.KeyBundle))
|
||||||
|
viper.BindPFlag(cfg.KeyBranch, cmd.PersistentFlags().Lookup(cfg.KeyBranch))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(cmd *cobra.Command, args []string) {
|
||||||
|
cfg.InitConfig()
|
||||||
|
|
||||||
|
root := viper.GetString(cfg.KeyReposRoot)
|
||||||
|
|
||||||
|
if bundle := viper.GetString(cfg.KeyBundle); bundle != "" {
|
||||||
|
opts, err := path.ParseBundleFile(bundle)
|
||||||
|
exitIfError(err)
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
path := pathpkg.Join(root, path.URLToPath(opt.URL))
|
||||||
|
opt.Path = path
|
||||||
|
_, _ = git.CloneRepo(opt)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
url, err := path.ParseURL(args[0])
|
||||||
|
exitIfError(err)
|
||||||
|
|
||||||
|
branch := viper.GetString(cfg.KeyBranch)
|
||||||
|
path := pathpkg.Join(root, path.URLToPath(url))
|
||||||
|
|
||||||
|
cloneOpts := &git.CloneOpts{
|
||||||
|
URL: url,
|
||||||
|
Path: path,
|
||||||
|
Branch: branch,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = git.CloneRepo(cloneOpts)
|
||||||
|
exitIfError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := cmd.Execute(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exitIfError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,47 +3,42 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/cfg"
|
"git-get/pkg/cfg"
|
||||||
"git-get/pkg/git"
|
|
||||||
"git-get/pkg/path"
|
"git-get/pkg/path"
|
||||||
"git-get/pkg/print"
|
"git-get/pkg/print"
|
||||||
"os"
|
"os"
|
||||||
pathpkg "path"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd = &cobra.Command{
|
var cmd = &cobra.Command{
|
||||||
Use: "git-get <repo>",
|
Use: "git-list",
|
||||||
Short: "git get",
|
Short: "git list",
|
||||||
Run: Run,
|
Run: Run,
|
||||||
Args: cobra.MaximumNArgs(1), // TODO: add custom validator
|
Args: cobra.NoArgs,
|
||||||
Version: cfg.Version(),
|
Version: cfg.Version(),
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd.PersistentFlags().BoolP(cfg.KeyList, "l", false, "Lists all repositories inside git-get root")
|
|
||||||
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories")
|
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
|
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
|
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.")
|
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", cfg.DefBranch, "Branch (or tag) to checkout after cloning")
|
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyBundle, "u", "", "Bundle file path")
|
cmd.PersistentFlags().StringP(cfg.KeyBundle, "u", "", "Bundle file path")
|
||||||
|
|
||||||
viper.BindPFlag(cfg.KeyList, cmd.PersistentFlags().Lookup(cfg.KeyList))
|
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories")
|
||||||
viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch))
|
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.")
|
||||||
|
|
||||||
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
|
||||||
viper.BindPFlag(cfg.KeyBranch, cmd.PersistentFlags().Lookup(cfg.KeyBranch))
|
|
||||||
viper.BindPFlag(cfg.KeyBundle, cmd.PersistentFlags().Lookup(cfg.KeyBundle))
|
viper.BindPFlag(cfg.KeyBundle, cmd.PersistentFlags().Lookup(cfg.KeyBundle))
|
||||||
|
viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch))
|
||||||
|
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(cmd *cobra.Command, args []string) {
|
func Run(cmd *cobra.Command, args []string) {
|
||||||
cfg.InitConfig()
|
cfg.InitConfig()
|
||||||
|
|
||||||
root := viper.GetString(cfg.KeyReposRoot)
|
root := viper.GetString(cfg.KeyReposRoot)
|
||||||
if viper.GetBool(cfg.KeyList) {
|
|
||||||
// TODO: move it to OpenAll and don't export
|
// TODO: move it to OpenAll and don't export
|
||||||
paths, err := path.FindRepos()
|
paths, err := path.FindRepos()
|
||||||
exitIfError(err)
|
exitIfError(err)
|
||||||
@@ -65,42 +60,13 @@ func Run(cmd *cobra.Command, args []string) {
|
|||||||
exitIfError(err)
|
exitIfError(err)
|
||||||
|
|
||||||
fmt.Println(printer.Print(root, repos))
|
fmt.Println(printer.Print(root, repos))
|
||||||
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
bundle := viper.GetString(cfg.KeyBundle)
|
|
||||||
if bundle != "" {
|
|
||||||
opts, err := path.ParseBundleFile(bundle)
|
|
||||||
exitIfError(err)
|
|
||||||
|
|
||||||
for _, opt := range opts {
|
|
||||||
path := pathpkg.Join(root, path.URLToPath(opt.URL))
|
|
||||||
opt.Path = path
|
|
||||||
_, _ = git.CloneRepo(opt)
|
|
||||||
}
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
url, err := path.ParseURL(args[0])
|
|
||||||
exitIfError(err)
|
|
||||||
|
|
||||||
branch := viper.GetString(cfg.KeyBranch)
|
|
||||||
path := pathpkg.Join(root, path.URLToPath(url))
|
|
||||||
|
|
||||||
cloneOpts := &git.CloneOpts{
|
|
||||||
URL: url,
|
|
||||||
Path: path,
|
|
||||||
Branch: branch,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = git.CloneRepo(cloneOpts)
|
|
||||||
exitIfError(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := cmd.Execute()
|
if err := cmd.Execute(); err != nil {
|
||||||
exitIfError(err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func exitIfError(err error) {
|
func exitIfError(err error) {
|
||||||
|
|||||||
@@ -22,9 +22,8 @@ const (
|
|||||||
KeyDefaultHost = "defaultHost"
|
KeyDefaultHost = "defaultHost"
|
||||||
DefDefaultHost = "github.com"
|
DefDefaultHost = "github.com"
|
||||||
KeyFetch = "fetch"
|
KeyFetch = "fetch"
|
||||||
KeyList = "list"
|
|
||||||
KeyOutput = "out"
|
KeyOutput = "out"
|
||||||
DefOutput = OutFlat
|
DefOutput = OutSimple
|
||||||
KeyPrivateKey = "privateKey"
|
KeyPrivateKey = "privateKey"
|
||||||
DefPrivateKey = "id_rsa"
|
DefPrivateKey = "id_rsa"
|
||||||
KeyReposRoot = "reposRoot"
|
KeyReposRoot = "reposRoot"
|
||||||
|
|||||||
Reference in New Issue
Block a user