Skip to content

Commit

Permalink
Merge branch 'release/v0.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Oct 31, 2020
2 parents bf6679d + b3c3562 commit 5aa1bbc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Contents:
clone:
localpath: <local base path for repositories to clone in> # default "."
branch: <checkout branch> # default master
force: <false|true> # remove directory if it exists. default false
```

Example:
Expand Down Expand Up @@ -99,6 +100,7 @@ algdati:
clone:
localpath: /tmp
branch: develop
clone: true
```

## Usage
Expand Down
5 changes: 5 additions & 0 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
}
8 changes: 8 additions & 0 deletions config/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Startercode struct {
type Clone struct {
LocalPath string
Branch string
Force bool
}

type Group struct {
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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
}
4 changes: 3 additions & 1 deletion config/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

Expand Down
23 changes: 20 additions & 3 deletions git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"fmt"
"os"
"strings"
"time"

Expand All @@ -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)
}
}
}
Expand All @@ -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],
Expand All @@ -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,
Expand Down

0 comments on commit 5aa1bbc

Please sign in to comment.