// Execute runs a given root cmd with the given grpc server
Execute runs a given root cmd with the given grpc server
// Execute runs a given root cmd with the given grpc server
func Execute(
	root *cobra.Command,
	server Server,
	preFunc func() error,
) {
	root.RunE = func(cmd *cobra.Command, args []string) error {
		Logger.Info("loaded settings", zap.Any("settings", viper.AllSettings()))
		if err := ConfigurePlugins(); err != nil {
			return err
		}
		if preFunc != nil {
			if err := preFunc(); err != nil {
				return err
			}
		}
		for _, stopable := range Stopables {
			WaitGroup.Add(1)
			go stopable.Run(&WaitGroup)
		}
		networkAddress := viper.GetString(flagNetworkAddress)
		uri, err := url.Parse(networkAddress)
		if err != nil {
			return err
		}
		var address string
		switch uri.Scheme {
		case "tcp":
			address = uri.Host
		case "unix":
			address = uri.Path
		default:
			return fmt.Errorf("%s: %w", uri.Scheme, ErrUnsupportedScheme)
		}
		lis, err := net.Listen(uri.Scheme, address)
		if err != nil {
			return err
		}
		Logger.Info("listening on", zap.String("address", networkAddress))
		go func() {
			sig := <-stop
			Logger.Info("stopping", zap.String("signal", sig.String()))
			switch s := server.(type) {
			case *grpc.Server:
				s.GracefulStop()
			case *http.Server:
				if err := s.Shutdown(context.Background()); err != nil {
					Logger.Error("error shutting down server", zap.Error(err))
				}
			}
			lis.Close()
			for _, stopable := range Stopables {
				stopable.Stop()
			}
			Logger.Info("stopped")
			if err := Logger.Sync(); err != nil {
				Logger.Error("error syncing logs", zap.Error(err))
			}
		}()
		return server.Serve(lis)
	}
	HandleErr(root.Execute())
}
func Execute ( root * cobra . Command , server Server , preFunc func ( ) error , ) { root . RunE = func ( cmd * cobra . Command , args [ ] string ) error { Logger . Info ( " " , zap . Any ( " " , viper . AllSettings ( ) ) ) if err := ConfigurePlugins ( ) ; err != nil { return err } if preFunc != nil { if err := preFunc ( ) ; err != nil { return err } } for _ , stopable := range Stopables { WaitGroup . Add ( 1 ) go stopable . Run ( & WaitGroup ) } networkAddress := viper . GetString ( flagNetworkAddress ) uri , err := url . Parse ( networkAddress ) if err != nil { return err } var address string switch uri . Scheme { case " " : address = uri . Host case " " : address = uri . Path default : return fmt . Errorf ( " " , uri . Scheme , ErrUnsupportedScheme ) } lis , err := net . Listen ( uri . Scheme , address ) if err != nil { return err } Logger . Info ( " " , zap . String ( " " , networkAddress ) ) go func ( ) { sig := <- stop Logger . Info ( " " , zap . String ( " " , sig . String ( ) ) ) switch s := server . ( type ) { case * grpc . Server : s . GracefulStop ( ) case * http . Server : if err := s . Shutdown ( context . Background ( ) ) ; err != nil { Logger . Error ( " " , zap . Error ( err ) ) } } lis . Close ( ) for _ , stopable := range Stopables { stopable . Stop ( ) } Logger . Info ( " " ) if err := Logger . Sync ( ) ; err != nil { Logger . Error ( " " , zap . Error ( err ) ) } } ( ) return server . Serve ( lis ) } HandleErr ( root . Execute ( ) ) }
Execute runs a given root cmd with the given grpc server
Execute runs a given root cmd with the given grpc server
// if configPath := viper.GetString(flagConfigPath); configPath != "" { // 	viper.AddConfigPath(configPath) // } // if err := viper.ReadInConfig(); err != nil { // 	if _, ok := err.(viper.ConfigFileNotFoundError); !ok { // 		HandleErr(err) // 	} // }