wdcli: Use progress writer instead of IOStream

This commit is contained in:
Tom Wiesing 2022-11-30 11:39:29 +01:00
parent 890022ae64
commit 3b78b06fff
No known key found for this signature in database
49 changed files with 396 additions and 393 deletions

View file

@ -3,13 +3,13 @@ package home
import (
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/goprogram/stream"
)
type Home struct {
@ -30,10 +30,10 @@ var (
func (*Home) Routes() []string { return []string{"/"} }
func (home *Home) Handler(ctx context.Context, route string, io stream.IOStream) (http.Handler, error) {
home.updateRedirect(ctx, io)
home.updateInstances(ctx, io)
home.updateRender(ctx, io)
func (home *Home) Handler(ctx context.Context, route string, progress io.Writer) (http.Handler, error) {
home.updateRedirect(ctx, progress)
home.updateInstances(ctx, progress)
home.updateRender(ctx, progress)
return home, nil
}

View file

@ -3,6 +3,8 @@ package home
import (
"bytes"
"context"
"fmt"
"io"
"time"
_ "embed"
@ -10,14 +12,13 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
"github.com/tkw1536/goprogram/stream"
"golang.org/x/sync/errgroup"
)
func (home *Home) updateInstances(ctx context.Context, io stream.IOStream) {
func (home *Home) updateInstances(ctx context.Context, progress io.Writer) {
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading instance list\n", t.Format(time.Stamp))
fmt.Fprintf(progress, "[%s]: reloading instance list\n", t.Format(time.Stamp))
err := (func() error {
ctx, cancel := context.WithTimeout(ctx, home.RefreshInterval)
@ -32,7 +33,7 @@ func (home *Home) updateInstances(ctx context.Context, io stream.IOStream) {
return nil
})()
if err != nil {
io.EPrintf("error reloading instances: ", err.Error())
fmt.Fprintf(progress, "error reloading instances: %s", err.Error())
}
}
}()
@ -51,10 +52,10 @@ func (home *Home) instanceMap(ctx context.Context) (map[string]struct{}, error)
return names, nil
}
func (home *Home) updateRender(ctx context.Context, io stream.IOStream) {
func (home *Home) updateRender(ctx context.Context, progress io.Writer) {
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading home render list\n", t.Format(time.Stamp))
fmt.Fprintf(progress, "[%s]: reloading home render list\n", t.Format(time.Stamp))
err := (func() error {
ctx, cancel := context.WithTimeout(ctx, home.RefreshInterval)
@ -69,7 +70,7 @@ func (home *Home) updateRender(ctx context.Context, io stream.IOStream) {
return nil
})()
if err != nil {
io.EPrintf("error reloading instances: ", err.Error())
fmt.Fprintf(progress, "error reloading instances: %s", err.Error())
}
}
}()

View file

@ -3,18 +3,19 @@ package home
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
"github.com/tkw1536/goprogram/stream"
)
func (home *Home) updateRedirect(ctx context.Context, io stream.IOStream) {
func (home *Home) updateRedirect(ctx context.Context, progress io.Writer) {
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading overrides\n", t.Format(time.Stamp))
fmt.Fprintf(progress, "[%s]: reloading overrides\n", t.Format(time.Stamp))
err := (func() error {
ctx, cancel := context.WithTimeout(ctx, home.RefreshInterval)
@ -29,7 +30,7 @@ func (home *Home) updateRedirect(ctx context.Context, io stream.IOStream) {
return nil
})()
if err != nil {
io.EPrintf("error reloading overrides: ", err.Error())
fmt.Fprintf(progress, "error reloading overrides: %s", err.Error())
}
}

View file

@ -2,6 +2,7 @@ package info
import (
"context"
"io"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
@ -12,7 +13,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/tkw1536/goprogram/stream"
)
type Info struct {
@ -33,7 +33,7 @@ var (
func (*Info) Routes() []string { return []string{"/dis/"} }
func (info *Info) Handler(ctx context.Context, route string, io stream.IOStream) (handler http.Handler, err error) {
func (info *Info) Handler(ctx context.Context, route string, progress io.Writer) (handler http.Handler, err error) {
router := mux.NewRouter()
{
socket := &httpx.WebSocket{

View file

@ -3,28 +3,29 @@ package info
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/tkw1536/goprogram/status"
"github.com/tkw1536/goprogram/stream"
)
type InstanceAction struct {
NumParams int
HandleInteractive func(ctx context.Context, info *Info, instance *wisski.WissKI, str stream.IOStream, params ...string) error
HandleInteractive func(ctx context.Context, info *Info, instance *wisski.WissKI, out io.Writer, params ...string) error
HandleResult func(ctx context.Context, info *Info, instance *wisski.WissKI, params ...string) (value any, err error)
}
var socketInstanceActions = map[string]InstanceAction{
"snapshot": {
HandleInteractive: func(ctx context.Context, info *Info, instance *wisski.WissKI, str stream.IOStream, params ...string) error {
HandleInteractive: func(ctx context.Context, info *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
return info.Exporter.MakeExport(
ctx,
str,
out,
exporter.ExportTask{
Dest: "",
Instance: instance,
@ -35,17 +36,17 @@ var socketInstanceActions = map[string]InstanceAction{
},
},
"rebuild": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, str stream.IOStream, params ...string) error {
return instance.Barrel().Build(ctx, str, true)
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
return instance.Barrel().Build(ctx, out, true)
},
},
"update": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, str stream.IOStream, params ...string) error {
return instance.Drush().Update(ctx, str)
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, out io.Writer, params ...string) error {
return instance.Drush().Update(ctx, out)
},
},
"cron": {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, str stream.IOStream, params ...string) error {
HandleInteractive: func(ctx context.Context, _ *Info, instance *wisski.WissKI, str io.Writer, params ...string) error {
return instance.Drush().Cron(ctx, str)
},
},
@ -108,33 +109,30 @@ func (info *Info) handleInstanceAction(conn httpx.WebSocketConnection, action In
}
defer writer.Close()
str := stream.NewIOStream(writer, writer, nil, 0)
// handle the interactive action
if action.HandleInteractive != nil {
err := action.HandleInteractive(conn.Context(), info, instance, str, params...)
err := action.HandleInteractive(conn.Context(), info, instance, writer, params...)
if err != nil {
str.EPrintln(err)
fmt.Fprintln(writer, err)
return
}
str.Println("done")
fmt.Fprintln(writer, "done")
}
// handle the result computation
if action.HandleResult != nil {
result, err := action.HandleResult(conn.Context(), info, instance, params...)
if err != nil {
str.Println("false")
fmt.Fprintln(writer, "false")
return
}
data, err := json.Marshal(result)
if err != nil {
str.Println("false")
fmt.Fprintln(writer, "false")
return
}
data = append(data, "\n"...)
str.Println("true")
str.Stdout.Write(data)
fmt.Fprintln(writer, "true")
fmt.Fprintln(writer, data)
}
}

View file

@ -2,24 +2,24 @@ package control
import (
"context"
"fmt"
"io"
"net/http"
"github.com/tkw1536/goprogram/stream"
)
// Server returns an http.Mux that implements the main server instance.
// The server may spawn background tasks, but these should be terminated once context closes.
//
// Logging messages are directed to io.
func (control *Control) Server(ctx context.Context, io stream.IOStream) (*http.ServeMux, error) {
// Logging messages are directed to progress
func (control *Control) Server(ctx context.Context, progress io.Writer) (*http.ServeMux, error) {
// create a new mux
mux := http.NewServeMux()
// add all the servable routes!
for _, s := range control.Servables {
for _, route := range s.Routes() {
io.Printf("mounting %s\n", route)
handler, err := s.Handler(ctx, route, io)
fmt.Fprintf(progress, "mounting %s\n", route)
handler, err := s.Handler(ctx, route, progress)
if err != nil {
return nil, err
}

View file

@ -4,11 +4,11 @@ package static
import (
"context"
"embed"
"io"
"io/fs"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/tkw1536/goprogram/stream"
)
type Static struct {
@ -24,7 +24,7 @@ func (*Static) Routes() []string { return []string{"/static/"} }
//go:embed dist
var staticFS embed.FS
func (static *Static) Handler(ctx context.Context, route string, io stream.IOStream) (http.Handler, error) {
func (static *Static) Handler(ctx context.Context, route string, progress io.Writer) (http.Handler, error) {
// take the filesystem
fs, err := fs.Sub(staticFS, "dist")
if err != nil {