Skip to content

Commit

Permalink
wip: Enhance CLI functionalities #188
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Nouguier committed Oct 20, 2020
1 parent 3880f09 commit 5903c15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
54 changes: 33 additions & 21 deletions kargo.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ function diffConfig () {
return true
}*/

function listEnvironment () {
let variables = []
_.forEach(_.toPairs(config.environment), variable => {
variables.push({ name: variable[0], value: variable[1]})
function setEnvironment () {
if (!readStates()) return false
_.forEach(_.toPairs(states.config.environment), variable => {
shell.env[_.toUpper(_.snakeCase(variable[0]))] = variable[1]
})
return variables
return true
}

function listStacks () {
return _.keys(config.stacks)
return _.keys(states.config.stacks)
}

function listServices (stack) {
Expand Down Expand Up @@ -237,7 +237,8 @@ async function apply () {

async function deploy (stackOrAll) {
debug('[subcommand] deploy')
if (!readConfig()) return
if (!setEnvironment()) return



/*if (writeConfig()) {
Expand All @@ -251,25 +252,31 @@ async function deploy (stackOrAll) {
}*/
}

function exec () {
function exec (script) {
debug('[subcommand] exec')
if (readConfig()) {
if (!setEnvironment()) return false
let scriptFile = path.join(runtimeScriptsDir, script)
if (!fs.existsSync(scriptFile)) {
log('The specified script file \'' + script + '\' does not exist', 'error')
return
}
if (shell.exec(scriptFile).code!==0) {
log('An error has occured while executing \'' + script + '\'', 'error')
}
}

function info () {
debug('[subcommand] info')
if (readConfig()) {
_.forEach(listStacks(), stack => {
console.log('- %s', stack)
_.forEach(listServices(stack), service => {
console.log(' - %s', service.name)
_.forEach(_.toPairs(service.settings), settings => {
console.log(' - %s: %s', settings[0], settings[1])
})
if (!readStates()) return false
_.forEach(listStacks(), stack => {
console.log('- %s', stack)
_.forEach(listServices(stack), service => {
console.log(' - %s', service.name)
_.forEach(_.toPairs(service.settings), settings => {
console.log(' - %s: %s', settings[0], settings[1])
})
})
}
})
}

function plan () {
Expand All @@ -289,6 +296,7 @@ function pull () {
}

function use (workspace) {
debug('[subcommand] use')
// TODO check states
// Check whether the specified path exists
if (!fs.existsSync(workspace)) {
Expand All @@ -314,7 +322,12 @@ function use (workspace) {
// Configure the states
states = {
'workspace': { 'name': path.basename(workspace), 'path': workspace },
'config': { environment: {}, labels: {}, stacks: {} }
'config': {
environment: {
"kargo-version": require('./package.json').version
},
labels: {},
stacks: {} }
}
// Save the states
if (writeStates()) {
Expand Down Expand Up @@ -345,7 +358,6 @@ program
program
.command('use <workspace>')
.action((workspace) => use(workspace))

program.parse(process.argv)
program.parse(process.argv)


Empty file added scripts/display-env.sh
Empty file.
39 changes: 1 addition & 38 deletions scripts/update-labels.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
#!/bin/bash

usage() {
echo "usage: update-labels <node> <labels>"
}
echo $KARGO_VERSION

help() {
echo "Update the labels of the specified node"
usage
}

exec() {
local NODE=$1
local LABELS=$2
# Retrieve the existing labels
local EXISTING_LABELS=`docker node inspect -f '{{ range $k, $v := .Spec.Labels }}{{ $k }}={{ $v }} {{end}}' $NODE`
# Keep the existing labels if needed
for EXISTING_LABEL in $EXISTING_LABELS; do
if [[ ! $NEW_LABELS =~ $EXISTING_LABEL ]]; then
EXISTING_LABEL_KEY=${EXISTING_LABEL%=*}
docker node update --label-rm $EXISTING_LABEL_KEY $NODE > /dev/null
fi
done
# Add the label of needed
for LABEL in $LABELS; do
if [[ ! $EXISTING_LABELS =~ $LABEL ]]; then
docker node update --label-add $LABEL $NODE > /dev/null
fi
done
}

if [ "$#" -ne 2 ]; then
echo error: illegal number of arguments
usage
exit 1
fi

case $1 in
-h|--help) help;;
*) exec "$1" "$2"
esac

0 comments on commit 5903c15

Please sign in to comment.