Skip to content

Commit a48f63d

Browse files
committed
feat(clone): suppress output for piping
1 parent bf9fe36 commit a48f63d

3 files changed

Lines changed: 60 additions & 29 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ Usage:
242242
243243
Flags:
244244
-b, --branch string checkout branch after cloning
245+
-f, --force remove directory if it already exists
245246
-h, --help help for clone
246247
-p, --path string clone in this directory
248+
-s, --suppress suppress config output for piping to other commands
247249
248250
Global Flags:
249251
--config string config file (default is $HOME/.glabs.yml)
@@ -252,6 +254,15 @@ Global Flags:
252254

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

257+
With `-s` it is possible to suppress all output but the local path for use in
258+
a pipe. For example
259+
260+
```
261+
glabs clone algdati blatt3 "grp0[35]" -fs | xargs code
262+
```
263+
264+
clones the two repositories for `grp03` and `grp05` and opens them in VS Code.
265+
255266
## Seeding using a custom tool
256267

257268
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.

cmd/clone.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@ var (
2727
if Force {
2828
assignmentConfig.SetForce()
2929
}
30-
assignmentConfig.Show()
31-
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
32-
fmt.Scanln() //nolint:errcheck
30+
if !Suppress {
31+
assignmentConfig.Show()
32+
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
33+
fmt.Scanln() //nolint:errcheck
34+
}
3335

34-
git.Clone(assignmentConfig)
36+
git.Clone(assignmentConfig, Suppress)
3537
},
3638
}
3739
Localpath string
3840
Branch string
3941
Force bool
42+
Suppress bool
4043
)
4144

4245
func init() {
4346
rootCmd.AddCommand(cloneCmd)
4447
cloneCmd.Flags().StringVarP(&Localpath, "path", "p", "", "clone in this directory")
4548
cloneCmd.Flags().StringVarP(&Branch, "branch", "b", "", "checkout branch after cloning")
4649
cloneCmd.Flags().BoolVarP(&Force, "force", "f", false, "remove directory if it already exists")
50+
cloneCmd.Flags().BoolVarP(&Suppress, "suppress", "s", false, "suppress config output for piping to other commands")
4751
}

git/clone.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/theckman/yacspin"
1616
)
1717

18-
func Clone(cfg *config.AssignmentConfig) {
18+
func Clone(cfg *config.AssignmentConfig, noSpinner bool) {
1919
auth, err := GetAuth()
2020
if err != nil {
2121
fmt.Printf("error: %v", err)
@@ -26,11 +26,11 @@ func Clone(cfg *config.AssignmentConfig) {
2626
case config.PerStudent:
2727
for _, stud := range cfg.Students {
2828
suffix := cfg.RepoSuffix(stud)
29-
clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth, cfg.Clone.Force)
29+
clone(localpath(cfg, suffix), cfg.Clone.Branch, cloneurl(cfg, suffix), auth, cfg.Clone.Force, noSpinner)
3030
}
3131
case config.PerGroup:
3232
for _, grp := range cfg.Groups {
33-
clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth, cfg.Clone.Force)
33+
clone(localpath(cfg, grp.Name), cfg.Clone.Branch, cloneurl(cfg, grp.Name), auth, cfg.Clone.Force, noSpinner)
3434
}
3535
}
3636
}
@@ -45,7 +45,7 @@ func localpath(cfg *config.AssignmentConfig, suffix string) string {
4545
return fmt.Sprintf("%s/%s-%s", cfg.Clone.LocalPath, cfg.Name, suffix)
4646
}
4747

48-
func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool) {
48+
func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool, noSpinner bool) {
4949
cfg := yacspin.Config{
5050
Frequency: 100 * time.Millisecond,
5151
CharSet: yacspin.CharSets[69],
@@ -62,49 +62,65 @@ func clone(localpath, branch, cloneurl string, auth ssh.AuthMethod, force bool)
6262
StopFailColors: []string{"fgRed"},
6363
}
6464

65-
spinner, err := yacspin.New(cfg)
66-
if err != nil {
67-
log.Debug().Err(err).Msg("cannot create spinner")
68-
}
69-
err = spinner.Start()
70-
if err != nil {
71-
log.Debug().Err(err).Msg("cannot start spinner")
65+
var spinner *yacspin.Spinner
66+
67+
if !noSpinner {
68+
spinner, err := yacspin.New(cfg)
69+
if err != nil {
70+
log.Debug().Err(err).Msg("cannot create spinner")
71+
}
72+
err = spinner.Start()
73+
if err != nil {
74+
log.Debug().Err(err).Msg("cannot start spinner")
75+
}
7276
}
7377

7478
if force {
75-
spinner.Message(" trying to remove folder if it exists")
79+
if !noSpinner {
80+
spinner.Message(" trying to remove folder if it exists")
81+
}
7682

7783
err := os.RemoveAll(localpath)
7884
if err != nil {
79-
spinner.StopFailMessage(fmt.Sprintf("error when trying to remove %s: %v", localpath, err))
85+
if !noSpinner {
86+
spinner.StopFailMessage(fmt.Sprintf("error when trying to remove %s: %v", localpath, err))
8087

81-
err := spinner.StopFail()
82-
if err != nil {
83-
log.Debug().Err(err).Msg("cannot stop spinner")
88+
err := spinner.StopFail()
89+
if err != nil {
90+
log.Debug().Err(err).Msg("cannot stop spinner")
91+
}
8492
}
8593
return
8694
}
87-
spinner.Message(" cloning")
95+
if !noSpinner {
96+
spinner.Message(" cloning")
97+
}
8898
}
8999

90-
_, err = git.PlainClone(localpath, false, &git.CloneOptions{
100+
_, err := git.PlainClone(localpath, false, &git.CloneOptions{
91101
Auth: auth,
92102
URL: cloneurl,
93103
ReferenceName: plumbing.ReferenceName("refs/heads/" + branch),
94104
})
95105

96106
if err != nil {
97-
spinner.StopFailMessage(fmt.Sprintf("problem: %v", err))
107+
if !noSpinner {
108+
spinner.StopFailMessage(fmt.Sprintf("problem: %v", err))
98109

99-
err := spinner.StopFail()
100-
if err != nil {
101-
log.Debug().Err(err).Msg("cannot stop spinner")
110+
err := spinner.StopFail()
111+
if err != nil {
112+
log.Debug().Err(err).Msg("cannot stop spinner")
113+
}
102114
}
103115
return
104116
}
105117

106-
errs := spinner.Stop()
107-
if errs != nil {
108-
log.Debug().Err(err).Msg("cannot stop spinner")
118+
fmt.Println(localpath)
119+
120+
if !noSpinner {
121+
errs := spinner.Stop()
122+
if errs != nil {
123+
log.Debug().Err(err).Msg("cannot stop spinner")
124+
}
109125
}
110126
}

0 commit comments

Comments
 (0)