Skip to content
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
32 changes: 12 additions & 20 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/logrusorgru/aurora/v4"
"github.com/obcode/glabs/v2/config"
Expand Down Expand Up @@ -45,7 +44,10 @@ it can gate a commit hook or CI.`,
Run: func(cmd *cobra.Command, args []string) {
problems := 0
for _, course := range coursesToProcess(args) {
path := courseFilePath(course)
path, err := findCourseFile(course)
if err != nil {
er(err)
}
source, decoded, err := decodeCourseFile(path)
if err != nil {
er(err)
Expand Down Expand Up @@ -86,7 +88,7 @@ parsed configuration, not edited in place. Review the diff before committing.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, course := range args {
rewriteCourseFile(courseFilePath(course))
rewriteCourseFile(course)
}
},
}
Expand All @@ -109,12 +111,17 @@ Comments and key order are not preserved. Review the diff before committing.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, course := range args {
rewriteCourseFile(courseFilePath(course))
rewriteCourseFile(course)
}
},
}

func rewriteCourseFile(path string) {
func rewriteCourseFile(course string) {
path, err := findCourseFile(course)
if err != nil {
er(err)
}

source, _, err := decodeCourseFile(path)
if err != nil {
er(err)
Expand Down Expand Up @@ -164,18 +171,3 @@ func coursesToProcess(args []string) []string {
}
return courses
}

// courseFilePath locates a course file the same way initConfig does: by name,
// under coursesfilepath, with the extensions viper would have accepted.
func courseFilePath(course string) string {
dir := viper.GetString("coursesfilepath")
for _, ext := range []string{".yaml", ".yml"} {
path := filepath.Join(dir, course+ext)
if _, err := os.Stat(path); err == nil {
return path
}
}
er(fmt.Sprintf("cannot find a course file for %q: looked for %s.yaml and %s.yml in %q",
course, course, course, dir))
return ""
}
42 changes: 32 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/obcode/glabs/v2/config"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -68,16 +70,36 @@ func initConfig() {

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
viper.AddConfigPath(viper.GetString("coursesfilepath"))
for _, course := range viper.GetStringSlice("courses") {
viper.SetConfigName(course)
err = viper.MergeInConfig()
if err != nil {
panic(fmt.Errorf("%s: should be %s.yml", err, course))
}
if err := viper.ReadInConfig(); err != nil {
er(fmt.Errorf("cannot read the main config file: %w", err))
}

// Course files are loaded into config's own registry rather than merged into
// viper. Merging them put course names in the same flat namespace as
// gitlab.host and coursesfilepath — a course named `courses` would have
// collided with the course list — and left resolution reading, and writing,
// global state.
for _, course := range viper.GetStringSlice("courses") {
path, err := findCourseFile(course)
if err != nil {
er(err)
}
if _, err := config.LoadCourseFile(path); err != nil {
er(err)
}
}
}

// findCourseFile locates a course file by name under coursesfilepath, accepting
// the extensions viper used to search for.
func findCourseFile(course string) (string, error) {
dir := viper.GetString("coursesfilepath")
for _, ext := range []string{".yaml", ".yml"} {
path := filepath.Join(dir, course+ext)
if _, err := os.Stat(path); err == nil {
return path, nil
}
} else {
panic(fmt.Errorf("fatal error config file: %s", err))
}
return "", fmt.Errorf("cannot find a course file for %q: looked for %s.yaml and %s.yml in %q",
course, course, course, dir)
}
Loading
Loading