Add context
This commit adds and passes context around to (almost) every function. This allows cancelling (almost) every function call globally.
This commit is contained in:
parent
996ecb9f80
commit
3455f491ca
104 changed files with 836 additions and 511 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package extras
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
|
|
@ -22,8 +23,8 @@ var pathbuilderPHP string
|
|||
// All returns the ids of all pathbuilders in consistent order.
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) All(server *phpx.Server) (ids []string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &ids, pathbuilderPHP, "all_list")
|
||||
func (pathbuilder *Pathbuilder) All(ctx context.Context, server *phpx.Server) (ids []string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(ctx, server, &ids, pathbuilderPHP, "all_list")
|
||||
slices.Sort(ids)
|
||||
return
|
||||
}
|
||||
|
|
@ -32,16 +33,16 @@ func (pathbuilder *Pathbuilder) All(server *phpx.Server) (ids []string, err erro
|
|||
// If it does not exist, it returns the empty string and nil error.
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) Get(server *phpx.Server, id string) (xml string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &xml, pathbuilderPHP, "one_xml", id)
|
||||
func (pathbuilder *Pathbuilder) Get(ctx context.Context, server *phpx.Server, id string) (xml string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(ctx, server, &xml, pathbuilderPHP, "one_xml", id)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAll returns all pathbuilders serialized as xml
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) GetAll(server *phpx.Server) (pathbuilders map[string]string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &pathbuilders, pathbuilderPHP, "all_xml")
|
||||
func (pathbuilder *Pathbuilder) GetAll(ctx context.Context, server *phpx.Server) (pathbuilders map[string]string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(ctx, server, &pathbuilders, pathbuilderPHP, "all_xml")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +51,6 @@ func (pathbuilder *Pathbuilder) Fetch(flags ingredient.FetcherFlags, info *statu
|
|||
return
|
||||
}
|
||||
|
||||
info.Pathbuilders, _ = pathbuilder.GetAll(flags.Server)
|
||||
info.Pathbuilders, _ = pathbuilder.GetAll(flags.Context, flags.Server)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package extras
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
|
|
@ -37,8 +38,8 @@ var listURIPrefixesPHP string
|
|||
//
|
||||
// server is an optional server to fetch prefixes from.
|
||||
// server may be nil.
|
||||
func (prefixes *Prefixes) All(server *phpx.Server) ([]string, error) {
|
||||
uris, err := prefixes.database(server)
|
||||
func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]string, error) {
|
||||
uris, err := prefixes.database(ctx, server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -51,9 +52,9 @@ func (prefixes *Prefixes) All(server *phpx.Server) ([]string, error) {
|
|||
return append(uris, uris2...), nil
|
||||
}
|
||||
|
||||
func (wisski *Prefixes) database(server *phpx.Server) (prefixes []string, err error) {
|
||||
func (wisski *Prefixes) database(ctx context.Context, server *phpx.Server) (prefixes []string, err error) {
|
||||
// get all the ugly prefixes
|
||||
err = wisski.PHP.ExecScript(server, &prefixes, listURIPrefixesPHP, "list_prefixes")
|
||||
err = wisski.PHP.ExecScript(ctx, server, &prefixes, listURIPrefixesPHP, "list_prefixes")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -143,28 +144,28 @@ func (wisski *Prefixes) filePrefixes() (prefixes []string, err error) {
|
|||
var prefix = mstore.For[string]("prefix")
|
||||
|
||||
// Prefixes returns the cached prefixes from the given instance
|
||||
func (wisski *Prefixes) AllCached() (results []string, err error) {
|
||||
return prefix.GetAll(wisski.MStore)
|
||||
func (wisski *Prefixes) AllCached(ctx context.Context) (results []string, err error) {
|
||||
return prefix.GetAll(ctx, wisski.MStore)
|
||||
}
|
||||
|
||||
// Update updates the cached prefixes of this instance
|
||||
func (wisski *Prefixes) Update() error {
|
||||
prefixes, err := wisski.All(nil)
|
||||
func (wisski *Prefixes) Update(ctx context.Context) error {
|
||||
prefixes, err := wisski.All(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return prefix.SetAll(wisski.MStore, prefixes...)
|
||||
return prefix.SetAll(ctx, wisski.MStore, prefixes...)
|
||||
}
|
||||
|
||||
func (prefixes *Prefixes) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
|
||||
info.NoPrefixes = prefixes.NoPrefix()
|
||||
if flags.Quick {
|
||||
// quick mode: grab only the cached prefixes
|
||||
info.Prefixes, _ = prefixes.AllCached()
|
||||
info.Prefixes, _ = prefixes.AllCached(flags.Context)
|
||||
} else {
|
||||
// slow mode: grab the fresh prefixes from the server
|
||||
// TODO: Do we want to update them while we are at it?
|
||||
info.Prefixes, _ = prefixes.All(flags.Server)
|
||||
info.Prefixes, _ = prefixes.All(flags.Context, flags.Server)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package extras
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
|
|
@ -17,11 +18,11 @@ type Settings struct {
|
|||
//go:embed settings.php
|
||||
var settingsPHP string
|
||||
|
||||
func (settings *Settings) Get(server *phpx.Server, key string) (value any, err error) {
|
||||
err = settings.PHP.ExecScript(server, &value, settingsPHP, "get_setting", key)
|
||||
func (settings *Settings) Get(ctx context.Context, server *phpx.Server, key string) (value any, err error) {
|
||||
err = settings.PHP.ExecScript(ctx, server, &value, settingsPHP, "get_setting", key)
|
||||
return
|
||||
}
|
||||
|
||||
func (settings *Settings) Set(server *phpx.Server, key string, value any) error {
|
||||
return settings.PHP.ExecScript(server, nil, settingsPHP, "set_setting", key, value)
|
||||
func (settings *Settings) Set(ctx context.Context, server *phpx.Server, key string, value any) error {
|
||||
return settings.PHP.ExecScript(ctx, server, nil, settingsPHP, "set_setting", key, value)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package extras
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"log"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
|
|
@ -20,11 +20,8 @@ type Stats struct {
|
|||
var statsPHP string
|
||||
|
||||
// Get fetches all statistics from the server
|
||||
func (stats *Stats) Get(server *phpx.Server) (data status.Statistics, err error) {
|
||||
err = stats.PHP.ExecScript(server, &data, statsPHP, "export_statistics")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
func (stats *Stats) Get(ctx context.Context, server *phpx.Server) (data status.Statistics, err error) {
|
||||
err = stats.PHP.ExecScript(ctx, server, &data, statsPHP, "export_statistics")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -33,6 +30,6 @@ func (stats *Stats) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (e
|
|||
return
|
||||
}
|
||||
|
||||
info.Statistics, _ = stats.Get(flags.Server)
|
||||
info.Statistics, _ = stats.Get(flags.Context, flags.Server)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package php
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
|
|
@ -28,7 +29,7 @@ type PHP struct {
|
|||
// It's arguments are encoded as json using [json.Marshal] and decoded within php.
|
||||
//
|
||||
// The return value of the function is again marshaled with json and returned to the caller.
|
||||
func (php *PHP) ExecScript(server *phpx.Server, value any, code string, entrypoint string, args ...any) (err error) {
|
||||
func (php *PHP) ExecScript(ctx context.Context, server *phpx.Server, value any, code string, entrypoint string, args ...any) (err error) {
|
||||
if server == nil {
|
||||
server = php.NewServer()
|
||||
if err != nil {
|
||||
|
|
@ -38,15 +39,15 @@ func (php *PHP) ExecScript(server *phpx.Server, value any, code string, entrypoi
|
|||
}
|
||||
|
||||
if code != "" {
|
||||
if err := server.MarshalEval(nil, strings.TrimPrefix(code, "<?php")); err != nil {
|
||||
if err := server.MarshalEval(ctx, nil, strings.TrimPrefix(code, "<?php")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return server.MarshalCall(value, entrypoint, args...)
|
||||
return server.MarshalCall(ctx, value, entrypoint, args...)
|
||||
}
|
||||
|
||||
func (php *PHP) EvalCode(server *phpx.Server, value any, code string) (err error) {
|
||||
func (php *PHP) EvalCode(ctx context.Context, server *phpx.Server, value any, code string) (err error) {
|
||||
if server == nil {
|
||||
server = php.NewServer()
|
||||
if err != nil {
|
||||
|
|
@ -55,5 +56,5 @@ func (php *PHP) EvalCode(server *phpx.Server, value any, code string) (err error
|
|||
defer server.Close()
|
||||
}
|
||||
|
||||
return server.MarshalEval(value, code)
|
||||
return server.MarshalEval(ctx, value, code)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package php
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
|
|
@ -14,11 +15,12 @@ import (
|
|||
// See [PHPServer].
|
||||
func (php *PHP) NewServer() *phpx.Server {
|
||||
return &phpx.Server{
|
||||
Context: context.Background(),
|
||||
Executor: phpx.SpawnFunc(php.spawn),
|
||||
}
|
||||
}
|
||||
|
||||
func (php *PHP) spawn(str stream.IOStream, code string) error {
|
||||
_, err := php.Barrel.Shell(str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))
|
||||
func (php *PHP) spawn(ctx context.Context, str stream.IOStream, code string) error {
|
||||
_, err := php.Barrel.Shell(ctx, str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@ import (
|
|||
|
||||
var errGetValidator = errors.New("GetPasswordValidator: Unknown Error")
|
||||
|
||||
func (u *Users) GetPasswordValidator(username string) (pv PasswordValidator, err error) {
|
||||
func (u *Users) GetPasswordValidator(ctx context.Context, username string) (pv PasswordValidator, err error) {
|
||||
server := u.PHP.NewServer()
|
||||
|
||||
var hash string
|
||||
err = u.PHP.ExecScript(server, &hash, usersPHP, "get_password_hash", username)
|
||||
err = u.PHP.ExecScript(ctx, server, &hash, usersPHP, "get_password_hash", username)
|
||||
if err != nil {
|
||||
server.Close()
|
||||
return pv, err
|
||||
|
|
@ -46,9 +46,9 @@ func (pv PasswordValidator) Close() error {
|
|||
return pv.server.Close()
|
||||
}
|
||||
|
||||
func (pv PasswordValidator) Check(password string) bool {
|
||||
func (pv PasswordValidator) Check(ctx context.Context, password string) bool {
|
||||
var result phpx.Boolean
|
||||
err := pv.server.MarshalCall(&result, "check_password_hash", password, string(pv.hash))
|
||||
err := pv.server.MarshalCall(ctx, &result, "check_password_hash", password, string(pv.hash))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
|
@ -65,10 +65,10 @@ func (cpe CommonPasswordError) Error() string {
|
|||
return fmt.Sprintf("%q from %q", cpe.Password.Password, cpe.Password.Source)
|
||||
}
|
||||
|
||||
func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.Writer) error {
|
||||
func (pv PasswordValidator) CheckDictionary(ctx context.Context, writer io.Writer) error {
|
||||
var counter int
|
||||
|
||||
if pv.Check(pv.username) {
|
||||
if pv.Check(ctx, pv.username) {
|
||||
if writer != nil {
|
||||
counter++
|
||||
fmt.Fprintln(writer, counter)
|
||||
|
|
@ -76,10 +76,10 @@ func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.W
|
|||
return errPasswordUsername
|
||||
}
|
||||
for candidate := range CommonPasswords() {
|
||||
if context.Err() != nil {
|
||||
if ctx.Err() != nil {
|
||||
continue
|
||||
}
|
||||
result := pv.Check(candidate.Password)
|
||||
result := pv.Check(ctx, candidate.Password)
|
||||
if writer != nil {
|
||||
counter++
|
||||
fmt.Fprintln(writer, counter)
|
||||
|
|
@ -90,7 +90,7 @@ func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.W
|
|||
}
|
||||
}
|
||||
|
||||
return context.Err()
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
//go:embed passwords
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"net/url"
|
||||
|
|
@ -21,19 +22,19 @@ type Users struct {
|
|||
var usersPHP string
|
||||
|
||||
// All returns all known usernames
|
||||
func (u *Users) All(server *phpx.Server) (users []status.User, err error) {
|
||||
err = u.PHP.ExecScript(server, &users, usersPHP, "list_users")
|
||||
func (u *Users) All(ctx context.Context, server *phpx.Server) (users []status.User, err error) {
|
||||
err = u.PHP.ExecScript(ctx, server, &users, usersPHP, "list_users")
|
||||
return
|
||||
}
|
||||
|
||||
var errLoginUnknownError = errors.New("Login: Unknown Error")
|
||||
|
||||
// Login generates a login link for the user with the given username
|
||||
func (u *Users) Login(server *phpx.Server, username string) (dest *url.URL, err error) {
|
||||
func (u *Users) Login(ctx context.Context, server *phpx.Server, username string) (dest *url.URL, err error) {
|
||||
|
||||
// generate a (relative) link
|
||||
var path string
|
||||
err = u.PHP.ExecScript(server, &path, usersPHP, "get_login_link", username)
|
||||
err = u.PHP.ExecScript(ctx, server, &path, usersPHP, "get_login_link", username)
|
||||
|
||||
// if something went wrong, return
|
||||
if err != nil {
|
||||
|
|
@ -57,9 +58,9 @@ func (u *Users) Login(server *phpx.Server, username string) (dest *url.URL, err
|
|||
var errSetPassword = errors.New("SetPassword: Unknown Error")
|
||||
|
||||
// SetPassword sets the password for a given user
|
||||
func (u *Users) SetPassword(server *phpx.Server, username, password string) error {
|
||||
func (u *Users) SetPassword(ctx context.Context, server *phpx.Server, username, password string) error {
|
||||
var ok bool
|
||||
err := u.PHP.ExecScript(server, &ok, usersPHP, "set_user_password", username, password)
|
||||
err := u.PHP.ExecScript(ctx, server, &ok, usersPHP, "set_user_password", username, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -74,6 +75,6 @@ func (u *Users) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err e
|
|||
return
|
||||
}
|
||||
|
||||
info.Users, _ = u.All(flags.Server)
|
||||
info.Users, _ = u.All(flags.Context, flags.Server)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue