Skip to content

Commit

Permalink
Merge branch 'release/v0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Oct 22, 2020
2 parents 0c6a56c + b49609f commit d86fb4e
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Available Commands:
check check course config
generate Generate repositories for each student.
help Help about any command
show-config Show config of a course
show Show config of an assignment
version Print the version number of Glabs
Flags:
Expand Down
22 changes: 0 additions & 22 deletions cmd/config.go

This file was deleted.

6 changes: 6 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"

"github.com/gookit/color"
"github.com/obcode/glabs/config"
"github.com/obcode/glabs/gitlab"
"github.com/spf13/cobra"
Expand All @@ -19,6 +22,9 @@ A student needs to exist on GitLab, a group needs to exist in the configuration
Args: cobra.MinimumNArgs(2), //nolint:gomnd
Run: func(cmd *cobra.Command, args []string) {
assignmentConfig := config.GetAssignmentConfig(args[0], args[1], args[2:]...)
assignmentConfig.Show()
color.Red.Print("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ...")
fmt.Scanln()
c := gitlab.NewClient()
c.Generate(assignmentConfig)
},
Expand Down
21 changes: 21 additions & 0 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/obcode/glabs/config"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(showConfigCmd)
}

var showConfigCmd = &cobra.Command{
Use: "show course assignment [groups...|students...]",
Short: "Show config of an assignment",
Long: `Show config of an assignment`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.GetAssignmentConfig(args[0], args[1], args[2:]...)
cfg.Show()
},
}
34 changes: 27 additions & 7 deletions config/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type AssignmentConfig struct {
type Per string

const (
PerStudent Per = "Student"
PerGroup Per = "Group"
PerStudent Per = "student"
PerGroup Per = "group"
PerFailed Per = "could not happen"
)

Expand All @@ -37,8 +37,8 @@ type Startercode struct {
}

type Group struct {
GroupName string
Members []string
Name string
Members []string
}

type AccessLevel int
Expand All @@ -50,6 +50,19 @@ const (
Maintainer AccessLevel = 40
)

func (ac AccessLevel) show() string {
if ac == 10 {
return "guest"
}
if ac == 20 {
return "reporter"
}
if ac == 30 {
return "developer"
}
return "maintainer"
}

func GetAssignmentConfig(course, assignment string, onlyForStudentsOrGroups ...string) *AssignmentConfig {
if !viper.IsSet(course) {
log.Fatal().
Expand Down Expand Up @@ -173,12 +186,19 @@ func groups(per Per, course string, onlyForStudentsOrGroups ...string) []*Group
groupsMap = onlyTheseGroups
}

keys := make([]string, 0, len(groupsMap))
for k := range groupsMap {
keys = append(keys, k)
}
sort.Strings(keys)

groups := make([]*Group, 0, len(groupsMap))
for groupname, members := range groupsMap {
for _, groupname := range keys {
members := groupsMap[groupname]
sort.Strings(members)
groups = append(groups, &Group{
GroupName: groupname,
Members: members,
Name: groupname,
Members: members,
})
}

Expand Down
80 changes: 80 additions & 0 deletions config/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package config

import (
"fmt"
"strings"

"github.com/gookit/color"
)

func (cfg *AssignmentConfig) Show() {
containerRegistry := "disabled"
if cfg.ContainerRegistry {
containerRegistry = "enabled"
}

startercode := "---"
if cfg.Startercode != nil {
startercode = fmt.Sprintf(`
URL: %s
FromBranch: %s
ToBranch: %s
ProtectToBranch: %t`,
cfg.Startercode.URL,
cfg.Startercode.FromBranch,
cfg.Startercode.ToBranch,
cfg.Startercode.ProtectToBranch,
)
}

var per strings.Builder
switch cfg.Per {
case PerStudent:
per.WriteString("Students:\n")
for _, s := range cfg.Students {
per.WriteString(" - ")
per.WriteString(s)
per.WriteString("\n")
}
case PerGroup:
per.WriteString("Groups:\n")
for _, grp := range cfg.Groups {
per.WriteString(" - ")
per.WriteString(grp.Name)
per.WriteString(": ")
for i, m := range grp.Members {
per.WriteString(m)
if i == len(grp.Members)-1 {
per.WriteString("\n")
} else {
per.WriteString(", ")
}
}
}
}

groupsOrStudents := per.String()

color.Cyan.Printf(`
Course: %s
Assignment: %s
Per: %s
Base-URL: %s
Description: %s
AccessLevel: %s
Container-Registry: %s
Startercode:%s
%s
`,
cfg.Course,
cfg.Name,
cfg.Per,
cfg.URL,
cfg.Description,
cfg.AccessLevel.show(),
containerRegistry,
startercode,
groupsOrStudents,
)

}
51 changes: 19 additions & 32 deletions gitlab/check.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package gitlab

import (
"fmt"

"github.com/gookit/color"
"github.com/obcode/glabs/config"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

func (c *Client) CheckCourse(cfg *config.CourseConfig) bool {
noOfErrors := 0
header(fmt.Sprintf("%s:\n", cfg.Course))
color.Cyan.Printf("%s:\n", cfg.Course)

if len(cfg.Students) > 0 {
header(fmt.Sprintln(" - students:"))
color.Cyan.Println(" - students:")

for _, student := range cfg.Students {
if !c.checkStudent(student, "") {
Expand All @@ -23,37 +20,42 @@ func (c *Client) CheckCourse(cfg *config.CourseConfig) bool {
}

if len(cfg.Groups) > 0 {
header(fmt.Sprintln(" - groups:"))
color.Cyan.Println(" - groups:")

for _, grp := range cfg.Groups {
header(fmt.Sprintf(" - %s:\n", grp.GroupName))
color.Cyan.Printf(" - %s:\n", grp.Name)
for _, student := range grp.Members {
if !c.checkStudent(student, " ") {
noOfErrors++
}
}
}

header(" # checking duplicates in groups\n")
color.Cyan.Print(" # checking duplicates in groups")
foundDup := false

if studsInMoreGroups := checkDupsInGroups(cfg.Groups); len(studsInMoreGroups) > 0 {

for student, inGroups := range studsInMoreGroups {
failure(fmt.Sprintf(" # %s is in more than one group: %v\n", student, inGroups))
color.Red.Printf("\n # %s is in more than one group: %v", student, inGroups)
foundDup = true
noOfErrors++
}

}

if !foundDup {
header(" # no duplicate found\n")
color.Green.Println("... no duplicate found")
}
}

if noOfErrors > 0 {
failure(fmt.Sprintf("# ===> %d error(s)\n", noOfErrors))
color.Red.Printf("\n# ===> %d error", noOfErrors)
if noOfErrors == 1 {
color.Red.Println()
} else {
color.Red.Println("s")
}
return false
}
return true
Expand All @@ -62,10 +64,11 @@ func (c *Client) CheckCourse(cfg *config.CourseConfig) bool {
func (c *Client) checkStudent(name, prefix string) bool {
user, err := c.getUser(name)
if err != nil {
failure(fmt.Sprintf(" # %s, error: %v\n", name, err))
color.Red.Printf(" # %s, error: %v\n", name, err)
return false
}
success(fmt.Sprintf("%s - %s # %s\n", prefix, user.Username, user.Name))
color.Cyan.Printf("%s - %s", prefix, user.Username)
color.Green.Printf(" # %s\n", user.Name)
return true
}

Expand All @@ -75,9 +78,9 @@ func checkDupsInGroups(groups []*config.Group) map[string][]string {
for _, student := range grp.Members {
_, ok := studsWithGroups[student]
if !ok {
studsWithGroups[student] = []string{grp.GroupName}
studsWithGroups[student] = []string{grp.Name}
} else {
studsWithGroups[student] = append(studsWithGroups[student], grp.GroupName)
studsWithGroups[student] = append(studsWithGroups[student], grp.Name)
}
}
}
Expand All @@ -91,19 +94,3 @@ func checkDupsInGroups(groups []*config.Group) map[string][]string {

return problems
}

func header(str string) {
if viper.GetBool("show-success") {
fmt.Print(chalk.Blue, chalk.Bold, str, chalk.Reset)
}
}

func success(str string) {
if viper.GetBool("show-success") {
fmt.Print(chalk.Green, str, chalk.Reset)
}
}

func failure(str string) {
fmt.Print(chalk.Red, str, chalk.Reset)
}
4 changes: 2 additions & 2 deletions gitlab/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (c *Client) generatePerGroup(assignmentCfg *config.AssignmentConfig, assign
}

for _, grp := range assignmentCfg.Groups {
project, generated, err := c.generateProject(assignmentCfg, grp.GroupName, assignmentGroupID)
project, generated, err := c.generateProject(assignmentCfg, grp.Name, assignmentGroupID)
if err != nil {
log.Error().Err(err).
Str("group", grp.GroupName).
Str("group", grp.Name).
Str("course", assignmentCfg.Course).
Str("assignment", assignmentCfg.Name).
Msg("error while generating project")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.15

require (
github.com/go-git/go-git/v5 v5.1.0
github.com/gookit/color v1.3.1
github.com/mitchellh/go-homedir v1.1.0
github.com/rs/zerolog v1.20.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
github.com/xanzy/go-gitlab v0.38.1
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
Expand Down Expand Up @@ -94,6 +95,8 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.3.1 h1:PPD/C7sf8u2L8XQPdPgsWRoAiLQGZEZOzU3cf5IYYUk=
github.com/gookit/color v1.3.1/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down Expand Up @@ -156,7 +159,9 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down Expand Up @@ -236,8 +241,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31 h1:OXcKh35JaYsGMRzpvFkLv/MEyPuL49CThT1pZ8aSml4=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/xanzy/go-gitlab v0.38.1 h1:st5/Ag4h8CqVfp3LpOWW0Jd4jYHTGETwu0KksYDPnYE=
github.com/xanzy/go-gitlab v0.38.1/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
Expand Down

0 comments on commit d86fb4e

Please sign in to comment.