Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jm/ex - Extension support added (local). #47

Merged
merged 33 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
dfcd110
First version for extension new/generate
jimmyaxod Sep 11, 2023
0325c42
Added extension list cmd
jimmyaxod Sep 13, 2023
bb23b76
Updated function new
jimmyaxod Sep 14, 2023
ec7ddac
First ver golang compile guest
jimmyaxod Sep 14, 2023
811c740
Removed some debug out
jimmyaxod Sep 14, 2023
c0a5391
Added simple test for ext
jimmyaxod Sep 15, 2023
9aa5001
Added missing parts for ext test
jimmyaxod Sep 15, 2023
6e7191e
Ext test now updates runner import locations
jimmyaxod Sep 15, 2023
9da7b26
Added 2nd run to example
jimmyaxod Sep 18, 2023
bd01013
Few changes to fix build
jimmyaxod Sep 18, 2023
31c0da5
Updated to import scale-extension-interfaces
jimmyaxod Sep 20, 2023
1bef57f
Fix go.mod for runner ext
jimmyaxod Sep 20, 2023
897ece1
Added missing scale-extension-interfaces
jimmyaxod Sep 21, 2023
3b591e7
Updated go.mod in runner
jimmyaxod Sep 21, 2023
e1d4638
Ext test now tests rust/typescript functions.
jimmyaxod Sep 21, 2023
54f4c5e
Rust new function now includes extensions in cargo
jimmyaxod Sep 22, 2023
1127ecb
Adjusted versions etc
jimmyaxod Sep 25, 2023
a3a89ad
Update to get rust compiling
jimmyaxod Sep 25, 2023
d877c10
Updated rust function code to test HttpFetch
jimmyaxod Sep 25, 2023
16483d6
Latest updates
jimmyaxod Sep 28, 2023
64a4829
Latest fn_code.rs
jimmyaxod Sep 28, 2023
39388f7
Latest runner code
jimmyaxod Sep 29, 2023
b5b7850
Fix unused var
jimmyaxod Sep 29, 2023
b26cdd7
Updated go fn and runner to handle error cases
jimmyaxod Sep 29, 2023
f609058
Updated test rs fn code a little
jimmyaxod Sep 29, 2023
361fc02
Fixed up rust ext code for new borrow stuff
jimmyaxod Oct 6, 2023
f6c5342
e2e work again for go host, go guest and rust guest
jimmyaxod Nov 22, 2023
8daabe8
Added changes for ts guest
jimmyaxod Dec 19, 2023
07df0da
Removed wip int tests
jimmyaxod Dec 19, 2023
4a53892
Merge branch 'staging' into jm/ext
dphilla Mar 8, 2024
ea94586
rm name/tag from template
dphilla Mar 8, 2024
9de5680
bump to latest release version
dphilla Mar 8, 2024
4b8dbaa
dependencies
dphilla Mar 8, 2024
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
1 change: 1 addition & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Shivansh Vij <shivanshvij@loopholelabs.io> @shivanshvij
- Dan Phillips <dan@loopholelabs.io> @dphilla
- Jimmy Moore <jamesmoore@loopholelabs.io> @jimmyaxod
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/loopholelabs/cmdutils/pkg/command"
"github.com/loopholelabs/scale-cli/cmd/access"
"github.com/loopholelabs/scale-cli/cmd/auth"
"github.com/loopholelabs/scale-cli/cmd/extension"
"github.com/loopholelabs/scale-cli/cmd/function"
"github.com/loopholelabs/scale-cli/cmd/registry"
"github.com/loopholelabs/scale-cli/cmd/signature"
Expand All @@ -35,5 +36,5 @@ var Cmd = command.New[*config.Config](
true,
version.V,
config.New,
[]command.SetupCommand[*config.Config]{access.Cmd(), auth.Cmd(), registry.Cmd(), function.Cmd(), signature.Cmd(), update.Cmd()},
[]command.SetupCommand[*config.Config]{access.Cmd(), auth.Cmd(), registry.Cmd(), function.Cmd(), signature.Cmd(), extension.Cmd(), update.Cmd()},
)
54 changes: 54 additions & 0 deletions cmd/extension/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2023 Loophole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extension

import (
"github.com/loopholelabs/cmdutils"
"github.com/loopholelabs/cmdutils/pkg/command"
"github.com/loopholelabs/scale-cli/internal/config"
"github.com/spf13/cobra"
)

type extensionModel struct {
Name string `header:"name" json:"name"`
Tag string `header:"tag" json:"tag"`
Org string `header:"org" json:"org"`
Hash string `header:"hash" json:"hash"`
Version string `header:"version" json:"version"`
}

// Cmd encapsulates the commands for extensions.
func Cmd() command.SetupCommand[*config.Config] {
return func(cmd *cobra.Command, ch *cmdutils.Helper[*config.Config]) {
extensionCmd := &cobra.Command{
Use: "extension <command>",
Aliases: []string{"ext"},
Short: "Create, list, and manage Scale Extensions",
}

newSetup := NewCmd(false)
newSetup(extensionCmd, ch)

generateSetup := GenerateCmd(false)
generateSetup(extensionCmd, ch)

listSetup := ListCmd(false)
listSetup(extensionCmd, ch)

cmd.AddCommand(extensionCmd)
}
}
130 changes: 130 additions & 0 deletions cmd/extension/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright 2023 Loophole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extension

import (
"fmt"
"os"
"path"
"strings"

"github.com/loopholelabs/cmdutils"
"github.com/loopholelabs/cmdutils/pkg/command"
"github.com/loopholelabs/cmdutils/pkg/printer"
"github.com/loopholelabs/scale-cli/internal/config"
"github.com/loopholelabs/scale-cli/utils"
"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/scalefunc"
"github.com/loopholelabs/scale/storage"
"github.com/spf13/cobra"
)

// GenerateCmd encapsulates the commands for generating an Extension from an Extension File
func GenerateCmd(hidden bool) command.SetupCommand[*config.Config] {
var directory string

return func(cmd *cobra.Command, ch *cmdutils.Helper[*config.Config]) {
generateCmd := &cobra.Command{
Use: "generate <name>:<tag> [flags]",
Args: cobra.ExactArgs(1),
Short: "generate a scale extension from an extension file",
Hidden: hidden,
PreRunE: utils.PreRunUpdateCheck(ch),
PostRunE: utils.PostRunAnalytics(ch),
RunE: func(cmd *cobra.Command, args []string) error {
sourceDir := directory
if !path.IsAbs(sourceDir) {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
sourceDir = path.Join(wd, sourceDir)
}

extensionPath := path.Join(sourceDir, "scale.extension")
extensionFile, err := extension.ReadSchema(extensionPath)
if err != nil {
return fmt.Errorf("failed to read extension file at %s: %w", extensionPath, err)
}

nametag := strings.Split(args[0], ":")
if len(nametag) != 2 {
return fmt.Errorf("invalid name or tag %s", args[0])
}
name := nametag[0]
tag := nametag[1]

if name == "" || !scalefunc.ValidString(name) {
return utils.InvalidStringError("name", name)
}

if tag == "" || !scalefunc.ValidString(tag) {
return utils.InvalidStringError("tag", tag)
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Generating scale extension local/%s:%s...", name, tag))

st := storage.DefaultExtension
if ch.Config.StorageDirectory != "" {
st, err = storage.NewExtension(ch.Config.StorageDirectory)
if err != nil {
end()
return fmt.Errorf("failed to instantiate function storage for %s: %w", ch.Config.StorageDirectory, err)
}
}

oldEntry, err := st.Get(name, tag, "local", "")
if err != nil {
end()
return fmt.Errorf("failed to check if scale extension already exists: %w", err)
}

if oldEntry != nil {
err = st.Delete(name, tag, oldEntry.Organization, oldEntry.Hash)
if err != nil {
end()
return fmt.Errorf("failed to delete existing scale extension %s:%s: %w", name, tag, err)
}
}

err = st.Put(name, tag, "local", extensionFile)
if err != nil {
end()
return fmt.Errorf("failed to store scale extension: %w", err)
}

end()

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Successfully generated scale extension %s\n", printer.BoldGreen(fmt.Sprintf("local/%s:%s", name, tag)))
return nil
}

return ch.Printer.PrintResource(map[string]string{
"name": name,
"tag": tag,
"org": "local",
"directory": directory,
})
},
}

generateCmd.Flags().StringVarP(&directory, "directory", "d", ".", "the directory containing the extension file")

cmd.AddCommand(generateCmd)
}
}
80 changes: 80 additions & 0 deletions cmd/extension/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2023 Loophole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extension

import (
"fmt"

"github.com/loopholelabs/cmdutils"
"github.com/loopholelabs/cmdutils/pkg/command"
"github.com/loopholelabs/cmdutils/pkg/printer"
"github.com/loopholelabs/scale-cli/analytics"
"github.com/loopholelabs/scale-cli/internal/config"
"github.com/loopholelabs/scale-cli/utils"
"github.com/loopholelabs/scale/storage"
"github.com/spf13/cobra"
)

// ListCmd encapsulates the commands for listing the available Extensions
func ListCmd(hidden bool) command.SetupCommand[*config.Config] {
return func(cmd *cobra.Command, ch *cmdutils.Helper[*config.Config]) {
listCmd := &cobra.Command{
Use: "list",
Short: "list locally available scale extensions",
Args: cobra.NoArgs,
PreRunE: utils.PreRunUpdateCheck(ch),
PostRunE: utils.PostRunAnalytics(ch),
RunE: func(cmd *cobra.Command, args []string) error {
st := storage.DefaultExtension
if ch.Config.StorageDirectory != "" {
var err error
st, err = storage.NewExtension(ch.Config.StorageDirectory)
if err != nil {
return fmt.Errorf("failed to instantiate extension storage for %s: %w", ch.Config.StorageDirectory, err)
}
}

analytics.Event("list-extension")

extensionEntries, err := st.List()
if err != nil {
return fmt.Errorf("failed to list scale extensions: %w", err)
}

if len(extensionEntries) == 0 && ch.Printer.Format() == printer.Human {
ch.Printer.Println("No Scale Extensions available yet.")
return nil
}

exts := make([]extensionModel, len(extensionEntries))
for i, entry := range extensionEntries {
exts[i] = extensionModel{
Name: entry.Name,
Tag: entry.Tag,
Org: entry.Organization,
Hash: entry.Hash,
Version: entry.Schema.Version,
}
}

return ch.Printer.PrintResource(exts)
},
}

cmd.AddCommand(listCmd)
}
}
75 changes: 75 additions & 0 deletions cmd/extension/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023 Loophole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extension

import (
"fmt"
"os"
"path"

"github.com/loopholelabs/cmdutils"
"github.com/loopholelabs/cmdutils/pkg/command"
"github.com/loopholelabs/cmdutils/pkg/printer"
"github.com/loopholelabs/scale-cli/analytics"
"github.com/loopholelabs/scale-cli/internal/config"
"github.com/loopholelabs/scale-cli/template"
"github.com/loopholelabs/scale-cli/utils"
"github.com/spf13/cobra"
)

// NewCmd encapsulates the commands for creating new Extensions
func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
var directory string
return func(cmd *cobra.Command, ch *cmdutils.Helper[*config.Config]) {
newCmd := &cobra.Command{
Use: "new [flags]",
Args: cobra.ExactArgs(0),
Short: "create a new scale extension",
Hidden: hidden,
PreRunE: utils.PreRunUpdateCheck(ch),
PostRunE: utils.PostRunAnalytics(ch),
RunE: func(cmd *cobra.Command, args []string) error {
analytics.Event("new-extension")
sourceDir := directory
if !path.IsAbs(sourceDir) {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
sourceDir = path.Join(wd, sourceDir)
}
err := os.WriteFile(path.Join(sourceDir, fmt.Sprintf("scale.extension")), []byte(template.ExtensionFile), 0644)
if err != nil {
return fmt.Errorf("error writing extension: %w", err)
}

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Successfully created new scale extension\n")
return nil
}

return ch.Printer.PrintResource(map[string]string{
"path": path.Join(directory, fmt.Sprintf("scale.extension")),
})
},
}

newCmd.Flags().StringVarP(&directory, "directory", "d", ".", "the directory to create the new scale extension in")

cmd.AddCommand(newCmd)
}
}
Loading
Loading