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

Replaces .env with config.toml #2

Merged
merged 13 commits into from
Apr 14, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
go.work
*.code-workspace
*.toml
2 changes: 1 addition & 1 deletion CLI/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ BisonExmple.txt
delme.sh
*.txt
.history
.env
*.toml
*.ocli
*.toml
13 changes: 4 additions & 9 deletions CLI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ For Linux, consult your respective Distribution docs

Clone the CLI repository
Execute make. It should automatically retrieve the necessary libraries. If not then execute the commands below
```
go get github.com/blynn/nex
go get -u github.com/cznic/goyacc
```

make

Expand All @@ -38,15 +34,14 @@ For Linux, consult your respective Distribution docs

Running
-------------
You must first have a ```.env``` file to be included in the same directory as the executable. If not included the CLI will exit on startup with an error message about this file.
You must first have a ```config.toml``` file to be included in the parent directory of the executable. If not included the CLI will exit on startup with an error message about this file.
See https://github.com/ditrit/OGrEE-Core/blob/main/README.md for an example.

You can view an example ```.env``` file here: https://ogree.ditrit.io/htmls/clienv.html

- Execute ```./main```
- Execute ```./cli``` (or cli.exe on windows)

If this is the first running the Shell, you will be greeted with a sign up prompt to input a user email and password.

DO NOT SHARE YOUR ```.env``` file since it contains your credentials
DO NOT SHARE YOUR ```config.toml``` file since it contains your credentials

Usage & Notes
-------------
Expand Down
18 changes: 18 additions & 0 deletions CLI/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"cli/config"
cmd "cli/controllers"
"encoding/json"
"fmt"
Expand All @@ -20,6 +21,23 @@ func GetDynamicSymbolTable() map[string]interface{} {
return dynamicSymbolTable
}

func InitVars(variables []config.Vardef) error {
for _, v := range variables {
varNode, _, err := parseRawText(lexQuotedString, newFrame(v.Value))
if err != nil {
return err
}
n := &assignNode{
variable: v.Name,
val: varNode,
}
if _, err := n.execute(); err != nil {
return err
}
}
return nil
}

type node interface {
execute() (interface{}, error)
}
Expand Down
106 changes: 106 additions & 0 deletions CLI/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/BurntSushi/toml"
flag "github.com/spf13/pflag"
)

type globalConfig struct {
Conf Config `toml:"OGrEE-CLI"`
}

type Vardef struct {
Name string
Value string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Value should be an interface{} type, if we want to pass numerical variables etc

}

type Config struct {
Verbose string
APIURL string
UnityURL string
UnityTimeout string
ConfigPath string
HistPath string
Script string
Drawable []string
DrawableJson map[string]string
DrawLimit int
Updates []string
User string
APIKEY string
Variables []Vardef
}

// Used for parsing (via JSON) into conf after parsing TOML
// since an object can only be decoded by TOML once
type ArgStruct struct {
ConfigPath string `json:",omitempty"`
Verbose string `json:",omitempty"`
UnityURL string `json:",omitempty"`
APIURL string `json:",omitempty"`
APIKEY string `json:",omitempty"`
HistPath string `json:",omitempty"`
Script string `json:",omitempty"`
}

func defaultConfig() Config {
return Config{
Verbose: "ERROR",
APIURL: "",
UnityURL: "",
UnityTimeout: "10ms",
ConfigPath: "../config.toml",
HistPath: "./.history",
Script: "",
Drawable: []string{"all"},
DrawableJson: map[string]string{},
DrawLimit: 50,
Updates: []string{"all"},
User: "",
APIKEY: "",
Variables: []Vardef{},
}
}

func ReadConfig() *Config {
globalConf := globalConfig{
Conf: defaultConfig(),
}
args := ArgStruct{}
conf := &globalConf.Conf

flag.StringVarP(&args.ConfigPath, "conf_path", "c", conf.ConfigPath,
"Indicate the location of the Shell's config file")
flag.StringVarP(&args.Verbose, "verbose", "v", conf.Verbose,
"Indicates level of debugging messages."+
"The levels are of in ascending order:"+
"{NONE,ERROR,WARNING,INFO,DEBUG}.")
flag.StringVarP(&args.UnityURL, "unity_url", "u", conf.UnityURL, "Unity URL")
flag.StringVarP(&args.APIURL, "api_url", "a", conf.APIURL, "API URL")
flag.StringVarP(&args.APIKEY, "api_key", "k", conf.APIKEY, "Indicate the key of the API")
flag.StringVarP(&args.HistPath, "history_path", "h", conf.HistPath,
"Indicate the location of the Shell's history file")
flag.StringVarP(&args.Script, "file", "f", conf.Script, "Launch the shell as an interpreter "+
" by only executing an OCLI script file")
flag.Parse()

configBytes, err := os.ReadFile(args.ConfigPath)
if err != nil {
fmt.Println("Cannot read config file", conf.ConfigPath, ":", err.Error())
fmt.Println("Please ensure that you have a properly formatted config file saved as 'config.toml' in the parent directory")
fmt.Println("For more details please refer to: https://github.com/ditrit/OGrEE-Core/blob/main/README.md")
}
_, err = toml.Decode(string(configBytes), &globalConf.Conf)
if err != nil {
println("Error reading config :", err.Error())
}

argBytes, _ := json.Marshal(args)
json.Unmarshal(argBytes, &conf)

return conf
}
6 changes: 3 additions & 3 deletions CLI/controllers/commandController.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,14 +827,14 @@ func LSOG() {
fmt.Println("OGREE Shell Information")
fmt.Println("********************************************")

fmt.Println("USER EMAIL:", GetEmail())
fmt.Println("USER EMAIL:", State.UserEmail)
fmt.Println("API URL:", State.APIURL+"/api/")
fmt.Println("UNITY URL:", State.UnityClientURL)
fmt.Println("BUILD DATE:", BuildTime)
fmt.Println("BUILD TREE:", BuildTree)
fmt.Println("BUILD HASH:", BuildHash)
fmt.Println("COMMIT DATE: ", GitCommitDate)
fmt.Println("ENV FILE PATH: ", State.EnvFilePath)
fmt.Println("CONFIG FILE PATH: ", State.ConfigPath)
fmt.Println("LOG PATH:", "./log.txt")
fmt.Println("HISTORY FILE PATH:", State.HistoryFilePath)
fmt.Println("DEBUG LEVEL: ", State.DebugLvl)
Expand Down Expand Up @@ -881,7 +881,7 @@ func Env(userVars, userFuncs map[string]interface{}) {
fmt.Println()
fmt.Println("Objects Unity shall be informed of upon update:")
for _, k := range State.ObjsForUnity {
fmt.Println(EntityToString(k))
fmt.Println(k)
}
fmt.Println()
fmt.Println("Objects Unity shall draw:")
Expand Down
Loading