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 purge command #16

Merged
merged 1 commit into from
Feb 12, 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
4 changes: 3 additions & 1 deletion cmd/cli/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return utils.ErrTooManyArgs
Expand All @@ -37,7 +39,7 @@ to quickly create a Cobra application.`,
if err != nil {
logger.Error().Str("command", cmd.Name()).Err(err).Msg(constants.FailedToProcessCommand)

return &utils.CommandError{Err: err, Code: 10}
return &utils.CommandError{Err: err, Code: 12}
}

logger.Info().
Expand Down
1 change: 1 addition & 0 deletions internal/constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ const (
FailedToInitializeIPC = "failed to initialize IPC"
FailedToRemoveRouteEntry = "failed to remove RouteEntry from state"
EntryAlreadyExists = "route entry already exists in state"
NoRoutesToPurge = "no routes to purge"
)
14 changes: 14 additions & 0 deletions internal/ipc/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ func handleAddCommand(logger zerolog.Logger, gw string, domains []string, conn n
func handlePurgeCommand(logger zerolog.Logger, conn net.Conn, st *state.State) {
logger = logger.With().Str("operation", "purge").Logger()

if len(st.Entries) == 0 {
if err := writeResponse(&DaemonResponse{
Success: false,
Response: "",
Error: errors.New(constants.NoRoutesToPurge).Error(),
}, conn); err != nil {
logger.Error().
Err(err).
Msg(constants.FailedToWriteToUnixDomainSocket)
}

return
}

for _, entry := range st.Entries {
for _, ip := range entry.ResolvedIPs {
if err := utils.RemoveRoute(ip); err != nil {
Expand Down
Loading