Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ob-branch-1 #34

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ Usage:

Flags:
-b, --branch string checkout branch after cloning
-f, --force remove directory if it already exists
-h, --help help for clone
-p, --path string clone in this directory
-s, --suppress suppress config output for piping to other commands

Global Flags:
--config string config file (default is $HOME/.glabs.yml)
Expand All @@ -252,6 +254,15 @@ Global Flags:

Command line options (`-b` and `-p`) override the config file settings.

With `-s` it is possible to suppress all output but the local path for use in
a pipe. For example

```
glabs clone algdati blatt3 "grp0[35]" -fs | xargs code
```

clones the two repositories for `grp03` and `grp05` and opens them in VS Code.

## Seeding using a custom tool

Instead of providing each student/group the same repository using the startercode option it is possible to run a tool to seed each repository individually.
Expand Down Expand Up @@ -299,7 +310,7 @@ What you can do is the following:
2. Create a new branch. Example:

```
$ git checkout -B ws20
$ git checkout -B ws24
```

3. Commit the whole tree with `commit-tree`. Be sure to remember the the
Expand Down
12 changes: 8 additions & 4 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ var (
if Force {
assignmentConfig.SetForce()
}
assignmentConfig.Show()
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
fmt.Scanln() //nolint:errcheck
if !Suppress {
assignmentConfig.Show()
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
fmt.Scanln() //nolint:errcheck
}

git.Clone(assignmentConfig)
git.Clone(assignmentConfig, Suppress)
},
}
Localpath string
Branch string
Force bool
Suppress 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")
cloneCmd.Flags().BoolVarP(&Suppress, "suppress", "s", false, "suppress config output for piping to other commands")
}
7 changes: 4 additions & 3 deletions config/students.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -58,8 +59,8 @@ func students(per Per, course, assignment string, onlyForStudentsOrGroups ...str
onlyForStudents := make([]string, 0, len(onlyForStudentsOrGroups))
for _, onlyStudent := range onlyForStudentsOrGroups {
for _, student := range studs {
if onlyStudent == student {
onlyForStudents = append(onlyForStudents, onlyStudent)
if ok, err := regexp.MatchString(onlyStudent, student); ok && err == nil {
onlyForStudents = append(onlyForStudents, student)
}
}
}
Expand Down Expand Up @@ -123,7 +124,7 @@ func groups(per Per, course, assignment string, onlyForStudentsOrGroups ...strin
onlyTheseGroups := make(map[string][]string)
for _, onlyGroup := range onlyForStudentsOrGroups {
for groupname, students := range groupsMap {
if onlyGroup == groupname {
if ok, err := regexp.MatchString(onlyGroup, groupname); ok && err == nil {
onlyTheseGroups[groupname] = students
}
}
Expand Down
66 changes: 41 additions & 25 deletions git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/theckman/yacspin"
)

func Clone(cfg *config.AssignmentConfig) {
func Clone(cfg *config.AssignmentConfig, noSpinner bool) {
auth, err := GetAuth()
if err != nil {
fmt.Printf("error: %v", err)
Expand All @@ -26,11 +26,11 @@ func Clone(cfg *config.AssignmentConfig) {
case config.PerStudent:
for _, stud := range cfg.Students {
suffix := cfg.RepoSuffix(stud)
clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth, cfg.Clone.Force)
clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth, cfg.Clone.Force, noSpinner)
}
case config.PerGroup:
for _, grp := range cfg.Groups {
clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth, cfg.Clone.Force)
clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth, cfg.Clone.Force, noSpinner)
}
}
}
Expand All @@ -45,7 +45,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, force bool) {
func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool, noSpinner bool) {
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Expand All @@ -62,49 +62,65 @@ func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool)
StopFailColors: []string{"fgRed"},
}

spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
var spinner *yacspin.Spinner

if !noSpinner {
spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}
}

if force {
spinner.Message(" trying to remove folder if it exists")
if !noSpinner {
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))
if !noSpinner {
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")
err := spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return
}
spinner.Message(" cloning")
if !noSpinner {
spinner.Message(" cloning")
}
}

_, err = git.PlainClone(localpath, false, &git.CloneOptions{
_, err := git.PlainClone(localpath, false, &git.CloneOptions{
Auth: auth,
URL: cloneurl,
ReferenceName: plumbing.ReferenceName("refs/heads/" + branch),
})

if err != nil {
spinner.StopFailMessage(fmt.Sprintf("problem: %v", err))
if !noSpinner {
spinner.StopFailMessage(fmt.Sprintf("problem: %v", err))

err := spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
err := spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
return
}

errs := spinner.Stop()
if errs != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
fmt.Println(localpath)

if !noSpinner {
errs := spinner.Stop()
if errs != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
}
Loading