Skip to content

Commit

Permalink
Move web & shell into new "modes" dir & add "defaultmode" for basic C…
Browse files Browse the repository at this point in the history
…LI (moved from main) (#186)
  • Loading branch information
asmaloney authored Jun 10, 2022
1 parent b8227c8 commit 0fb2dfe
Show file tree
Hide file tree
Showing 39 changed files with 175 additions and 100 deletions.
107 changes: 7 additions & 100 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (

"github.com/urfave/cli/v2"

"github.com/asmaloney/gactar/actr"
"github.com/asmaloney/gactar/amod"
"github.com/asmaloney/gactar/framework"
"github.com/asmaloney/gactar/shell"
"github.com/asmaloney/gactar/web"
"github.com/asmaloney/gactar/modes/defaultmode"
"github.com/asmaloney/gactar/modes/shell"
"github.com/asmaloney/gactar/modes/web"

"github.com/asmaloney/gactar/util/clicontext"
"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/filesystem"
"github.com/asmaloney/gactar/util/frameworkutil"
"github.com/asmaloney/gactar/util/validate"
"github.com/asmaloney/gactar/util/version"
)

Expand Down Expand Up @@ -212,107 +211,15 @@ func handleInteractive(ctx *cli.Context, frameworks framework.List) (err error)
}

func handleDefault(ctx *cli.Context, frameworks framework.List) (err error) {
cli.ShowVersion(ctx)

// Check if files exist first
files := ctx.Args().Slice()

if len(files) == 0 {
err = fmt.Errorf("error: no input files specified on command line")
return
}

existingFiles := files[:0]
for _, file := range files {
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
fmt.Printf("error: file does not exist - %q\n", file)
continue
}

existingFiles = append(existingFiles, file)
}

if len(existingFiles) == 0 {
err = fmt.Errorf("error: no files to process")
return
}

tempPath := ctx.Path("temp")
fmt.Printf("Intermediate file path: %q\n", tempPath)

err = generateCode(frameworks, existingFiles, tempPath, ctx.Bool("run"))
s, err := defaultmode.Initialize(ctx, frameworks)
if err != nil {
return err
}

if ctx.Bool("run") {
runCode(frameworks)
}
return
}

func generateCode(frameworks framework.List, files []string, outputDir string, runCode bool) (err error) {
modelMap := map[string]*actr.Model{}

for _, file := range files {
fmt.Printf("Generating model for %s\n", file)
model, log, err := amod.GenerateModelFromFile(file)
if err != nil {
fmt.Print(log)
continue
}

// When using "-r" the goal must be initialized in the code.
validate.Goal(model, "", log)

fmt.Print(log)

modelMap[file] = model
}

if len(modelMap) == 0 {
err = errors.New("no valid models to run")
return
}

for _, f := range frameworks {
for file, model := range modelMap {
fmt.Printf("\t- generating code for %s\n", file)

log := f.ValidateModel(model)
fmt.Print(log)
if log.HasError() {
continue
}

err = f.SetModel(model)
if err != nil {
fmt.Println(err.Error())
continue
}

fileName, err := f.WriteModel(outputDir, framework.InitialBuffers{})
if err != nil {
fmt.Println(err.Error())
continue
}
fmt.Printf("\t- written to %s\n", fileName)
}
err = s.Start()
if err != nil {
return err
}

return
}

func runCode(frameworks framework.List) {
for _, f := range frameworks {
result, err := f.Run(framework.InitialBuffers{})
if err != nil {
fmt.Println(err.Error())
continue
}

fmt.Printf("== %s ==\n", f.Info().Name)
fmt.Println(string(result.Output))
fmt.Println()
}
}
29 changes: 29 additions & 0 deletions modes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Modes

gactar can be used in several different modes. This directory contains the code to handle each of them.

More details may be found in the main [README file](../README.md).

## Default Mode (CLI)

If you use the command line without the `-i` or `-w` options, gactar lets the user process a file (and optionally run it with `-r`).

```sh
$ ./gactar {amod file}
```

## Shell (Interactive CLI)

This allow the user to use commands to work with gactar interactively - loading & running amod files.

```sh
$ ./gactar -i
```

## Web

This runs a web server with an HTTP interface to load & run amod models. Details of the endpoints may be found in the [doc directory](../doc/Web%20API.md).

```sh
$ ./gactar -w
```
139 changes: 139 additions & 0 deletions modes/defaultmode/defaultmode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package defaultmode

import (
"errors"
"fmt"
"os"

"github.com/asmaloney/gactar/actr"
"github.com/asmaloney/gactar/amod"
"github.com/asmaloney/gactar/framework"
"github.com/asmaloney/gactar/util/validate"
"github.com/urfave/cli/v2"
)

type DefaultMode struct {
context *cli.Context
actrFrameworks framework.List

fileList []string

tempPath string
}

func Initialize(ctx *cli.Context, frameworks framework.List) (d *DefaultMode, err error) {
d = &DefaultMode{
context: ctx,
actrFrameworks: frameworks,

tempPath: ctx.Path("temp"),
}

cli.ShowVersion(ctx)

// Check if files exist first
files := ctx.Args().Slice()

if len(files) == 0 {
err = fmt.Errorf("error: no input files specified on command line")
return
}

existingFiles := files[:0]
for _, file := range files {
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
fmt.Printf("error: file does not exist - %q\n", file)
continue
}

existingFiles = append(existingFiles, file)
}

if len(existingFiles) == 0 {
err = fmt.Errorf("error: no files to process")
return
}

d.fileList = existingFiles
return
}

func (d *DefaultMode) Start() (err error) {
fmt.Printf("Intermediate file path: %q\n", d.tempPath)

err = generateCode(d.actrFrameworks, d.fileList, d.tempPath, d.context.Bool("run"))
if err != nil {
return err
}

if d.context.Bool("run") {
runCode(d.actrFrameworks)
}
return
}

func generateCode(frameworks framework.List, files []string, outputDir string, runCode bool) (err error) {
modelMap := map[string]*actr.Model{}

for _, file := range files {
fmt.Printf("Generating model for %s\n", file)
model, log, err := amod.GenerateModelFromFile(file)
if err != nil {
fmt.Print(log)
continue
}

// When using "-r" the goal must be initialized in the code.
validate.Goal(model, "", log)

fmt.Print(log)

modelMap[file] = model
}

if len(modelMap) == 0 {
err = errors.New("no valid models to run")
return
}

for _, f := range frameworks {
for file, model := range modelMap {
fmt.Printf("\t- generating code for %s\n", file)

log := f.ValidateModel(model)
fmt.Print(log)
if log.HasError() {
continue
}

err = f.SetModel(model)
if err != nil {
fmt.Println(err.Error())
continue
}

fileName, err := f.WriteModel(outputDir, framework.InitialBuffers{})
if err != nil {
fmt.Println(err.Error())
continue
}
fmt.Printf("\t- written to %s\n", fileName)
}
}

return
}

func runCode(frameworks framework.List) {
for _, f := range frameworks {
result, err := f.Run(framework.InitialBuffers{})
if err != nil {
fmt.Println(err.Error())
continue
}

fmt.Printf("== %s ==\n", f.Info().Name)
fmt.Println(string(result.Output))
fmt.Println()
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0fb2dfe

Please sign in to comment.