Initial commit
This commit is contained in:
72
cmd/step/main.go
Normal file
72
cmd/step/main.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Command step evaluates STEP files and prints the result to standard out.
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "io"
|
||||
import "fmt"
|
||||
import "context"
|
||||
import "git.tebibyte.media/sashakoshka/step"
|
||||
import "git.tebibyte.media/sashakoshka/go-cli"
|
||||
import "git.tebibyte.media/sashakoshka/step/providers"
|
||||
|
||||
func main () {
|
||||
// TODO: add some kind of way to play an http request to a STEP file,
|
||||
// and have it be executed as if the request actually happened.
|
||||
// TODO: initialize plugins on startup and tear them down on finish
|
||||
|
||||
// parse command line arguments
|
||||
flagMetadata := cli.NewFlag (
|
||||
'm', "metadata",
|
||||
"Print document metadata as front matter")
|
||||
cmd := cli.New (
|
||||
"Evaluate STEP files and print the result",
|
||||
flagMetadata,
|
||||
cli.NewHelp())
|
||||
cmd.Syntax = "[OPTIONS]... FILE"
|
||||
cmd.ParseOrExit(os.Args)
|
||||
if len(cmd.Args) > 1 {
|
||||
cmd.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// set up the environment
|
||||
environment := step.Environment { }
|
||||
environment.FuncProviders = providers.All()
|
||||
err := environment.Init(context.Background())
|
||||
handleErr(cmd, err)
|
||||
|
||||
// load and execute the document
|
||||
var document *step.Document
|
||||
if len(cmd.Args) > 0 {
|
||||
document, err = environment.Parse(cmd.Args[0])
|
||||
} else {
|
||||
document, err = environment.ParseReader(".", os.Stdin)
|
||||
}
|
||||
handleErr(cmd, err)
|
||||
if flagMetadata.Value == "true" {
|
||||
writeMetadata(os.Stdout, document)
|
||||
}
|
||||
err = document.Execute(os.Stdout, step.ExecutionData { })
|
||||
handleErr(cmd, err)
|
||||
}
|
||||
|
||||
func handleErr (cmd *cli.Cli, err error) {
|
||||
if err != nil {
|
||||
cmd.Errorln(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func writeMetadata (output io.Writer, document *step.Document) {
|
||||
fmt.Fprintln(output, "---")
|
||||
if document.Author != "" {
|
||||
fmt.Fprintf(output, "Author: %s\n", document.Author)
|
||||
}
|
||||
if document.Title != "" {
|
||||
fmt.Fprintf(output, "Title: %s\n", document.Title)
|
||||
}
|
||||
if document.Extends != "" {
|
||||
fmt.Fprintf(output, "Extends: %s\n", document.Extends)
|
||||
}
|
||||
fmt.Fprintln(output, "---")
|
||||
}
|
||||
Reference in New Issue
Block a user