Skip to content

Commit

Permalink
Merge pull request #24 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
refactor: using constants instead of hardcoded values
  • Loading branch information
bilalcaliskan authored Mar 24, 2024
2 parents 0eeac02 + 88b5cdc commit 78e193e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (

fmt.Println(opts)

logger := logging.GetLogger().With().Str("job", "main").Logger()
logger := logging.GetLogger().With().Str("job", constants.JobMain).Logger()
logger.Info().Str("appVersion", ver.GitVersion).Str("goVersion", ver.GoVersion).Str("goOS", ver.GoOs).
Str("goArch", ver.GoArch).Str("gitCommit", ver.GitCommit).Str("buildDate", ver.BuildDate).
Msg(constants.AppStarted)
Expand All @@ -65,7 +65,7 @@ var (
logger.Info().Str("socket", opts.SocketPath).Msg(constants.IPCInitialized)

defer func() {
logger := logger.With().Str("job", "cleanup").Logger()
logger := logger.With().Str("job", constants.JobCleanup).Logger()
logger.Info().Msg(constants.CleaningUpIPC)
if err := ipc.Cleanup(opts.SocketPath); err != nil {
logger.Error().Err(err).Msg(constants.FailedToCleanupIPC)
Expand All @@ -77,7 +77,7 @@ var (
go func() {
// Create a ticker that fires every 5 minutes
ticker := time.NewTicker(time.Duration(int64(opts.CheckIntervalMin)) * time.Minute)
logger := logger.With().Str("job", "ip-change-check").Logger()
logger := logger.With().Str("job", constants.JobIpChangeCheck).Logger()

for range ticker.C {
if <-ticker.C; true {
Expand Down
7 changes: 7 additions & 0 deletions internal/constants/jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constants

const (
JobMain = "main"
JobIpChangeCheck = "ip-change-check"
JobCleanup = "cleanup"
)
2 changes: 0 additions & 2 deletions internal/ipc/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

// InitIPC initializes the IPC setup and continuously listens on the given path for incoming connections
func InitIPC(st *state.State, socketPath string, logger zerolog.Logger) error {
logger = logger.With().Str("job", "ipc").Logger()

// Check and remove the socket file if it already exists
//if _, err := os.Stat(opts.SocketPath); err == nil {
// if err := os.Remove(opts.SocketPath); err != nil {
Expand Down
12 changes: 3 additions & 9 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,16 @@ func NewRouteEntry(domain, gateway string, resolvedIPs []string) *RouteEntry {

func (s *State) CheckIPChanges() error {
if len(s.Entries) == 0 {
s.logger.Info().
Str("job", "ip-change").
Msg("no entries found in the state, skipping ip check")
s.logger.Info().Msg("no entries found in the state, skipping ip check")
return nil
}

if applyNeeded := s.updateEntries(); applyNeeded {
s.logger.Info().
Str("job", "ip-change").
Msg("ip changes detected, applying internal state")
s.logger.Info().Msg("ip changes detected, applying internal state")
return s.Write()
}

s.logger.Info().
Str("job", "ip-change").
Msg("no change, skipping state update")
s.logger.Info().Msg("no change, skipping state update")

return nil
}
Expand Down

0 comments on commit 78e193e

Please sign in to comment.