-
Notifications
You must be signed in to change notification settings - Fork 2
/
local_file_configuration_source_options.go
79 lines (66 loc) · 2.07 KB
/
local_file_configuration_source_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package local_file
import (
"errors"
"fmt"
"github.com/ereb-or-od/kenobi/pkg/utilities"
"strings"
)
var (
validFileTypes = []string{"yaml", "toml", "json", "ini", "env"}
configurationFileMustBeSpecifiedError = errors.New("configuration file name must be specified")
configurationFileTypeMustBeSpecifiedError = errors.New("configuration file type must be specified")
configurationFileTypeCouldNotBeRecognizedError = errors.New(fmt.Sprintf("configuration file type could not be recognized. your file type must be one of %s", strings.Join(validFileTypes, ",")))
configurationFilePathMustBeSpecifiedError = errors.New("configuration file path must be specified")
)
type ConfigurationSourceOptions struct {
fileName string
fileType string
filePath string
}
func New(fileName string, fileType string, filePath string) (*ConfigurationSourceOptions, error) {
options := &ConfigurationSourceOptions{
fileName: fileName,
fileType: fileType,
filePath: filePath,
}
if err := options.validateFileName(); err != nil {
return nil, err
}
if err := options.validateFileType(); err != nil {
return nil, err
}
if err := options.validateFilePath(); err != nil {
return nil, err
}
return options, nil
}
func (c *ConfigurationSourceOptions) GetFileName() string {
return c.fileName
}
func (c *ConfigurationSourceOptions) GetFileType() string {
return c.fileType
}
func (c *ConfigurationSourceOptions) GetFilePath() string {
return c.filePath
}
func (c *ConfigurationSourceOptions) validateFileName() error {
if len(c.fileName) == 0 {
return configurationFileMustBeSpecifiedError
}
return nil
}
func (c *ConfigurationSourceOptions) validateFileType() error {
if len(c.fileType) == 0 {
return configurationFileTypeMustBeSpecifiedError
}
if !utilities.ContainsInStringSlice(validFileTypes, c.fileType) {
return configurationFileTypeCouldNotBeRecognizedError
}
return nil
}
func (c *ConfigurationSourceOptions) validateFilePath() error {
if len(c.filePath) == 0 {
return configurationFilePathMustBeSpecifiedError
}
return nil
}