Skip to content

Commit

Permalink
Use contexts
Browse files Browse the repository at this point in the history
Also:

* Update dependencies
* Small fix to Ruby wrapper
  • Loading branch information
tliron committed May 25, 2023
1 parent 0eab560 commit 149f12e
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 100 deletions.
5 changes: 3 additions & 2 deletions clout/js/clout-api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package js

import (
contextpkg "context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -30,13 +31,13 @@ func (self *Context) NewCloutAPI(clout *cloutpkg.Clout, jsContext *js.Context) *
}
}

func (self *CloutAPI) Load(data any) (*CloutAPI, error) {
func (self *CloutAPI) Load(context contextpkg.Context, data any) (*CloutAPI, error) {
var clout *cloutpkg.Clout
var err error

switch data_ := data.(type) {
case string:
if clout, err = cloutpkg.Load(data_, "", self.cloutContext.Context.URLContext); err != nil {
if clout, err = cloutpkg.Load(context, data_, "", self.cloutContext.Context.URLContext); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion clout/js/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (self *Context) NewEnvironment(clout *cloutpkg.Clout, apis map[string]any)
environment.CreateResolver = func(url exturl.URL, context *js.Context) js.ResolveFunc {
return func(id string, raw bool) (exturl.URL, error) {
if scriptlet, err := GetScriptlet(id, clout); err == nil {
url := exturl.NewInternalURL(id, self.URLContext)
url := self.URLContext.NewInternalURL(id)
url.SetContent(scriptlet)
return url, nil
} else {
Expand Down
6 changes: 4 additions & 2 deletions clout/js/puccini-api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package js

import (
contextpkg "context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -108,8 +109,9 @@ func (self *PucciniAPI) Write(data any, path string, dontOverwrite bool) {
}

func (self *PucciniAPI) LoadString(url string) (string, error) {
if url_, err := exturl.NewValidURL(url, nil, self.context.URLContext); err == nil {
return exturl.ReadString(url_)
context := contextpkg.TODO()
if url_, err := self.context.URLContext.NewValidURL(context, url, nil); err == nil {
return exturl.ReadString(context, url_)
} else {
return "", err
}
Expand Down
12 changes: 8 additions & 4 deletions clout/load.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package clout

import (
contextpkg "context"

"github.com/tliron/exturl"
"github.com/tliron/kutil/util"
)

func Load(url string, format string, urlContext *exturl.Context) (*Clout, error) {
func Load(context contextpkg.Context, url string, format string, urlContext *exturl.Context) (*Clout, error) {
var url_ exturl.URL

var err error
if url != "" {
if url_, err = exturl.NewValidURL(url, nil, urlContext); err != nil {
if url_, err = urlContext.NewValidURL(context, url, nil); err != nil {
return nil, err
}
} else {
if url_, err = exturl.ReadToInternalURLFromStdin(format, urlContext); err != nil {
if url_, err = urlContext.ReadToInternalURLFromStdin(context, format); err != nil {
return nil, err
}
}

if reader, err := url_.Open(); err == nil {
if reader, err := url_.Open(context); err == nil {
reader = util.NewContextualReadCloser(context, reader)
defer reader.Close()
return Read(reader, url_.Format())
} else {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cobra v1.7.0
github.com/tliron/commonlog v0.1.0
github.com/tliron/exturl v0.1.0
github.com/tliron/go-ard v0.1.0
github.com/tliron/kutil v0.2.1
github.com/tliron/exturl v0.2.0
github.com/tliron/go-ard v0.1.1
github.com/tliron/kutil v0.2.2
github.com/tliron/yamlkeys v1.3.6
github.com/vmihailenco/msgpack/v5 v5.3.5
)
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tliron/commonlog v0.1.0 h1:EsNHnG/oPSJ8BqTjIRxLc/lmurz5cyUuoKe7CabmHfg=
github.com/tliron/commonlog v0.1.0/go.mod h1:aF5mXemxKqtWPIvsexXjgKXWLkXnBUt33mqek5E0UJc=
github.com/tliron/exturl v0.1.0 h1:SduF2opgjjrjABgp70dhDS1Kcm9ThCNMhvZf/JoAsSQ=
github.com/tliron/exturl v0.1.0/go.mod h1:Y/8ktoPMD+PVpIkTU5tAZqYZUWV0nLTlRXPB3iYK3+Q=
github.com/tliron/go-ard v0.1.0 h1:Vl6/vfsidKayb5GmppasrVy5Sz8MrDeyumVG4CE7XoI=
github.com/tliron/go-ard v0.1.0/go.mod h1:cbb68FvbNUyd3PbAQzf3sFY1ZVW4ADW3YkpzOk+4uaU=
github.com/tliron/kutil v0.2.1 h1:yZU7XNSNK6EP0oxu/oo49hAaxrOFI4QoVDd1Ov4xqeE=
github.com/tliron/kutil v0.2.1/go.mod h1:O7uioslKYBifB7zygq5jx3uCqGUXLiTPYImjssulAqU=
github.com/tliron/exturl v0.2.0 h1:0+aAu35tPzlY7kUKzRbKAetD6x3YOwRqD90f0q8RBNY=
github.com/tliron/exturl v0.2.0/go.mod h1:FhuTLDbfDOYbYV33qlbx14+Lsx8hhgnTKS06nt7GZig=
github.com/tliron/go-ard v0.1.1 h1:Z0JD63j387RmjiJNuMHEmj6+ht3Aj0/tY3sSbv/MzRU=
github.com/tliron/go-ard v0.1.1/go.mod h1:b8BG7fxGd/trwh9zD6FH797xgRLa9g6SN+sR4xrkzEA=
github.com/tliron/kutil v0.2.2 h1:1mQs7+D+CQf5JMdD91uMEcTj7yue3cdad5GS0yt/EwQ=
github.com/tliron/kutil v0.2.2/go.mod h1:O7uioslKYBifB7zygq5jx3uCqGUXLiTPYImjssulAqU=
github.com/tliron/yamlkeys v1.3.6 h1:PPV4q7flMqIvmSUSsEZuns7Qt3VIMxkhBj+6KTRvI9c=
github.com/tliron/yamlkeys v1.3.6/go.mod h1:K/uKQwMke5a9h6YW/Sj9pcp66vU3lRP97OrOjo/ELoU=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
Expand Down
6 changes: 4 additions & 2 deletions library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"C"
"bytes"
contextpkg "context"
"errors"
"strings"

Expand Down Expand Up @@ -66,12 +67,13 @@ func Compile(url *C.char, inputs *C.char, quirks *C.char, resolve C.char, coerce

urlContext := exturl.NewContext()
defer urlContext.Release()
context := contextpkg.TODO()

if url_, err = exturl.NewValidURL(C.GoString(url), nil, urlContext); err != nil {
if url_, err = urlContext.NewValidURL(context, C.GoString(url), nil); err != nil {
return result(nil, nil, err)
}

if _, serviceTemplate, problems, err = parserContext.Parse(url_, nil, nil, quirks_, inputs_); err != nil {
if _, serviceTemplate, problems, err = parserContext.Parse(context, url_, nil, nil, quirks_, inputs_); err != nil {
return result(nil, problems, err)
}

Expand Down
9 changes: 6 additions & 3 deletions puccini-clout/commands/scriptlet-exec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package commands

import (
contextpkg "context"

"github.com/spf13/cobra"
"github.com/tliron/exturl"
"github.com/tliron/kutil/terminal"
Expand Down Expand Up @@ -32,19 +34,20 @@ var execCommand = &cobra.Command{

urlContext := exturl.NewContext()
defer urlContext.Release()
context := contextpkg.TODO()

clout, err := cloutpkg.Load(url, inputFormat, urlContext)
clout, err := cloutpkg.Load(context, url, inputFormat, urlContext)
util.FailOnError(err)

// Try loading JavaScript from Clout
scriptlet, err := js.GetScriptlet(scriptletName, clout)

if err != nil {
// Try loading JavaScript from path or URL
scriptletUrl, err := exturl.NewValidURL(scriptletName, nil, urlContext)
scriptletUrl, err := urlContext.NewValidURL(context, scriptletName, nil)
util.FailOnError(err)

scriptlet, err = exturl.ReadString(scriptletUrl)
scriptlet, err = exturl.ReadString(context, scriptletUrl)
util.FailOnError(err)

err = js.SetScriptlet(scriptletName, js.CleanupScriptlet(scriptlet), clout)
Expand Down
3 changes: 2 additions & 1 deletion puccini-clout/commands/scriptlet-get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
contextpkg "context"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,7 +34,7 @@ var getCommand = &cobra.Command{
urlContext := exturl.NewContext()
defer urlContext.Release()

clout, err := cloutpkg.Load(url, inputFormat, urlContext)
clout, err := cloutpkg.Load(contextpkg.TODO(), url, inputFormat, urlContext)
util.FailOnError(err)

scriptlet, err := js.GetScriptlet(scriptletName, clout)
Expand Down
3 changes: 2 additions & 1 deletion puccini-clout/commands/scriptlet-list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -30,7 +31,7 @@ var listCommand = &cobra.Command{
urlContext := exturl.NewContext()
defer urlContext.Release()

clout, err := cloutpkg.Load(url, inputFormat, urlContext)
clout, err := cloutpkg.Load(context.TODO(), url, inputFormat, urlContext)
util.FailOnError(err)

List(clout)
Expand Down
8 changes: 5 additions & 3 deletions puccini-clout/commands/scriptlet-put.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
contextpkg "context"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,14 +33,15 @@ var putCommand = &cobra.Command{

urlContext := exturl.NewContext()
defer urlContext.Release()
context := contextpkg.TODO()

clout, err := cloutpkg.Load(url, inputFormat, urlContext)
clout, err := cloutpkg.Load(context, url, inputFormat, urlContext)
util.FailOnError(err)

scriptletUrl_, err := exturl.NewValidURL(scriptletUrl, nil, urlContext)
scriptletUrl_, err := urlContext.NewValidURL(context, scriptletUrl, nil)
util.FailOnError(err)

scriptlet, err := exturl.ReadString(scriptletUrl_)
scriptlet, err := exturl.ReadString(context, scriptletUrl_)
util.FailOnError(err)

err = js.SetScriptlet(scriptletName, js.CleanupScriptlet(scriptlet), clout)
Expand Down
2 changes: 0 additions & 2 deletions puccini-csar/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ var createdBy string
var entryDefinitions string
var otherDefinitions []string

// TODO zip compression level

func init() {
rootCommand.AddCommand(createCommand)

Expand Down
6 changes: 4 additions & 2 deletions puccini-csar/commands/meta.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
contextpkg "context"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,13 +46,14 @@ func Meta(url string) {

urlContext := exturl.NewContext()
util.OnExitError(urlContext.Release)
context := contextpkg.TODO()

var csarUrl exturl.URL
csarUrl, err = exturl.NewValidURL(url, nil, urlContext)
csarUrl, err = urlContext.NewValidURL(context, url, nil)
util.FailOnError(err)

var meta *csar.Meta
meta, err = csar.ReadMetaFromURL(csarUrl, archiveFormat)
meta, err = csar.ReadMetaFromURL(context, csarUrl, archiveFormat)
util.FailOnError(err)

if !terminal.Quiet || (output != "") {
Expand Down
19 changes: 10 additions & 9 deletions puccini-tosca/commands/compile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
contextpkg "context"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,15 +47,15 @@ var compileCommand = &cobra.Command{
url = args[0]
}

Compile(url)
Compile(contextpkg.TODO(), url)
},
}

func Compile(url string) {
func Compile(context contextpkg.Context, url string) {
// Parse
context, serviceTemplate := Parse(url)
problems := context.GetProblems()
urlContext := context.Root.GetContext().URL.Context()
serviceContext, serviceTemplate := Parse(context, url)
problems := serviceContext.GetProblems()
urlContext := serviceContext.Root.GetContext().URL.Context()

// Compile
clout, err := serviceTemplate.Compile()
Expand All @@ -73,24 +74,24 @@ func Compile(url string) {
}

if exec != "" {
err = Exec(exec, arguments, clout, urlContext)
err = Exec(context, exec, arguments, clout, urlContext)
util.FailOnError(err)
} else if !terminal.Quiet || (output != "") {
err = transcribe.WriteOrPrint(clout, format, os.Stdout, strict, pretty, output)
util.FailOnError(err)
}
}

func Exec(scriptletName string, arguments map[string]string, clout *cloutpkg.Clout, urlContext *exturl.Context) error {
func Exec(context contextpkg.Context, scriptletName string, arguments map[string]string, clout *cloutpkg.Clout, urlContext *exturl.Context) error {
// Try loading JavaScript from Clout
scriptlet, err := js.GetScriptlet(scriptletName, clout)

if err != nil {
// Try loading JavaScript from path or URL
url, err := exturl.NewValidURL(scriptletName, nil, urlContext)
url, err := urlContext.NewValidURL(context, scriptletName, nil)
util.FailOnError(err)

scriptlet, err = exturl.ReadString(url)
scriptlet, err = exturl.ReadString(context, url)
util.FailOnError(err)

err = js.SetScriptlet(exec, js.CleanupScriptlet(scriptlet), clout)
Expand Down
Loading

0 comments on commit 149f12e

Please sign in to comment.