mirror of
https://github.com/grdl/git-get.git
synced 2026-02-06 11:02:57 +00:00
Refactor implementations of get and list commands
- Simplify the main functions by moving actual implementation to "pkg" package - Remove the "path" package and move files into the root "pkg" package - Replace viper with explicit Cfg structs
This commit is contained in:
@@ -1,28 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git-get/pkg"
|
||||
"git-get/pkg/cfg"
|
||||
"git-get/pkg/path"
|
||||
"git-get/pkg/print"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cmd = &cobra.Command{
|
||||
Use: "git-list",
|
||||
Short: "git list",
|
||||
Run: Run,
|
||||
Args: cobra.NoArgs,
|
||||
Version: cfg.Version(),
|
||||
Use: "git-list",
|
||||
Short: "git list",
|
||||
RunE: run,
|
||||
Args: cobra.NoArgs,
|
||||
Version: cfg.Version(),
|
||||
SilenceUsage: true,
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
|
||||
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
|
||||
|
||||
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories")
|
||||
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.")
|
||||
|
||||
@@ -32,46 +29,19 @@ func init() {
|
||||
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
||||
}
|
||||
|
||||
func Run(cmd *cobra.Command, args []string) {
|
||||
cfg.InitConfig()
|
||||
func run(cmd *cobra.Command, args []string) error {
|
||||
cfg.Init()
|
||||
|
||||
root := viper.GetString(cfg.KeyReposRoot)
|
||||
|
||||
// TODO: move it to OpenAll and don't export
|
||||
paths, err := path.FindRepos()
|
||||
exitIfError(err)
|
||||
|
||||
repos, err := path.OpenAll(paths)
|
||||
exitIfError(err)
|
||||
|
||||
var printer print.Printer
|
||||
switch viper.GetString(cfg.KeyOutput) {
|
||||
case cfg.OutFlat:
|
||||
printer = &print.FlatPrinter{}
|
||||
case cfg.OutTree:
|
||||
printer = &print.SimpleTreePrinter{}
|
||||
case cfg.OutSmart:
|
||||
printer = &print.SmartTreePrinter{}
|
||||
case cfg.OutDump:
|
||||
printer = &print.DumpPrinter{}
|
||||
default:
|
||||
err = fmt.Errorf("invalid --out flag; allowed values: %v", []string{cfg.OutFlat, cfg.OutTree, cfg.OutSmart})
|
||||
config := &pkg.ListCfg{
|
||||
Fetch: viper.GetBool(cfg.KeyFetch),
|
||||
Output: viper.GetString(cfg.KeyOutput),
|
||||
PrivateKey: viper.GetString(cfg.KeyPrivateKey),
|
||||
Root: viper.GetString(cfg.KeyReposRoot),
|
||||
}
|
||||
exitIfError(err)
|
||||
|
||||
fmt.Println(printer.Print(root, repos))
|
||||
return pkg.List(config)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
cmd.Execute()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user