refactor: Configuration to separate file

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-09-11 20:25:20 +02:00
parent a25dabe20b
commit bc793b7162
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
2 changed files with 37 additions and 31 deletions

35
configuration.go Normal file
View file

@ -0,0 +1,35 @@
package main
import (
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
Gitea struct {
InstanceURL string `yaml:"instance_url"`
Owner string `yaml:"owner"`
Repository string `yaml:"repository"`
} `yaml:"gitea"`
Language struct {
OpeningOnSeparateLine bool `yaml:"separate_opening"`
Opening string `yaml:"opening"`
Continuation string `yaml:"continuation"`
Closing string `yaml:"closing"`
} `yaml:"language"`
}
func LoadConfig(config *Config, filename string) {
if filename == "" {
filename = ".frag_review.yml"
}
configFile, err := os.Open(filename)
ExitOnError(err)
defer configFile.Close()
decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config)
ExitOnError(err)
}

33
main.go
View file

@ -9,25 +9,8 @@ import (
"strconv"
"code.gitea.io/sdk/gitea"
"gopkg.in/yaml.v2"
)
type Config struct {
Gitea struct {
InstanceURL string `yaml:"instance_url"`
Owner string `yaml:"owner"`
Repository string `yaml:"repository"`
} `yaml:"gitea"`
Language struct {
OpeningOnSeparateLine bool `yaml:"separate_opening"`
Opening string `yaml:"opening"`
Continuation string `yaml:"continuation"`
Closing string `yaml:"closing"`
} `yaml:"language"`
}
var config Config = Config{}
type ByLineNum []*gitea.PullReviewComment
func (a ByLineNum) Len() int { return len(a) }
@ -138,23 +121,11 @@ func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
return result
}
func LoadConfig(filename string) {
if filename == "" {
filename = ".frag_review.yml"
}
configFile, err := os.Open(filename)
ExitOnError(err)
defer configFile.Close()
decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config)
ExitOnError(err)
}
var config Config = Config{}
func main() {
token := os.Getenv("GITEA_TOKEN")
LoadConfig(os.Getenv("FRAG_REVIEW_CONFIG"))
LoadConfig(&config, os.Getenv("FRAG_REVIEW_CONFIG"))
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(token))
ExitOnError(err)