-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7fce2cb
replaces .env with config.toml
GeremWD f87b31c
tidy go.mod and removes go.work.sum
GeremWD fec9487
removes analyser option again
GeremWD 351bed5
fix old script loading
GeremWD 8deb3a9
fixes bug APIKEY
GeremWD a463f5b
shared config.toml with OGrEE-3D
GeremWD ec88aec
fixes lsog email bug
GeremWD ec4446f
removes ConfigPath from example of config.toml
GeremWD 2d9888e
adds variables to config.toml
GeremWD 83f1898
improves readme variables
GeremWD c7f9665
Refactors init section into main.go to keep repl.go more focused. Thi…
5fe00f7
conf parsed into wrong struct + var string -> any
GeremWD f3d4b2b
Merge branch 'main' into config-toml
GeremWD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.vscode/ | ||
go.work | ||
*.code-workspace | ||
*.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,6 @@ BisonExmple.txt | |
delme.sh | ||
*.txt | ||
.history | ||
.env | ||
*.toml | ||
*.ocli | ||
*.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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