diff --git a/addrbook.rex b/addrbook.rex index 16e7ce0..73505d8 100755 --- a/addrbook.rex +++ b/addrbook.rex @@ -10,7 +10,11 @@ **************************************************************************/ signal on HALT name ProgramHalt .environment['STOPNOW'] = 0 -call setEnv +rc = setEnv("./config.json") +if rc \= 0 then do + say "Configuration failed." + exit rc +end app = .AddressBookApp~new() app~run() @@ -51,20 +55,59 @@ Exit db~closeDb() return -::ROUTINE setEnv - .environment~home = SysGetpwnam("gmgauthier", "d") - .environment~projectRoot = .home||"/Projects/rexx-address-book" - .environment~pkgPath = .projectRoot||"/app" - .environment~dbPath = .projectRoot||"/db" - .environment~dbFile = .dbPath||"/contacts.sqlite" - .environment["REXX_PATH"] = .projectRoot||";"||.pkgPath||";"||.dbPath||";" +/******** HELPER FUNCTIONS ****************/ +/* Configuration Retrieval */ +::ROUTINE setConfig + use arg filespec + + file = .Stream~new(filespec) + json_string = "" + do file~lines + json_string = json_string||file~lineIn + end + json_config = .json~new()~fromJSON(json_string) + Do index over json_config~allIndexes + val = json_config[index] + INTERPRET '.environment~'index' = "'val'"' + END +return 0 + +/** SET ENV **/ +::ROUTINE setEnv + use arg configFilePath + + rc = 0 + .environment~home = SysGetpwuid(SysGetuid(), "d") + if configFilePath = .nil | configFilePath = '' then do + configFilePath = .environment~home||"/.config/rexx-address-book/config.json" /* assume its in the same location */ + Say "No config specified. Default will be used at : "configFilePath + END + + if SysFileExists(configFilePath) then do + call setConfig(configFilePath) + /* set up the database config */ + IF .environment~dbFile = .nil then do + .environment~dbFile = .environment~home||"/.local/share/rexx-address-book/rexx-address-book/contacts.db" + say "Config has no detabase specified. A default will be created at: ".environment~dbFile + END + IF \SysFileExists(.environment~dbFile) then do + say "Specified database not found. A new database will be created." + END + END + else do + say "No config file Not Found at: "configFilePath + rc = -1 + END + +return rc /** External Libraries **/ -::requires 'app/appdb.cls' -::requires 'app/appui.cls' -::requires 'app/utils.rex' +::requires 'appdb.cls' +::requires 'appui.cls' +::requires 'utils.rex' ::requires 'ooSQLite.cls' ::requires "rxunixsys" LIBRARY ::requires 'ncurses.cls' +::requires 'json.cls' \ No newline at end of file diff --git a/app/utils.rex b/app/utils.rex index 3a86a17..411d6c7 100644 --- a/app/utils.rex +++ b/app/utils.rex @@ -1,11 +1,12 @@ ::ROUTINE SysWait PUBLIC - use arg seconds - address system 'sleep' seconds - return + use arg seconds + address system 'sleep' seconds +return /* Handle program termination */ ::ROUTINE ProgramHalt signal off halt Say "PROGRAM TERMINATED AT THE KEYBOARD" - exit 0 +exit 0 + diff --git a/config.json b/config.json new file mode 100644 index 0000000..63910f8 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "dbFile":"/home/gmgauthier/Projects/rexx-address-book/db/contacts.sqlite" +}