From ced9c711de6f3b4af80bb15f7c48743c7d83c9a2 Mon Sep 17 00:00:00 2001 From: emanuele francioni Date: Fri, 21 Feb 2020 17:58:08 +0100 Subject: [PATCH] add flag to download to force fetch files if needed --- cmd/download.go | 77 +++++++++++++++++++++++++++++++++---------------- cmd/install.go | 4 +-- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/cmd/download.go b/cmd/download.go index 007e64d..70fbf5c 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -27,33 +27,11 @@ var downloadCmd = &cobra.Command{ Use: "download", Short: "update your local repository with all changes performed remotely", RunE: func(cmd *cobra.Command, args []string) error { - cfg := &Conf{} - if err := viper.Unmarshal(cfg); err != nil { - return err - } - - projects := make([]string, 0) - for _, repo := range cfg.Repos { - projects = append(projects, repo.Name) - } - - prompt := promptui.Select{ - Label: "select which project you want to sync", - Items: projects, - } - - i, _, err := prompt.Run() + ft, err := cmd.PersistentFlags().GetBool("fetch-tracks") if err != nil { return err } - - out, err := common.ExecCmd("git", "-C", cfg.Repos[i].Location, "pull", "origin", "master") - if err != nil { - return err - } - - Print(out) - return nil + return pull(ft) }, } @@ -64,9 +42,58 @@ func init() { // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: - // downloadCmd.PersistentFlags().String("foo", "", "A help for foo") + downloadCmd.PersistentFlags().BoolP("fetch-tracks", "f", false, "force git-lfs to fetch the actual tracks instead of the git-lfs links") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // downloadCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } + +func pull(fetchTracks bool) error { + cfg := &Conf{} + if err := viper.Unmarshal(cfg); err != nil { + return err + } + + repo, err := selectLocalRepo(cfg) + if err != nil { + return err + } + + out, err := common.ExecCmd("git", "-C", repo, "pull", "origin", "master") + if err != nil { + return err + } + + Print(out) + + if fetchTracks { + out, err := common.ExecCmd("git", "-C", repo, "lfs", "pull") + if err != nil { + return err + } + + Print(out) + } + return nil +} + +func selectLocalRepo(cfg *Conf) (string, error) { + + projects := make([]string, 0) + for _, repo := range cfg.Repos { + projects = append(projects, repo.Name) + } + + prompt := promptui.Select{ + Label: "select which project you want to sync", + Items: projects, + } + + i, _, err := prompt.Run() + if err != nil { + return "", err + } + + return cfg.Repos[i].Location, nil +} diff --git a/cmd/install.go b/cmd/install.go index d919205..680a549 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -69,7 +69,7 @@ var installCmd = &cobra.Command{ if err := viper.Unmarshal(cfg); err != nil { return err } - remoteRepo, err := selectRepo(sharedDir, cfg) + remoteRepo, err := selectRemoteRepo(sharedDir, cfg) if err != nil { return err } @@ -143,7 +143,7 @@ func execGit(localRepo string, args ...string) error { return nil } -func selectRepo(sharedDir string, conf *Conf) (string, error) { +func selectRemoteRepo(sharedDir string, conf *Conf) (string, error) { projects := make([]string, 0) files, err := ioutil.ReadDir(sharedDir) if err != nil {