Compare commits
No commits in common. "279500db508f7e2254442f7a5278965eb7cbe803" and "02ab2ec86775c9b42ed62462d864a6386fe5cce0" have entirely different histories.
279500db50
...
02ab2ec867
4 changed files with 11 additions and 65 deletions
|
@ -1 +0,0 @@
|
|||
*.toml
|
23
Dockerfile
23
Dockerfile
|
@ -1,23 +0,0 @@
|
|||
FROM golang:1.13 AS builder
|
||||
|
||||
WORKDIR /app
|
||||
COPY go.mod go.sum /app/
|
||||
RUN go mod download
|
||||
COPY *go /app/
|
||||
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -tags netgo -o /sendmail ./...
|
||||
|
||||
|
||||
FROM gcr.io/distroless/static
|
||||
|
||||
ENV sm_server ""
|
||||
ENV sm_port "465"
|
||||
ENV sm_user ""
|
||||
ENV sm_password ""
|
||||
ENV sm_from ""
|
||||
ENV sm_to ""
|
||||
ENV sm_sub ""
|
||||
|
||||
COPY --from=builder /sendmail /
|
||||
WORKDIR /
|
||||
|
||||
ENTRYPOINT ["/sendmail"]
|
10
config.go
10
config.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
toml "github.com/pelletier/go-toml"
|
||||
)
|
||||
|
@ -70,20 +71,21 @@ func (c *Config) Merge(key string, value interface{}) {
|
|||
Merge(key, value, c)
|
||||
}
|
||||
|
||||
func readConfig(configPath, section string) (*Config, error) {
|
||||
func readConfig(configPath, section string) *Config {
|
||||
config := NewConfig()
|
||||
tree, err := toml.LoadFile(configPath)
|
||||
if err != nil {
|
||||
return config, err
|
||||
Error.F("Error in parsing the configuration\n%s", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
keys := tree.Keys()
|
||||
|
||||
if !IsPresent(keys, section) {
|
||||
return config, errors.New("missing section")
|
||||
return config
|
||||
}
|
||||
sub := tree.Get(section).(*toml.Tree)
|
||||
err = sub.Unmarshal(config)
|
||||
return config, err
|
||||
return config
|
||||
}
|
||||
|
||||
func checkValidity(validations *[]Validation, obj interface{}, name, cmd, param string) {
|
||||
|
|
42
main.go
42
main.go
|
@ -8,8 +8,6 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var version = "0.1"
|
||||
|
||||
func readFromConsole() string {
|
||||
var text, line string
|
||||
var err error
|
||||
|
@ -19,8 +17,6 @@ func readFromConsole() string {
|
|||
line, err = reader.ReadString('\n')
|
||||
if line == "\n" {
|
||||
counter += 1
|
||||
} else {
|
||||
counter = 0
|
||||
}
|
||||
text += fmt.Sprintf("%s\n", line)
|
||||
}
|
||||
|
@ -28,7 +24,7 @@ func readFromConsole() string {
|
|||
Error.F("Error in reading text from console\n%s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return strings.TrimRight(text, "\n")
|
||||
return text
|
||||
}
|
||||
|
||||
func toList(input string) []string {
|
||||
|
@ -42,37 +38,13 @@ func toList(input string) []string {
|
|||
return result
|
||||
}
|
||||
|
||||
func initializeConfig(configPath, section string) *Config {
|
||||
if configPath == "" {
|
||||
configPath = "/etc/sendmail.toml"
|
||||
|
||||
// Ignore non existing config only if `-conf` not used on command line.
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return NewConfig()
|
||||
}
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath, section)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
Error.F("Error in parsing the configuration\n%s", err)
|
||||
os.Exit(-2)
|
||||
}
|
||||
}
|
||||
|
||||
Debug.F("---\nConfig from %s\n%s", configPath, *config)
|
||||
return config
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
|
||||
var encryption, dbg, versionFlag bool
|
||||
var encryption, dbg bool
|
||||
var serverPort_ int
|
||||
var serverPort int64
|
||||
|
||||
flag.BoolVar(&versionFlag, "version", false, "Prints the version and exits")
|
||||
flag.StringVar(&configPath, "conf", "", "Path to a config file (defaults to /etc/sendmail.toml)")
|
||||
flag.StringVar(&configPath, "conf", "/etc/sendmail.toml", "Path to a config file (defaults to /etc/sendmail.toml)")
|
||||
flag.StringVar(§ion, "section", "default", "Section of the conf to read (defaults to \"default\")")
|
||||
flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
|
||||
flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
|
||||
|
@ -89,11 +61,6 @@ func main() {
|
|||
|
||||
LogInit(dbg)
|
||||
|
||||
if versionFlag {
|
||||
Info.F("Version: %s", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if flag.NArg() == 0 {
|
||||
text = readFromConsole()
|
||||
} else {
|
||||
|
@ -128,8 +95,9 @@ parameters:
|
|||
from,
|
||||
subject,
|
||||
)
|
||||
config := readConfig(configPath, section)
|
||||
Debug.F("---\nConfig from %s\n%s", configPath, *config)
|
||||
|
||||
config := initializeConfig(configPath, section)
|
||||
config.Server.Merge("Address", serverAddress)
|
||||
config.Server.Merge("Port", serverPort)
|
||||
config.Server.Merge("Encryption", encryption)
|
||||
|
|
Loading…
Reference in a new issue