Init initializes the Dnote environment and returns a new dnote context apiEndpoint is used when creating a new config file (e.g., from ldflags during tests)
(versionTag, apiEndpoint, dbPath string)
| 106 | // Init initializes the Dnote environment and returns a new dnote context |
| 107 | // apiEndpoint is used when creating a new config file (e.g., from ldflags during tests) |
| 108 | func Init(versionTag, apiEndpoint, dbPath string) (*context.DnoteCtx, error) { |
| 109 | ctx, err := newBaseCtx(versionTag, dbPath) |
| 110 | if err != nil { |
| 111 | return nil, errors.Wrap(err, "initializing a context") |
| 112 | } |
| 113 | |
| 114 | if err := initFiles(ctx, apiEndpoint); err != nil { |
| 115 | return nil, errors.Wrap(err, "initializing files") |
| 116 | } |
| 117 | |
| 118 | if err := InitDB(ctx); err != nil { |
| 119 | return nil, errors.Wrap(err, "initializing database") |
| 120 | } |
| 121 | if err := InitSystem(ctx); err != nil { |
| 122 | return nil, errors.Wrap(err, "initializing system data") |
| 123 | } |
| 124 | |
| 125 | if err := migrate.Legacy(ctx); err != nil { |
| 126 | return nil, errors.Wrap(err, "running legacy migration") |
| 127 | } |
| 128 | if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil { |
| 129 | return nil, errors.Wrap(err, "running migration") |
| 130 | } |
| 131 | |
| 132 | ctx, err = setupCtx(ctx) |
| 133 | if err != nil { |
| 134 | return nil, errors.Wrap(err, "setting up the context") |
| 135 | } |
| 136 | |
| 137 | log.Debug("context: %+v\n", context.Redact(ctx)) |
| 138 | |
| 139 | return &ctx, nil |
| 140 | } |
| 141 | |
| 142 | // setupCtx enriches the base context with values from config file and database. |
| 143 | // This is called after files and database have been initialized. |