2018-05-15 21:13:11 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-05-30 23:28:31 +00:00
|
|
|
"errors"
|
2018-05-15 21:13:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var projectRoot string
|
|
|
|
|
2018-05-23 23:48:35 +00:00
|
|
|
var dockerAvailable, pandocAvailable bool
|
|
|
|
|
2018-05-30 23:28:31 +00:00
|
|
|
const (
|
|
|
|
Jira = "jira"
|
|
|
|
GitHub = "github"
|
2018-07-21 00:45:20 +00:00
|
|
|
GitLab = "gitlab"
|
2018-05-30 23:28:31 +00:00
|
|
|
NoTickets = "none"
|
|
|
|
)
|
|
|
|
|
2018-05-23 23:48:35 +00:00
|
|
|
const (
|
|
|
|
// UseDocker invokes pandoc within Docker
|
|
|
|
UseDocker = "docker"
|
|
|
|
// UsePandoc invokes pandoc directly
|
|
|
|
UsePandoc = "pandoc"
|
|
|
|
)
|
|
|
|
|
2018-05-15 21:13:11 +00:00
|
|
|
// SetProjectRoot is used by the test suite.
|
|
|
|
func SetProjectRoot(dir string) {
|
|
|
|
projectRoot = dir
|
|
|
|
}
|
|
|
|
|
|
|
|
type Project struct {
|
2018-08-29 22:48:07 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Pandoc string `yaml:"pandoc,omitempty"`
|
|
|
|
FilePrefix string `yaml:"filePrefix"`
|
|
|
|
Tickets map[string]interface{} `yaml:"tickets"`
|
|
|
|
ApprovedBranch string `yaml:"approvedBranch"`
|
2021-08-03 22:19:24 +00:00
|
|
|
DocOutputs []string `yaml:"outputs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get outputfile extensions from yaml but set default as pdf for backwards
|
|
|
|
// compatibility with legacy yaml files
|
|
|
|
func GetFileExtensions() []string {
|
|
|
|
fileExtensions := Config().DocOutputs
|
|
|
|
|
|
|
|
if len(fileExtensions) == 0 {
|
|
|
|
fileExtensions = append(fileExtensions, "pdf")
|
|
|
|
return fileExtensions
|
|
|
|
} else {
|
|
|
|
return fileExtensions
|
|
|
|
}
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 23:48:35 +00:00
|
|
|
// SetPandoc records pandoc availability during initialization
|
|
|
|
func SetPandoc(pandoc bool, docker bool) {
|
|
|
|
pandocAvailable = pandoc
|
|
|
|
dockerAvailable = docker
|
|
|
|
}
|
|
|
|
|
|
|
|
// WhichPandoc indicates which pandoc invocation path should be used
|
|
|
|
func WhichPandoc() string {
|
|
|
|
cfg := Config()
|
|
|
|
if cfg.Pandoc == UsePandoc {
|
|
|
|
return UsePandoc
|
|
|
|
}
|
|
|
|
if cfg.Pandoc == UseDocker {
|
|
|
|
return UseDocker
|
|
|
|
}
|
|
|
|
if pandocAvailable {
|
|
|
|
return UsePandoc
|
|
|
|
}
|
|
|
|
return UseDocker
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:13:11 +00:00
|
|
|
// YAML is the parsed contents of ProjectRoot()/config.yml.
|
|
|
|
func YAML() map[interface{}]interface{} {
|
|
|
|
m := make(map[interface{}]interface{})
|
|
|
|
cfgBytes, err := ioutil.ReadFile(filepath.Join(ProjectRoot(), "comply.yml"))
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to load config.yml: " + err.Error())
|
|
|
|
}
|
|
|
|
yaml.Unmarshal(cfgBytes, &m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists tests for the presence of a comply configuration file.
|
|
|
|
func Exists() bool {
|
|
|
|
_, err := ioutil.ReadFile(filepath.Join(ProjectRoot(), "comply.yml"))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the parsed contents of ProjectRoot()/config.yml.
|
2018-05-30 23:28:31 +00:00
|
|
|
func Config() *Project {
|
2018-05-15 21:13:11 +00:00
|
|
|
p := Project{}
|
|
|
|
cfgBytes, err := ioutil.ReadFile(filepath.Join(ProjectRoot(), "comply.yml"))
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to load config.yml: " + err.Error())
|
|
|
|
}
|
|
|
|
yaml.Unmarshal(cfgBytes, &p)
|
2018-05-30 23:28:31 +00:00
|
|
|
return &p
|
2018-05-15 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectRoot is the fully-qualified path to the root directory.
|
|
|
|
func ProjectRoot() string {
|
|
|
|
if projectRoot == "" {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
projectRoot = dir
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectRoot
|
|
|
|
}
|
2018-05-30 23:28:31 +00:00
|
|
|
|
|
|
|
// TicketSystem indicates the type of the configured ticket system
|
|
|
|
func (p *Project) TicketSystem() (string, error) {
|
|
|
|
if len(p.Tickets) > 1 {
|
|
|
|
return NoTickets, errors.New("multiple ticket systems configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range p.Tickets {
|
|
|
|
switch k {
|
|
|
|
case GitHub:
|
|
|
|
return GitHub, nil
|
|
|
|
case Jira:
|
|
|
|
return Jira, nil
|
2018-07-21 00:45:20 +00:00
|
|
|
case GitLab:
|
|
|
|
return GitLab, nil
|
2018-05-30 23:28:31 +00:00
|
|
|
case NoTickets:
|
|
|
|
return NoTickets, nil
|
|
|
|
default:
|
|
|
|
// explicit error for this case
|
|
|
|
return "", errors.New("unrecognized ticket system configured")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no ticket block configured
|
|
|
|
return NoTickets, nil
|
|
|
|
}
|