Skip to content

Target v2

target <uniq_name> <key> <value>
target some_name host 10.0.2.5

/usr/bin/target

#!/usr/bin/python3
import sys
import json
import os
import os.path

uid = sys.argv[1]
key = sys.argv[2] if len(sys.argv) > 2 else ''
val = sys.argv[3] if len(sys.argv) > 3 else ''
dir = os.getcwd() + "/"
action = ''


def safe(uid):
    if not os.path.isfile(dir + uid + '.json'):
        with open(dir + uid + '.json', 'w') as f:
            json.dump({}, f)


def save(uid, key, val):
    safe(uid)
    storage = all(uid)
    storage[key] = val
    with open(dir + uid + '.json', 'w') as f:
        json.dump(storage, f)


def get(uid, key):
    try:
        safe(uid)
        return all(uid)[key]
    except:
        return ''


def all(uid):
    safe(uid)
    with open(dir + uid + '.json', 'r') as f:
        return json.load(f)


if key != '' and val != '':
    action = 'SAVE'
elif key != '':
    action = 'READ'
else:
    action = 'ALL'

# print(f"Workspace '{uid}' Key '{key}' Value '{val}' Action '{action}'")
if action == 'SAVE':
    save(uid, key, val)
if action == 'READ':
    print(get(uid, key))
if action == 'ALL':
    print(all(uid))