Skip to content

Commit

Permalink
Merge pull request #4 from massix/config-stringer
Browse files Browse the repository at this point in the history
Implement Stringer interface
  • Loading branch information
massix authored Jun 15, 2024
2 parents 194fa68 + b320e13 commit ee6a940
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GO := $(shell which go)
VERSION = 1.2
VERSION = 1.3

.PHONY: clean

Expand Down
15 changes: 2 additions & 13 deletions cmd/protrans/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -15,14 +14,6 @@ import (
"github.com/sirupsen/logrus"
)

func dumpNatConfiguration(conf *config.ProtransConfiguration) string {
return fmt.Sprintf("\tGateway: %s\n\tPortLifetime: %d\n", conf.Nat.Gateway, conf.Nat.PortLifetime)
}

func dumpTransmissionConfiguration(conf *config.ProtransConfiguration) string {
return fmt.Sprintf("\tHost: %s\n\tPort: %d\n\tUsername: %s\n", conf.Transmission.Host, conf.Transmission.Port, conf.Transmission.Username)
}

var Version string

func main() {
Expand All @@ -35,13 +26,11 @@ func main() {
logrus.Info("Using default values")
}

conf := config.NewConfiguration(configurationPath, true)
conf := config.New(configurationPath, true)
logrus.SetLevel(conf.LogrusLogLevel())

logrus.Infof("Protrans version: %s", Version)
logrus.Infof("Log level: %s", conf.LogrusLogLevel().String())
logrus.Infof("NAT Configuration:\n%s", dumpNatConfiguration(conf))
logrus.Infof("Transmission Configuration:\n%s", dumpTransmissionConfiguration(conf))
logrus.Info(conf)

natClient := nat.New(natpmp.NewClientWithTimeout(conf.GatewayIP(), 2*time.Second))
realTransmission, err := transmissionrpc.New(conf.Transmission.Host, conf.Transmission.Username, conf.Transmission.Password, &transmissionrpc.AdvancedConfig{
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
hardeningDisable = [ "fortify" ];
version = "1.2";
version = "1.3";
in
{
devShells.${system}.default = pkgs.mkShell {
Expand Down
19 changes: 18 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func overrideWithEnvironment(iface, concrete any) {
}
}

func NewConfiguration(filePath string, shouldUseEnvironment bool) *ProtransConfiguration {
func New(filePath string, shouldUseEnvironment bool) *ProtransConfiguration {
// Init with some default values
conf := &ProtransConfiguration{
Transmission: &TransmissionConfiguration{
Expand Down Expand Up @@ -95,6 +95,23 @@ func NewConfiguration(filePath string, shouldUseEnvironment bool) *ProtransConfi
return conf
}

func (nc *NatConfiguration) String() string {
return fmt.Sprintf("Nat{Gateway=%q PortLifetime=%d}", nc.Gateway, nc.PortLifetime)
}

func (tc *TransmissionConfiguration) String() string {
password := "not set"
if tc.Password != "" {
password = "set"
}

return fmt.Sprintf("Transmission{Host=%q Port=%d Username=%q Password=%q}", tc.Host, tc.Port, tc.Username, password)
}

func (pc *ProtransConfiguration) String() string {
return fmt.Sprintf("ProTrans{LogLevel=%q %s %s}", pc.LogLevel, pc.Nat, pc.Transmission)
}

func (c *ProtransConfiguration) LogrusLogLevel() (level logrus.Level) {
level = logrus.InfoLevel
switch c.LogLevel {
Expand Down
58 changes: 53 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"fmt"
"os"
"testing"

Expand All @@ -13,7 +14,7 @@ func Test_ShouldParseConfiguration(t *testing.T) {
os.Setenv("PROTRANS_TRANSMISSION_PORT", "1234")
os.Setenv("PROTRANS_NAT_GATEWAY", "192.168.1.1")

res := config.NewConfiguration("../../tests/test_configuration.yaml", true)
res := config.New("../../tests/test_configuration.yaml", true)
assert.NotNil(t, res)
assert.Equal(t, "localhost", res.Transmission.Host) // Default value
assert.Equal(t, uint16(1234), res.Transmission.Port) // Overridden by environment
Expand All @@ -25,24 +26,24 @@ func Test_ShouldParseConfiguration(t *testing.T) {

func Test_ShouldOnlyUseEnvironment(t *testing.T) {
os.Setenv("PROTRANS_NAT_GATEWAY", "192.168.42.1")
res := config.NewConfiguration("", true)
res := config.New("", true)
assert.Equal(t, "localhost", res.Transmission.Host)
assert.Equal(t, "192.168.42.1", res.Nat.Gateway)
}

func Test_FileDoesNotExist_DefaultConfiguration(t *testing.T) {
res := config.NewConfiguration("not-exists.yaml", false)
res := config.New("not-exists.yaml", false)
assert.Equal(t, "localhost", res.Transmission.Host)
}

func Test_InvalidYAMLFile_DefaultConfiguration(t *testing.T) {
res := config.NewConfiguration("../../tests/test_invalid_configuration.yaml", false)
res := config.New("../../tests/test_invalid_configuration.yaml", false)
assert.Equal(t, uint16(9091), res.Transmission.Port)
}

func Test_InvalidFile_UseEnvironment(t *testing.T) {
os.Setenv("PROTRANS_TRANSMISSION_USERNAME", "user_from_environment")
res := config.NewConfiguration("../../tests/test_invalid_configuration.yaml", true)
res := config.New("../../tests/test_invalid_configuration.yaml", true)
assert.Equal(t, "user_from_environment", res.Transmission.Username)
}

Expand All @@ -59,3 +60,50 @@ func Test_GatewayIP_KO(t *testing.T) {

assert.Equal(t, "10.2.0.1", res.String())
}

func ExampleTransmissionConfiguration_String() {
tc := &config.TransmissionConfiguration{
Host: "localhost",
Port: 9091,
Username: "SomeUser",
Password: "WithPassword",
}

fmt.Print(tc.String())

// Output:
// Transmission{Host="localhost" Port=9091 Username="SomeUser" Password="set"}
}

func ExampleNatConfiguration_String() {
nc := &config.NatConfiguration{
Gateway: "10.0.2.1",
PortLifetime: 600,
}

fmt.Print(nc)

// Output:
// Nat{Gateway="10.0.2.1" PortLifetime=600}
}

func ExampleProtransConfiguration_String() {
ptc := &config.ProtransConfiguration{
Transmission: &config.TransmissionConfiguration{
Host: "192.168.1.2",
Port: 8080,
Username: "",
Password: "",
},
Nat: &config.NatConfiguration{
Gateway: "10.42.3.1",
PortLifetime: 126,
},
LogLevel: "DEBUG",
}

fmt.Print(ptc)

// Output:
// ProTrans{LogLevel="DEBUG" Nat{Gateway="10.42.3.1" PortLifetime=126} Transmission{Host="192.168.1.2" Port=8080 Username="" Password="not set"}}
}

0 comments on commit ee6a940

Please sign in to comment.