Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: handle remove cli subcommand properly at the daemon side #11

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading