From 88b5cdc8e4ea3c50bdd80d01b5b271f495433ba3 Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Sun, 24 Mar 2024 12:27:19 +0300 Subject: [PATCH] refactor: using constants instead of hardcoded values --- cmd/daemon/daemon.go | 6 +++--- internal/constants/jobs.go | 7 +++++++ internal/ipc/ipc.go | 2 -- internal/state/state.go | 12 +++--------- 4 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 internal/constants/jobs.go diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index be61b70..e930fbf 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -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) @@ -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) @@ -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 { diff --git a/internal/constants/jobs.go b/internal/constants/jobs.go new file mode 100644 index 0000000..c6013e5 --- /dev/null +++ b/internal/constants/jobs.go @@ -0,0 +1,7 @@ +package constants + +const ( + JobMain = "main" + JobIpChangeCheck = "ip-change-check" + JobCleanup = "cleanup" +) diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go index ec02566..24fb738 100644 --- a/internal/ipc/ipc.go +++ b/internal/ipc/ipc.go @@ -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 { diff --git a/internal/state/state.go b/internal/state/state.go index b979755..90282a9 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -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 }