github.com/exercism/configlet@v3.9.3-0.20200318193232-c70be6269e71+incompatible/track/track.go (about)

     1  package track
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"regexp"
     7  )
     8  
     9  // Track is a collection of Exercism exercises for a programming language.
    10  type Track struct {
    11  	ID               string
    12  	Config           Config
    13  	MaintainerConfig MaintainerConfig
    14  	Exercises        []Exercise
    15  	path             string
    16  }
    17  
    18  // New loads a track.
    19  func New(path string) (Track, error) {
    20  	track := Track{
    21  		path: filepath.FromSlash(path),
    22  	}
    23  
    24  	ap, err := filepath.Abs(track.path)
    25  	if err != nil {
    26  		return track, err
    27  	}
    28  	track.ID = filepath.Base(ap)
    29  
    30  	c, err := NewConfig(filepath.Join(path, "config.json"))
    31  	if err != nil {
    32  		return track, err
    33  	}
    34  	track.Config = c
    35  
    36  	mc, err := NewMaintainerConfig(filepath.Join(path, "config", "maintainers.json"))
    37  	if err != nil {
    38  		return track, err
    39  	}
    40  	track.MaintainerConfig = mc
    41  
    42  	dir := filepath.Join(track.path, "exercises")
    43  	files, err := ioutil.ReadDir(dir)
    44  	if err != nil {
    45  		return track, err
    46  	}
    47  
    48  	// Valid exercise directory names do not begin with `.` or `_`.
    49  	re := regexp.MustCompile("^[._]")
    50  	for _, file := range files {
    51  		if file.IsDir() {
    52  			fn := file.Name()
    53  			if re.MatchString(fn) {
    54  				continue
    55  			}
    56  			fp := filepath.Join(dir, fn)
    57  
    58  			ex, err := NewExercise(fp, track.Config.PatternGroup)
    59  			if err != nil {
    60  				return track, err
    61  			}
    62  
    63  			track.Exercises = append(track.Exercises, ex)
    64  		}
    65  	}
    66  	return track, nil
    67  }