diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 95e53d6..7cbcf65 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ repos: - repo: https://github.com/golangci/golangci-lint.git - rev: v1.31.0 + rev: v1.32.1 hooks: - id: golangci-lint diff --git a/README.md b/README.md index 689d4fb..b40be80 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Contents: clone: localpath: # default "." branch: # default master + force: # remove directory if it exists. default false ``` Example: @@ -99,6 +100,7 @@ algdati: clone: localpath: /tmp branch: develop + clone: true ``` ## Usage diff --git a/cmd/clone.go b/cmd/clone.go index bc3795b..2f5fa34 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -24,6 +24,9 @@ var ( if len(Localpath) > 0 { assignmentConfig.SetLocalpath(Localpath) } + if Force { + assignmentConfig.SetForce() + } assignmentConfig.Show() fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ...")) fmt.Scanln() @@ -33,10 +36,12 @@ var ( } Localpath string Branch string + Force bool ) func init() { rootCmd.AddCommand(cloneCmd) cloneCmd.Flags().StringVarP(&Localpath, "path", "p", "", "clone in this directory") cloneCmd.Flags().StringVarP(&Branch, "branch", "b", "", "checkout branch after cloning") + cloneCmd.Flags().BoolVarP(&Force, "force", "f", false, "remove directory if it already exists") } diff --git a/config/assignment.go b/config/assignment.go index c3836b8..c2bd166 100644 --- a/config/assignment.go +++ b/config/assignment.go @@ -40,6 +40,7 @@ type Startercode struct { type Clone struct { LocalPath string Branch string + Force bool } type Group struct { @@ -257,9 +258,12 @@ func clone(assignmentKey string) *Clone { branch = "master" } + force := viper.GetBool(assignmentKey + ".clone.force") + return &Clone{ LocalPath: localpath, Branch: branch, + Force: force, } } @@ -270,3 +274,7 @@ func (cfg *AssignmentConfig) SetBranch(branch string) { func (cfg *AssignmentConfig) SetLocalpath(localpath string) { cfg.Clone.LocalPath = localpath } + +func (cfg *AssignmentConfig) SetForce() { + cfg.Clone.Force = true +} diff --git a/config/show.go b/config/show.go index 8a5f78f..f7b0990 100644 --- a/config/show.go +++ b/config/show.go @@ -31,9 +31,11 @@ func (cfg *AssignmentConfig) Show() { if cfg.Clone != nil { clone = aurora.Sprintf(aurora.Cyan(`Clone: Localpath: %s - Branch: %s`), + Branch: %s + Force: %t`), aurora.Yellow(cfg.Clone.LocalPath), aurora.Yellow(cfg.Clone.Branch), + aurora.Yellow(cfg.Clone.Force), ) } diff --git a/git/clone.go b/git/clone.go index a312b39..12dae53 100644 --- a/git/clone.go +++ b/git/clone.go @@ -2,6 +2,7 @@ package git import ( "fmt" + "os" "strings" "time" @@ -24,11 +25,11 @@ func Clone(cfg *config.AssignmentConfig) { switch cfg.Per { case config.PerStudent: for _, suffix := range cfg.Students { - clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth) + clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth, cfg.Clone.Force) } case config.PerGroup: for _, grp := range cfg.Groups { - clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth) + clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth, cfg.Clone.Force) } } } @@ -43,7 +44,7 @@ func localpath(cfg *config.AssignmentConfig, suffix string) string { return fmt.Sprintf("%s/%s-%s", cfg.Clone.LocalPath, cfg.Name, suffix) } -func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod) { +func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool) { cfg := yacspin.Config{ Frequency: 100 * time.Millisecond, CharSet: yacspin.CharSets[69], @@ -69,6 +70,22 @@ func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod) { log.Debug().Err(err).Msg("cannot start spinner") } + if force { + spinner.Message(" trying to remove folder if it exists") + + err := os.RemoveAll(localpath) + if err != nil { + spinner.StopFailMessage(fmt.Sprintf("error when trying to remove %s: %v", localpath, err)) + + err := spinner.StopFail() + if err != nil { + log.Debug().Err(err).Msg("cannot stop spinner") + } + return + } + spinner.Message(" cloning") + } + _, err = git.PlainClone(localpath, false, &git.CloneOptions{ Auth: auth, URL: cloneurl,