Skip to content

Commit

Permalink
Merge pull request #18 from infeeeee/dev
Browse files Browse the repository at this point in the history
1.1.0 config location in envvar
  • Loading branch information
infeeeee authored Sep 24, 2020
2 parents 0fd58c9 + 806cd70 commit a631a30
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 50 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ You can create your settings.ini file manually, by downloading, renaming and edi

On the windows installer version settings.ini location: `C:\Users\Username\AppData\Roaming\kimai2-cmd\settings.ini`

You can also store the settings.ini file in custom location, just export the full path to the `KIMAI_CONFIG` variable, something like this:

```bash
export KIMAI_CONFIG=$XDG_CONFIG_HOME/kimai2/settings.ini
```

Integration settings are not asked during first run, you have to change them manually in settings.ini. If you don't use an integration, you can safely ignore it's settings.

## Development version
Expand Down
105 changes: 58 additions & 47 deletions kimai2-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ function callKimaiApi(httpMethod, kimaiMethod, serversettings, options = false)
const qs = options.qs || false
const reqbody = options.reqbody || false

if (program.verbose) {
console.log("calling kimai:", httpMethod, kimaiMethod, serversettings)
}
debug("calling kimai:", httpMethod, kimaiMethod, serversettings)

return new Promise((resolve, reject) => {
const options = {
Expand All @@ -72,9 +70,7 @@ function callKimaiApi(httpMethod, kimaiMethod, serversettings, options = false)
options.headers['Content-Type'] = 'application/json'
}

if (program.verbose) {
console.log("request options:", options)
}
debug("request options:", options)

request(options, (error, response, body) => {
if (error) {
Expand All @@ -83,9 +79,7 @@ function callKimaiApi(httpMethod, kimaiMethod, serversettings, options = false)

let jsonarr = JSON.parse(response.body)

if (program.verbose) {
console.log("Response body:", jsonarr)
}
debug("Response body:", jsonarr)

if (jsonarr.message) {
console.log('Server error message:')
Expand Down Expand Up @@ -153,9 +147,9 @@ function uiMainMenu(settings) {
]
}])
.then(answers => {
if (program.verbose) {
console.log('selected answer: ' + answers.mainmenu)
}

debug('selected answer: ' + answers.mainmenu)

switch (answers.mainmenu) {
case 'restart':
kimaiList(settings, 'timesheets/recent', false)
Expand Down Expand Up @@ -273,9 +267,8 @@ function kimaiStart(settings, project, activity) {
project: project,
activity: activity
}
if (program.verbose) {
console.log("kimaistart calling api:", body)
}

debug("kimaistart calling api:", body)

callKimaiApi('POST', 'timesheets', settings.serversettings, {
reqbody: body
Expand Down Expand Up @@ -402,16 +395,15 @@ function kimaiList(settings, endpoint, print = false, options = false) {
* @param {string} endpoint for selecting display layout
*/
function printList(settings, arr, endpoint) {
if (program.verbose) {
console.log()
if (arr.length > 1) {
console.log(arr.length + ' results:')
} else if (arr.length == 0) {
console.log('No results')
} else {
console.log('One result:')
}

if (arr.length > 1) {
debug(arr.length + ' results:')
} else if (arr.length == 0) {
debug('No results')
} else {
debug('One result:')
}

//no result for scripts:
if (arr.length == 0) {
if (program.argos) {
Expand Down Expand Up @@ -590,11 +582,23 @@ function uiAutocompleteSelect(thelist, message) {
* @returns false: If no settings found
*/
function iniPath() {
if (program.verbose) {
console.log('Looking for settings.ini in the following places:')
console.log(iniRoot)

// Check path in environment variable
if (process.env.KIMAI_CONFIG) {
const envIniPath = process.env.KIMAI_CONFIG
debug('Found in KIMAI_CONFIG envvar: ' + envIniPath)
if (fs.existsSync(envIniPath)) {
return envIniPath
} else {
debug('KIMAI_CONFIG variable malformed')
}
} else {
debug('No environment variable found')
}

debug('Looking for settings.ini in the following places:')
debug(iniRoot)

for (var key in iniRoot) {
if (iniRoot.hasOwnProperty(key)) {
const currentIniPath = path.join(iniRoot[key], '/settings.ini')
Expand All @@ -615,9 +619,13 @@ function iniPath() {
*/
function checkSettings() {
return new Promise((resolve, reject) => {




const settingsPath = iniPath()
if (settingsPath) {
if (program.verbose) console.log("settings.ini found at: ", settingsPath)
debug("settings.ini found at: " + settingsPath)
let settings = ini.parse(fs.readFileSync(settingsPath, 'utf-8'))
resolve(settings)
} else {
Expand All @@ -631,6 +639,14 @@ function checkSettings() {
})
}

/**
* Prints to console if verbose
* @param {string} msg
*/
function debug(msg) {
if (program.verbose) console.log(msg)
}

/**
* Interactive ui: asks for settings than saves them
*
Expand Down Expand Up @@ -681,9 +697,7 @@ function uiAskForSettings() {
settings.rainmeter.meterstyle = "styleProjects"

const thePath = iniFullPath()
if (program.verbose) {
console.log('Trying to save settings to: ' + thePath)
}
debug('Trying to save settings to: ' + thePath)

fs.writeFileSync(thePath, ini.stringify(settings))
console.log('Settings saved to ' + iniPath())
Expand All @@ -702,23 +716,20 @@ function iniFullPath() {

//Maybe I should replace this terrible 'if' with some registry value reading
if (platform == 'win32' && installDir[installDir.length - 2] == "Program Files" && installDir[installDir.length - 1] == "kimai2-cmd") {
if (program.verbose) {
console.log('This is an installer based windows installation')
}
debug('This is an installer based windows installation')

if (!fs.existsSync(path.join(appdata, 'kimai2-cmd'))) {
fs.mkdirSync(path.join(appdata, 'kimai2-cmd'))
}
return path.join(iniRoot.wininstaller, 'settings.ini')
} else if (dirArr[0] == 'snapshot' || dirArr[1] == 'snapshot') {
if (program.verbose) {
console.log('This is a pkg version')
}
debug('This is a pkg version')

//for pkg version:
return path.join(iniRoot.pkg, 'settings.ini')
} else {
if (program.verbose) {
console.log('This is an npm version')
}
debug('This is an npm version')

//For npm version:
return path.join(iniRoot.npm, 'settings.ini')
}
Expand Down Expand Up @@ -809,13 +820,13 @@ function updateRainmeter(settings) {
fs.writeFileSync(rainmeterDataPath, rainmeterDataIni, {
encoding: 'utf16le'
})
if (program.verbose) {
console.log("Rainmeter files:")
console.log(rainmeterVarPath, rainmeterDataPath)
console.log("rainmeter data:")
console.log(rainmeterVars)
console.log(rainmeterDataIni)
}

debug("Rainmeter files:")
debug(rainmeterVarPath, rainmeterDataPath)
debug("rainmeter data:")
debug(rainmeterVars)
debug(rainmeterDataIni)

})
}

Expand Down
2 changes: 1 addition & 1 deletion kimai2-innosetup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{A10BF7B2-6641-4B06-9C68-268B649FCE57}
AppName=kimai2-cmd
AppVersion=1.0.2
AppVersion=1.1.0
AppPublisher=infeeeee
AppPublisherURL=https://github.com/infeeeee/kimai2-cmd
AppSupportURL=https://github.com/infeeeee/kimai2-cmd
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kimai2-cmd",
"version": "1.0.2",
"version": "1.1.0",
"description": "Command line client for Kimai2",
"main": "kimai2-cmd.js",
"bin": {
Expand Down

0 comments on commit a631a30

Please sign in to comment.