fixed code smells: string literal repetition + cyclomatic complexity

This commit is contained in:
A.Unger
2021-11-17 12:25:21 +01:00
parent c567eb6eea
commit aa4112e2e2
7 changed files with 95 additions and 98 deletions
+22 -18
View File
@@ -124,10 +124,6 @@ func NewService(options ...Option) (*Service, error) {
// Start an rpc service. By default the package scope Start will run all default extensions to provide with a working
// oCIS instance.
func Start(o ...Option) error {
// Start the runtime. Most likely this was called ONLY by the `ocis server` subcommand, but since we cannot protect
// from the caller, the previous statement holds truth.
// prepare a new rpc Service struct.
s, err := NewService(o...)
if err != nil {
return err
@@ -171,10 +167,7 @@ func Start(o ...Option) error {
s.cfg.Storage.Log = &shared.Log{}
}
s.cfg.Storage.Log.Color = s.cfg.Commons.Color
s.cfg.Storage.Log.Level = s.cfg.Commons.Level
s.cfg.Storage.Log.Pretty = s.cfg.Commons.Pretty
s.cfg.Storage.Log.File = s.cfg.Commons.File
propagateLoggingCommonsToStorages(s)
if err = rpc.Register(s); err != nil {
if s != nil {
@@ -188,16 +181,7 @@ func Start(o ...Option) error {
s.Log.Fatal().Err(err)
}
defer func() {
if r := recover(); r != nil {
reason := strings.Builder{}
if _, err := net.Dial("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port)); err != nil {
reason.WriteString("runtime address already in use")
}
fmt.Println(reason.String())
}
}()
defer gracefulRecovery(s)
// prepare the set of services to run
s.generateRunSet(s.cfg)
@@ -222,6 +206,26 @@ func Start(o ...Option) error {
return http.Serve(l, nil)
}
func propagateLoggingCommonsToStorages(s *Service) {
s.cfg.Storage.Log.Color = s.cfg.Commons.Color
s.cfg.Storage.Log.Level = s.cfg.Commons.Level
s.cfg.Storage.Log.Pretty = s.cfg.Commons.Pretty
s.cfg.Storage.Log.File = s.cfg.Commons.File
}
func gracefulRecovery(s *Service) {
func() {
if r := recover(); r != nil {
reason := strings.Builder{}
if _, err := net.Dial("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port)); err != nil {
reason.WriteString("runtime address already in use")
}
fmt.Println(reason.String())
}
}()
}
// scheduleServiceTokens adds service tokens to the service supervisor.
func scheduleServiceTokens(s *Service, funcSet serviceFuncMap) {
for _, name := range runset {