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

     1  package track
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  // MaintainerConfig contains the list of current and previous maintainers.
    12  // The files is used both to manage the GitHub maintainer team, as well
    13  // as to configure the display values for each maintainer on the Exercism
    14  // website.
    15  type MaintainerConfig struct {
    16  	DocsURL     string       `json:"docs_url"`
    17  	Maintainers []Maintainer `json:"maintainers"`
    18  }
    19  
    20  // Maintainer contains data about a track maintainer.
    21  type Maintainer struct {
    22  	Username      string  `json:"github_username"`
    23  	Alumnus       bool    `json:"alumnus"`
    24  	ShowOnWebsite bool    `json:"show_on_website"`
    25  	Name          *string `json:"name"`
    26  	LinkText      *string `json:"link_text"`
    27  	LinkURL       *string `json:"link_url"`
    28  	AvatarURL     *string `json:"avatar_url"`
    29  	Bio           *string `json:"bio"`
    30  }
    31  
    32  // NewMaintainerConfig reads the maintainer config file, if present.
    33  func NewMaintainerConfig(path string) (MaintainerConfig, error) {
    34  	mc := MaintainerConfig{}
    35  	if _, err := os.Stat(path); os.IsNotExist(err) {
    36  		return mc, nil
    37  	}
    38  
    39  	bytes, err := ioutil.ReadFile(path)
    40  	if err != nil {
    41  		return mc, err
    42  	}
    43  	err = json.Unmarshal(bytes, &mc)
    44  	if err != nil {
    45  		return mc, fmt.Errorf("invalid config %s -- %s", path, err.Error())
    46  	}
    47  	return mc, nil
    48  }
    49  
    50  // LoadFromFile loads a config from file given the path to the file.
    51  func (mCfg *MaintainerConfig) LoadFromFile(path string) error {
    52  	file, err := os.Open(filepath.FromSlash(path))
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	if err := json.NewDecoder(file).Decode(mCfg); err != nil {
    58  		return err
    59  	}
    60  	return nil
    61  }
    62  
    63  // ToJSON marshals the Config to normalized JSON.
    64  func (mCfg MaintainerConfig) ToJSON() ([]byte, error) {
    65  	return json.MarshalIndent(&mCfg, "", "  ")
    66  }