Execute initializes the application (if not already) and executes the pb.RootCmd with graceful shutdown support. This method differs from pb.Start() by not registering the default system commands!
()
| 177 | // This method differs from pb.Start() by not registering the default |
| 178 | // system commands! |
| 179 | func (pb *PocketBase) Execute() error { |
| 180 | if !pb.skipBootstrap() { |
| 181 | if err := pb.Bootstrap(); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | done := make(chan bool, 1) |
| 187 | |
| 188 | // listen for interrupt signal to gracefully shutdown the application |
| 189 | go func() { |
| 190 | sigch := make(chan os.Signal, 1) |
| 191 | signal.Notify(sigch, os.Interrupt, syscall.SIGTERM) |
| 192 | <-sigch |
| 193 | |
| 194 | done <- true |
| 195 | }() |
| 196 | |
| 197 | // execute the root command |
| 198 | go func() { |
| 199 | // note: leave to the commands to decide whether to print their error |
| 200 | pb.RootCmd.Execute() |
| 201 | |
| 202 | done <- true |
| 203 | }() |
| 204 | |
| 205 | <-done |
| 206 | |
| 207 | // trigger cleanups |
| 208 | // |
| 209 | // @todo consider skipping and just call the finalizer in case OnTerminate was already invoked manually? |
| 210 | event := new(core.TerminateEvent) |
| 211 | event.App = pb |
| 212 | return pb.OnTerminate().Trigger(event, func(e *core.TerminateEvent) error { |
| 213 | return e.App.ResetBootstrapState() |
| 214 | }) |
| 215 | } |
| 216 | |
| 217 | // eagerParseFlags parses the global app flags before calling pb.RootCmd.Execute(). |
| 218 | // so we can have all PocketBase flags ready for use on initialization. |