Skip to content

Commit

Permalink
Add args and url loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 2, 2024
1 parent 18da184 commit 76385e0
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 89 deletions.
1 change: 1 addition & 0 deletions examples/input.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "${input}"
18 changes: 18 additions & 0 deletions pkg/assemble/assemble.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package assemble

import (
"context"
"encoding/json"
"io"

"github.com/acorn-io/gptscript/pkg/types"
)

var Header = []byte("GPTSCRIPT\x00")

func Assemble(ctx context.Context, tool types.Tool, output io.Writer) error {
if _, err := output.Write(Header); err != nil {
return err
}
return json.NewEncoder(output).Encode(tool)
}
35 changes: 32 additions & 3 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package cli

import (
"fmt"
"io"
"os"
"strings"

"github.com/acorn-io/cmd"
"github.com/acorn-io/gptscript/pkg/assemble"
"github.com/acorn-io/gptscript/pkg/input"
"github.com/acorn-io/gptscript/pkg/loader"
"github.com/acorn-io/gptscript/pkg/runner"
"github.com/acorn-io/gptscript/pkg/version"
Expand All @@ -15,8 +18,10 @@ import (

type GPTScript struct {
runner.Options
Output string `usage:"Save output to a file" short:"o"`
SubTool string `usage:"Target tool name in file to run"`
Output string `usage:"Save output to a file" short:"o"`
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
SubTool string `usage:"Target tool name in file to run"`
Assemble bool `usage:"Assemble tool to a single artifact and saved to --output"`
}

func New() *cobra.Command {
Expand All @@ -26,6 +31,7 @@ func New() *cobra.Command {
func (r *GPTScript) Customize(cmd *cobra.Command) {
cmd.Use = version.ProgramName
cmd.Args = cobra.MinimumNArgs(1)
cmd.Flags().SetInterspersed(false)
}

func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -34,6 +40,20 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
return err
}

if r.Assemble {
var out io.Writer = os.Stdout
if r.Output != "" {
f, err := os.Create(r.Output)
if err != nil {
return fmt.Errorf("opening %s: %w", r.Output, err)
}
defer f.Close()
out = f
}

return assemble.Assemble(cmd.Context(), *tool, out)
}

if !r.Quiet {
if !term.IsTerminal(int(os.Stdout.Fd())) {
r.Quiet = true
Expand All @@ -45,7 +65,16 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
return err
}

s, err := runner.Run(cmd.Context(), *tool, "")
toolInput, err := input.FromFile(r.Input)
if err != nil {
return err
}

if toolInput == "" {
toolInput = input.FromArgs(args[1:])
}

s, err := runner.Run(cmd.Context(), *tool, toolInput)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/input/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package input

import (
"fmt"
"io"
"os"
"strings"
)

func FromArgs(args []string) string {
return strings.Join(args, " ")
}

func FromFile(file string) (string, error) {
if file == "-" {
log.Debugf("reading stdin")
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("reading stdin: %w", err)
}
return string(data), nil
} else if file != "" {
log.Debugf("reading file %s", file)
data, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("reading %s: %w", file, err)
}
return string(data), nil
}

return "", nil
}
5 changes: 5 additions & 0 deletions pkg/input/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package input

import "github.com/acorn-io/gptscript/pkg/mvl"

var log = mvl.Package()
Loading

0 comments on commit 76385e0

Please sign in to comment.