Move connecting socket to client: network

Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
This commit is contained in:
Helder Correia
2022-03-28 18:06:46 +00:00
parent 75a3ae4204
commit d771699df9
15 changed files with 117 additions and 67 deletions

View File

@@ -79,11 +79,6 @@ func (t clientFilesystemReadTask) readContents(ctx context.Context, pctx *planco
return t.readFS(ctx, pctx, s, v, path)
}
if plancontext.IsServiceValue(contents) {
lg.Debug().Str("path", path).Msg("loading local service")
return t.readService(pctx, v, path)
}
if plancontext.IsSecretValue(contents) {
lg.Debug().Str("path", path).Msg("loading local secret file")
return t.readSecret(pctx, path)
@@ -156,27 +151,6 @@ func (t clientFilesystemReadTask) readFS(ctx context.Context, pctx *plancontext.
return fs.MarshalCUE(), nil
}
func (t clientFilesystemReadTask) readService(pctx *plancontext.Context, v *compiler.Value, path string) (*compiler.Value, error) {
typ, err := v.Lookup("type").String()
if err != nil {
return nil, err
}
var unix, npipe string
switch typ {
case "unix":
unix = path
case "npipe":
npipe = path
default:
return nil, fmt.Errorf("invalid socket type %q", typ)
}
service := pctx.Services.New(unix, npipe)
return service.MarshalCUE(), nil
}
func (t clientFilesystemReadTask) readSecret(pctx *plancontext.Context, path string) (*compiler.Value, error) {
contents, err := t.readString(path)
if err != nil {

View File

@@ -0,0 +1,64 @@
package task
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("ClientNetwork", func() Task { return &clientNetwork{} })
}
type clientNetwork struct {
}
func (t clientNetwork) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
lg := log.Ctx(ctx)
addr, err := v.Lookup("address").String()
if err != nil {
return nil, err
}
u, err := url.Parse(addr)
if err != nil {
return nil, err
}
lg.Debug().Str("type", u.Scheme).Str("path", u.Path).Msg("loading local socket")
if _, err := os.Stat(u.Path); errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("path %q does not exist", u.Path)
}
var unix, npipe string
switch u.Scheme {
case "unix":
unix = u.Path
case "npipe":
npipe = u.Path
default:
return nil, fmt.Errorf("invalid service type %q", u.Scheme)
}
connect := v.Lookup("connect")
if !plancontext.IsServiceValue(connect) {
return nil, fmt.Errorf("wrong type %q", connect.Kind())
}
service := pctx.Services.New(unix, npipe)
return compiler.NewValue().FillFields(map[string]interface{}{
"connect": service.MarshalCUE(),
})
}