Skip to content

Commit

Permalink
Add basic run command
Browse files Browse the repository at this point in the history
- Introduced new run command that lets users start an instance of OPA.
- Added basic REPL as first mode that can be run, server mode coming soon.
- Extended Storage to support a JSON Patch like interface.
  • Loading branch information
tsandall committed May 2, 2016
1 parent fcc9a31 commit d1c3ca5
Show file tree
Hide file tree
Showing 422 changed files with 64,506 additions and 125,988 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.vscode
opa
opalog/parser.go
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
PACKAGES := \
github.com/open-policy-agent/opa/cmd/.../ \
github.com/open-policy-agent/opa/eval/.../ \
github.com/open-policy-agent/opa/opalog/.../
github.com/open-policy-agent/opa/opalog/.../ \
github.com/open-policy-agent/opa/runtime/.../

BUILD_COMMIT := $(shell ./build/get-build-commit.sh)
BUILD_TIMESTAMP := $(shell ./build/get-build-timestamp.sh)
Expand Down
65 changes: 65 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package cmd

import (
"os"
"path"

"github.com/open-policy-agent/opa/runtime"
"github.com/spf13/cobra"
)

// default filename for the interactive shell's history
var defaultHistoryFile = ".opa_history"

func init() {

params := &runtime.Params{}

runCommand := &cobra.Command{
Use: "run",
Short: "Start OPA in interative or server mode",
Long: `Start an instance of the Open Policy Agent (OPA).
The 'run' command starts an instance of the OPA runtime. The OPA
runtime can be started as an interactive shell or a server.
When the runtime is started as a shell, users can define rules and
evaluate expressions interactively. When the runtime is started as
a server, users can access OPA's APIs via HTTP.
The runtime can be initialized with one or more JSON files that
represent base documents.
`,
Run: func(cmd *cobra.Command, args []string) {
params.BaseDocPaths = args
rt := &runtime.Runtime{}
rt.Start(params)
},
}

runCommand.Flags().BoolVarP(&params.Server, "server", "s", false, "start the runtime in server mode")
runCommand.Flags().StringVarP(&params.HistoryPath, "history", "H", historyPath(), "set path of history file")

usageTemplate := `Usage:
{{.UseLine}} [flags] [files]
Flags:
{{.LocalFlags.FlagUsages | trimRightSpace}}
`

runCommand.SetUsageTemplate(usageTemplate)

RootCommand.AddCommand(runCommand)
}

func historyPath() string {
home := os.Getenv("HOME")
if len(home) == 0 {
return defaultHistoryFile
}
return path.Join(home, defaultHistoryFile)
}
Loading

0 comments on commit d1c3ca5

Please sign in to comment.