Skip to content

Commit

Permalink
Merge pull request #11 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
refactor: handle remove cli subcommand properly at the daemon side
  • Loading branch information
bilalcaliskan authored Feb 8, 2024
2 parents dab9238 + 62da82c commit 43f65d8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ const (
FailedToCleanupIPC = "failed to cleanup IPC"
FailedToInitializeIPC = "failed to initialize IPC"
FailedToRemoveRouteEntry = "failed to remove RouteEntry from state"
EntryAlreadyExists = "route entry already exists in state"
)
2 changes: 1 addition & 1 deletion internal/ipc/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func handleRemoveCommand(logger zerolog.Logger, gw string, domains []string, con
if err := writeResponse(&DaemonResponse{
Success: false,
Response: "",
Error: errors.New(constants.EntryNotFound).Error(),
Error: errors.Wrap(errors.New(constants.EntryNotFound), constants.FailedToRemoveRouteEntry).Error(),
}, conn); err != nil {
logger.Error().
Err(err).
Expand Down
10 changes: 6 additions & 4 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func NewRouteEntry(domain, resolvedIP, gateway string) *RouteEntry {
func (s *State) AddEntry(entry *RouteEntry) error {
for _, e := range s.Entries {
if e.Domain == entry.Domain {
if e.ResolvedIP != entry.ResolvedIP {
e.ResolvedIP = entry.ResolvedIP
if e.ResolvedIP == entry.ResolvedIP {
return errors.New(constants.EntryAlreadyExists)
}
return nil

e.ResolvedIP = entry.ResolvedIP
return s.Write("/tmp/state.json")
}
}

Expand All @@ -56,7 +58,7 @@ func (s *State) RemoveEntry(domain string) error {
for i, entry := range s.Entries {
if entry.Domain == domain {
s.Entries = append(s.Entries[:i], s.Entries[i+1:]...)
return nil
return s.Write("/tmp/state.json")
}
}

Expand Down

0 comments on commit 43f65d8

Please sign in to comment.