-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jop-software/new-config
task: initiate new config
- Loading branch information
Showing
13 changed files
with
184 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/.idea | ||
|
||
.env | ||
imap-mailbox-exporter | ||
imap-mailbox-exporter | ||
/config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,23 @@ http://127.0.0.1:9101/probe?target=INBOX | |
probe_mailbox_count 0 | ||
``` | ||
|
||
### Configuration | ||
|
||
The `imap-mailbox-exporter` can be configures with a `config.yaml` file and environment variables. | ||
|
||
```yaml | ||
server: | ||
- hostname: 'hostname' | ||
port: '1234' | ||
accounts: | ||
- username: '[email protected]' | ||
password: 'env:E_AT_MAIL_COM_PASSWORD' | ||
``` | ||
You can use environment variables with the `env:VARIABLE_NAME` directive in YAML. | ||
|
||
The configuration file is expected in `./config.yaml` relative to the `imap-mailbox-exporter` binary. | ||
|
||
### Example Usage | ||
|
||
You can find a example docker compose configuration. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type ConfigAcccount struct { | ||
Username string `yaml:"username"` | ||
Password string `yaml:"password"` | ||
} | ||
|
||
type ConfigServer struct { | ||
Host string `yaml:"hostname"` | ||
Port string `yaml:"port"` | ||
|
||
Account []ConfigAcccount `yaml:"accounts"` | ||
} | ||
|
||
func (configServer ConfigServer) HostPort() string { | ||
return configServer.Host + ":" + configServer.Port | ||
} | ||
|
||
type Config struct { | ||
Server []ConfigServer `yaml:"server"` | ||
} | ||
|
||
func NewConfig(path string) (*Config, error) { | ||
config := &Config{} | ||
|
||
configBytes, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
configString := replaceEnvPlaceholder(string(configBytes)) | ||
|
||
err = yaml.Unmarshal([]byte(configString), &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func replaceEnvPlaceholder(data string) string { | ||
expr := regexp.MustCompile("env:([A-Z_]+)") | ||
matches := expr.FindAll([]byte(data), -1) | ||
|
||
for _, match := range matches { | ||
variable := string(match) | ||
variable = strings.TrimLeft(variable, "env:") | ||
|
||
env := os.Getenv(variable) | ||
if env == "" { | ||
log.Printf("Environment variable %s is empty. Skipping replacement.", variable) | ||
continue | ||
} | ||
|
||
data = strings.ReplaceAll(data, fmt.Sprintf("env:%s", variable), env) | ||
} | ||
|
||
return data | ||
} | ||
|
||
// Find the account and server from the given hostname and username | ||
func (config Config) FindAccountInServer(hostname, username string) (*ConfigServer, *ConfigAcccount, error) { | ||
for _, server := range config.Server { | ||
if server.Host != hostname { | ||
continue | ||
} | ||
|
||
for _, account := range server.Account { | ||
if account.Username != username { | ||
continue | ||
} | ||
|
||
return &server, &account, nil | ||
} | ||
} | ||
|
||
return nil, nil, errors.New("cound not find user on given server in configuration") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConfigServerHostPort(t *testing.T) { | ||
server := &ConfigServer{ | ||
Host: "hostname", | ||
Port: "0000", | ||
} | ||
|
||
result := server.HostPort() | ||
if result != "hostname:0000" { | ||
t.Logf("Expected hostname:0000, got %s", result) | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
server: | ||
- hostname: 'imap.mail.com' | ||
port: '993' | ||
accounts: | ||
- username: '[email protected]' | ||
password: 'env:E_MAIL_COM_PW' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
IMAP_SERVER="" | ||
IMAP_USERNAME="" | ||
IMAP_PASSWORD="" | ||
E_MAIL_COM_PW="my-very-secure-password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters