Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cli/connect/internal/luabody/eval.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package luabody

import (
"bytes"
_ "embed"
"fmt"
"os"
"strings"

"github.com/tarantool/cartridge-cli/cli/templates"
"text/template"
)

//go:embed eval_func_body.lua
Expand All @@ -15,6 +15,23 @@ var evalFuncBody string
//go:embed get_suggestions_func_body.lua
var getSuggestionsFuncBody string

// GetTemplatedStr returns a templated string.
func GetTemplatedStr(text string, obj interface{}) (string, error) {
tmpl, err := template.New("s").Parse(text)
if err != nil {
return "", err
}

buf := new(bytes.Buffer)

err = tmpl.Execute(buf, obj)
if err != nil {
return "", err
}

return buf.String(), nil
}

// GetEvalFuncBody returns lua code of eval func.
func GetEvalFuncBody(evaler string) (string, error) {
mapping := map[string]string{}
Expand All @@ -30,10 +47,10 @@ func GetEvalFuncBody(evaler string) (string, error) {
}
}

return templates.GetTemplatedStr(&evalFuncBody, mapping)
return GetTemplatedStr(evalFuncBody, mapping)
}

// GetEvalFuncBody returns lua code for completions.
// getSuggestionsFuncBody returns lua code for completions.
func GetSuggestionsFuncBody() string {
return getSuggestionsFuncBody
}
4 changes: 2 additions & 2 deletions cli/connect/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tarantool/cartridge-cli/cli/templates"
. "github.com/tarantool/tt/cli/connect"
"github.com/tarantool/tt/cli/connect/internal/luabody"
"github.com/tarantool/tt/cli/connector"
)

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestChangeLanguage_requestInputs(t *testing.T) {
rawFun, err := os.ReadFile("./internal/luabody/eval_func_body.lua")
require.NoError(t, err, "Failed to read lua file")
rawFunStr := string(rawFun)
expectedFun, err := templates.GetTemplatedStr(&rawFunStr, map[string]string{})
expectedFun, err := luabody.GetTemplatedStr(rawFunStr, map[string]string{})
require.NoError(t, err, "Failed to render eval func body template")

expectedOpts := connector.RequestOpts{}
Expand Down
Loading