diff --git a/internal/portforward/loop.go b/internal/portforward/loop.go index c819317a..23a9566c 100644 --- a/internal/portforward/loop.go +++ b/internal/portforward/loop.go @@ -85,7 +85,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{}, // Stop call takes care of stopping the service return case partialUpdate := <-updateTrigger: - updatedSettings, err := l.settings.updateWith(partialUpdate) + updatedSettings, err := l.settings.updateWith(partialUpdate, *l.settings.VPNIsUp) if err != nil { updateResult <- err continue diff --git a/internal/portforward/service/settings.go b/internal/portforward/service/settings.go index 78f9b9a3..3cd54217 100644 --- a/internal/portforward/service/settings.go +++ b/internal/portforward/service/settings.go @@ -34,17 +34,28 @@ func (s *Settings) OverrideWith(update Settings) { } var ( - ErrServerNameNotSet = errors.New("server name not set") - ErrFilepathNotSet = errors.New("file path not set") - ErrInterfaceNotSet = errors.New("interface not set") + ErrPortForwarderNotSet = errors.New("port forwarder not set") + ErrServerNameNotSet = errors.New("server name not set") + ErrFilepathNotSet = errors.New("file path not set") + ErrInterfaceNotSet = errors.New("interface not set") ) -func (s *Settings) Validate() (err error) { - switch { - // Port forwarder can be nil when the loop updates - // to stop the service. - case s.Filepath == "": +func (s *Settings) Validate(forStartup bool) (err error) { + // Minimal validation + if s.Filepath == "" { return fmt.Errorf("%w", ErrFilepathNotSet) + } + + if !forStartup { + // No additional validation needed if the service + // is not to be started with the given settings. + return nil + } + + // Startup validation requires additional fields set. + switch { + case s.PortForwarder == nil: + return fmt.Errorf("%w", ErrPortForwarderNotSet) case s.Interface == "": return fmt.Errorf("%w", ErrInterfaceNotSet) case s.PortForwarder.Name() == providers.PrivateInternetAccess && s.ServerName == "": diff --git a/internal/portforward/settings.go b/internal/portforward/settings.go index 6ac7d582..02103cad 100644 --- a/internal/portforward/settings.go +++ b/internal/portforward/settings.go @@ -17,10 +17,11 @@ type Settings struct { // fields set in the partialUpdate argument, validates the new settings // and returns them if they are valid, or returns an error otherwise. // In all cases, the receiving settings are unmodified. -func (s Settings) updateWith(partialUpdate Settings) (updated Settings, err error) { +func (s Settings) updateWith(partialUpdate Settings, + forStartup bool) (updated Settings, err error) { updated = s.copy() updated.overrideWith(partialUpdate) - err = updated.validate() + err = updated.validate(forStartup) if err != nil { return updated, err } @@ -38,6 +39,6 @@ func (s *Settings) overrideWith(update Settings) { s.Service.OverrideWith(update.Service) } -func (s Settings) validate() (err error) { - return s.Service.Validate() +func (s Settings) validate(forStartup bool) (err error) { + return s.Service.Validate(forStartup) }