Skip to content

Commit

Permalink
Refactor packaging and namings
Browse files Browse the repository at this point in the history
  • Loading branch information
metehanozturk committed May 28, 2020
1 parent 0089949 commit 3f6405a
Show file tree
Hide file tree
Showing 37 changed files with 899 additions and 1,047 deletions.
152 changes: 11 additions & 141 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,10 @@ package conf

import (
"github.com/opsgenie/oec/git"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
"time"
)

const (
LocalSourceType = "local"
GitSourceType = "git"

DefaultBaseUrl = "https://api.opsgenie.com"
)

var readConfigurationFromGitFunc = readConfigurationFromGit
var readConfigurationFromLocalFunc = readConfigurationFromLocal

var defaultConfFilepath = filepath.Join("~", "oec", "config.json")

type Configuration struct {
ActionSpecifications `yaml:",inline"`
AppName string `json:"appName" yaml:"appName"`
Expand All @@ -44,11 +28,11 @@ type ActionName string

type ActionMappings map[ActionName]MappedAction

func (m ActionMappings) GitActions() []git.GitOptions {
func (m ActionMappings) GitActions() []git.Options {

opts := make([]git.GitOptions, 0)
opts := make([]git.Options, 0)
for _, action := range m {
if (action.GitOptions != git.GitOptions{}) {
if (action.GitOptions != git.Options{}) {
opts = append(opts, action.GitOptions)
}
}
Expand All @@ -57,14 +41,14 @@ func (m ActionMappings) GitActions() []git.GitOptions {
}

type MappedAction struct {
SourceType string `json:"sourceType" yaml:"sourceType"`
GitOptions git.GitOptions `json:"gitOptions" yaml:"gitOptions"`
Filepath string `json:"filepath" yaml:"filepath"`
Flags Flags `json:"flags" yaml:"flags"`
Args []string `json:"args" yaml:"args"`
Env []string `json:"env" yaml:"env"`
Stdout string `json:"stdout" yaml:"stdout"`
Stderr string `json:"stderr" yaml:"stderr"`
SourceType string `json:"sourceType" yaml:"sourceType"`
GitOptions git.Options `json:"gitOptions" yaml:"gitOptions"`
Filepath string `json:"filepath" yaml:"filepath"`
Flags Flags `json:"flags" yaml:"flags"`
Args []string `json:"args" yaml:"args"`
Env []string `json:"env" yaml:"env"`
Stdout string `json:"stdout" yaml:"stdout"`
Stderr string `json:"stderr" yaml:"stderr"`
}

type Flags map[string]string
Expand Down Expand Up @@ -93,117 +77,3 @@ type PoolConf struct {
KeepAliveTimeInMillis time.Duration `json:"keepAliveTimeInMillis" yaml:"keepAliveTimeInMillis"`
MonitoringPeriodInMillis time.Duration `json:"monitoringPeriodInMillis" yaml:"monitoringPeriodInMillis"`
}

func ReadConfFile() (*Configuration, error) {

confSourceType := os.Getenv("OEC_CONF_SOURCE_TYPE")
conf, err := readConfFileFromSource(strings.ToLower(confSourceType))
if err != nil {
return nil, err
}

if os.Getenv("OEC_API_KEY") != "" {
conf.ApiKey = os.Getenv("OEC_API_KEY")
}

err = validateConfiguration(conf)
if err != nil {
return nil, err
}

addHomeDirPrefixToActionMappings(conf.ActionMappings)
chmodLocalActions(conf.ActionMappings, 0700)

conf.addDefaultFlags()

return conf, nil
}

func (c *Configuration) addDefaultFlags() {
c.GlobalArgs = append(
[]string{
"-apiKey", c.ApiKey,
"-opsgenieUrl", c.BaseUrl,
"-logLevel", strings.ToUpper(c.LogLevel),
},
c.GlobalArgs...,
)
}

func validateConfiguration(conf *Configuration) error {

if conf == nil || conf == (&Configuration{}) {
return errors.New("The configuration is empty.")
}
if conf.ApiKey == "" {
return errors.New("ApiKey is not found in the configuration file.")
}
if conf.BaseUrl == "" {
conf.BaseUrl = DefaultBaseUrl
logrus.Infof("BaseUrl is not found in the configuration file, default url[%s] is set.", DefaultBaseUrl)
}

if len(conf.ActionMappings) == 0 {
return errors.New("Action mappings configuration is not found in the configuration file.")
} else {
for actionName, action := range conf.ActionMappings {
if action.SourceType != LocalSourceType &&
action.SourceType != GitSourceType {
return errors.Errorf("Action source type of action[%s] should be either local or git.", actionName)
} else {
if action.Filepath == "" {
return errors.Errorf("Filepath of action[%s] is empty.", actionName)
}
if action.SourceType == GitSourceType &&
action.GitOptions == (git.GitOptions{}) {
return errors.Errorf("Git options of action[%s] is empty.", actionName)
}
}
}
}

level, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
conf.LogrusLevel = logrus.InfoLevel
conf.LogLevel = "info"
} else {
conf.LogrusLevel = level
}

return nil
}

func readConfFileFromSource(confSourceType string) (*Configuration, error) {

switch confSourceType {
case GitSourceType:
url := os.Getenv("OEC_CONF_GIT_URL")
privateKeyFilepath := os.Getenv("OEC_CONF_GIT_PRIVATE_KEY_FILEPATH")
passphrase := os.Getenv("OEC_CONF_GIT_PASSPHRASE")
confFilepath := os.Getenv("OEC_CONF_GIT_FILEPATH")

if privateKeyFilepath != "" {
privateKeyFilepath = addHomeDirPrefix(privateKeyFilepath)
}

if confFilepath == "" {
return nil, errors.New("Git configuration filepath could not be empty.")
}

return readConfigurationFromGitFunc(url, privateKeyFilepath, passphrase, confFilepath)
case LocalSourceType:
confFilepath := os.Getenv("OEC_CONF_LOCAL_FILEPATH")

if len(confFilepath) <= 0 {
confFilepath = addHomeDirPrefix(defaultConfFilepath)
} else {
confFilepath = addHomeDirPrefix(confFilepath)
}

return readConfigurationFromLocalFunc(confFilepath)
case "":
return nil, errors.Errorf("OEC_CONF_SOURCE_TYPE should be set as \"local\" or \"git\".")
default:
return nil, errors.Errorf("Unknown configuration source type[%s], valid types are \"local\" and \"git\".", confSourceType)
}
}
11 changes: 0 additions & 11 deletions conf/from_local.go

This file was deleted.

10 changes: 5 additions & 5 deletions conf/from_git.go → conf/git_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package conf
import (
"github.com/opsgenie/oec/git"
"os"
"path/filepath"
fpath "path/filepath"
)

var cloneMasterFunc = git.CloneMaster

func readConfigurationFromGit(url, privateKeyFilepath, passPhrase, confFilepath string) (*Configuration, error) {
func readFileFromGit(url, privateKeyFilepath, passPhrase, filepath string) (*Configuration, error) {

err := checkFileExtension(confFilepath)
err := checkFileExtension(filepath)
if err != nil {
return nil, err
}
Expand All @@ -22,7 +22,7 @@ func readConfigurationFromGit(url, privateKeyFilepath, passPhrase, confFilepath

defer os.RemoveAll(repoFilepath)

confFilepath = filepath.Join(repoFilepath, confFilepath)
filepath = fpath.Join(repoFilepath, filepath)

return readConfigurationFromFile(confFilepath)
return readFile(filepath)
}
6 changes: 3 additions & 3 deletions conf/from_git_test.go → conf/git_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"testing"
)

func TestReadConfigurationFromGit(t *testing.T) {
func TestReadFileFromGit(t *testing.T) {

defer func() { cloneMasterFunc = git.CloneMaster }()

confPath, err := util.CreateTempTestFile(mockJsonConfFileContent, ".json")
confPath, err := util.CreateTempTestFile(mockJsonFileContent, ".json")
cloneMasterFunc = func(url, privateKeyFilepath, passPhrase string) (repositoryPath string, err error) {
return "", nil
}

config, err := readConfigurationFromGit("", "", "", confPath)
config, err := readFileFromGit("", "", "", confPath)

assert.Nil(t, err)
assert.Equal(t, mockConf, config)
Expand Down
11 changes: 11 additions & 0 deletions conf/local_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package conf

func readFileFromLocal(filepath string) (*Configuration, error) {

err := checkFileExtension(filepath)
if err != nil {
return nil, err
}

return readFile(filepath)
}
6 changes: 3 additions & 3 deletions conf/from_local_test.go → conf/local_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"testing"
)

func TestReadConfigurationFromLocal(t *testing.T) {
func TestReadFileFromLocal(t *testing.T) {

confPath, err := util.CreateTempTestFile(mockJsonConfFileContent, ".json")
confPath, err := util.CreateTempTestFile(mockJsonFileContent, ".json")
assert.Nil(t, err)

actualConf, _ := readConfigurationFromLocal(confPath)
actualConf, _ := readFileFromLocal(confPath)

defer os.Remove(confPath)

Expand Down
Loading

0 comments on commit 3f6405a

Please sign in to comment.