Skip to content

Commit

Permalink
feat: add shell json
Browse files Browse the repository at this point in the history
  • Loading branch information
jouyouyun committed May 14, 2019
1 parent 8eae1c8 commit bf53134
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 0 deletions.
21 changes: 21 additions & 0 deletions shell/json/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CC=g++ -Wall -g
PWD=$(shell pwd)
PROG=json_shell

vpath %.cpp ${PWD} ../../cpp/runner
LDFLAGS := -lboost_system -lboost_filesystem -lboost_thread -lpthread

OBJS := json_shell.o task.o task_manager.o

build: ${PROG}

${PROG}: ${OBJS}
${CC} -o $@ $^ ${LDFLAGS}

%.o:%.cpp
${CC} -c $<

clean:
rm -f *.o ${PROG}

rebuild: clean build
64 changes: 64 additions & 0 deletions shell/json/config_softwware.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

VALUES=$1

for s in $(echo $VALUES | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"); do
export $s
done

function configSoftware() {
local markSoft=$1
markSoft=${markSoft//\[/}
markSoft=${markSoft//\]/}
local markList=(${markSoft//,/ })

local availSoft=$2
availSoft=${availSoft//\[/}
availSoft=${availSoft//\]/}
local availList=(${availSoft//,/ })

declare -a removeList
idx=0
for mark in "${markList[@]}"; do
found=0
for avail in "${availList[@]}"; do
if [ $mark == $avail ]; then
found=1
fi
done
if [ $found == 0 ]; then
removeList[$idx]=$mark
((idx++))
fi
done

removeLen=${#removeList[@]}
if [ $removeLen != 0 ]; then
# remove
echo "Will remove: apt-get remove ${removeList[@]}"
# apt-get remove --allow-remove-essential ${removeList[@]}
fi
availLen=${#availList[@]}
if [ $availLen != 0 ]; then
# install
echo "Will install: apt-get install -y ${availList[@]}"
# apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" ${availList[@]}
fi
}

# MARK_SOFTWARES=`echo "$1" | jq -c '.MARK_SOFTWARES'`
# AVAILABLE_SOFTWARES=`echo "$1" | jq -c '.AVAILABLE_SOFTWARES'`
echo "Mark: ${MARK_SOFTWARES}"
echo "Avail: ${AVAILABLE_SOFTWARES}"

if [ ! -z ${MARK_SOFTWARES} ]; then
echo "Has mark"
fi

if [ ! -z ${AVAILABLE_SOFTWARES} ]; then
echo "Has avail"
fi

if [ ! -z ${MARK_SOFTWARES} -a ! -z ${AVAILABLE_SOFTWARES} ]; then
configSoftware ${MARK_SOFTWARES} ${AVAILABLE_SOFTWARES}
fi
58 changes: 58 additions & 0 deletions shell/json/dmcg_software_diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

if [ "$1"x == ""x ]; then
echo "Please input mark and available json"
exit
fi

if [ "$2"x == ""x ]; then
echo "Please input mark and available json"
exit
fi

markRet=`cat $1 | jq -c '[.[]]'`
echo "Software Mark: ${markRet}"
markRet=${markRet//\[/}
markRet=${markRet//\]/}
markList=(${markRet//,/ })

availRet=`cat $2 | jq -c '[.available_softwares[]]'`
echo "Available List: ${availRet}"
availRet=${availRet//\[/}
availRet=${availRet//\]/}
availList=(${availRet//,/ })

function is_in_available_list() {
local name=$1
local list=$2

for avail in "${availList[@]}"; do
echo -e "\tCompare: ${avail}, ${name}"
if [ "$avail"x == "$name"x ]; then
return 1
fi
done

return 0
}

declare -a removeList
idx=0
for mark in "${markList[@]}"; do
echo -e "\nStart: ${mark}"
is_in_available_list ${mark}
ret=$?
echo "Ret: $ret, $idx"
if [ "${ret}"x == "0"x ]; then
removeList[$ret]=$mark
echo "After insert: ${removeList}"
((idx++))
fi
done

removeLen=${#removeList[@]}
if [ $removeLen == 0 ]; then
echo "nothing to do"
exit
fi
echo "Remove len(${removeLen}): apt-get purge ${removeList[@]}"
94 changes: 94 additions & 0 deletions shell/json/json_shell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Compile: g++ -Wall -g
#include <iostream>
#include <fstream>
#include <vector>
#include <nlohmann/json.hpp>

#include <boost/shared_ptr.hpp>

#include "../../cpp/runner/task.h"
#include "../../cpp/runner/task_manager.h"

using namespace std;

static string
load_filename(const string &filename)
{
string content;
try {
fstream fr;
fr.open(filename, ios::in);
istreambuf_iterator<char> beg(fr);
istreambuf_iterator<char> end;
content = string(beg, end);
} catch (const exception &e) {
cout<<"Load file failed: "<<e.what()<<endl;
}

return content;
}

static nlohmann::json
parse_json_file(const string &filename)
{
nlohmann::json json;
string content = load_filename(filename);
if (content.empty()) {
return json;
}
try {
json = nlohmann::json::parse(content);
} catch (const exception &e) {
cout<<"Parse content failed: "<<e.what()<<endl;
}
return json;
}

int
main(int argc, char *argv[])
{
if (argc != 4) {
cout<<"Usage: "<<argv[0]<<" <shell> <mark software file> <avail software file>"<<endl;
return -1;
}

nlohmann::json mark_json = parse_json_file(argv[2]);
nlohmann::json avail_json = parse_json_file(argv[3]);
if (mark_json.is_null() || avail_json.is_null()) {
return -1;
}

nlohmann::json values;
values["MARK_SOFTWARES"] = mark_json;
values["AVAILABLE_SOFTWARES"] = avail_json["available_softwares"];
vector<string> args = {values.dump(-1)};

namespace dmr = dmcg::module::runner;
dmr::TaskManager manager;
boost::shared_ptr<dmr::Task> task = manager.Create(argv[1], args);
if (!task) {
cout<<"Create task failed"<<endl;
return -1;
}

task->Finish.connect([task](
const string &exception,
const int &exit_code,
const string &output,
const string &error_output
){
if (!exception.empty()) {
cout<<"Exception: "<<exception<<endl;
return;
}
cout<<"Exit code: "<<exit_code<<endl;
if (!error_output.empty()) {
cout<<"stderr: "<<error_output<<endl;
return;
}
cout<<"stdout: "<<output<<endl;
});
task->Run(true);

return 0;
}
1 change: 1 addition & 0 deletions shell/json/testdata/pc_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pc_config":{"iptables":{"content":["iptables -A INPUT -p tcp --dport 80 -j DROP","iptables -A OUTPUT -p tcp --sport 80 -j DROP"]},"screensaver":{"enable":true,"delay":600},"power":{"display_standby":3600},"pam_cracklib":{"minlen":20,"reject_username":true,"minclass":0},"wallpaper":{"image":"/data/wallpaper/1557747710158231205.png"},"script":{"name":"empty.sh","content":"","upload_at":"2019-05-10T05:22:29Z"}},"available_softwares":["deepin-terminal","libghc-aeson-qq-dev","libghc-aeson-qq-doc"],"can_login":false}
1 change: 1 addition & 0 deletions shell/json/testdata/software_mark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["deepin-terminal","libghc-aeson-qq-dev","libghc-aeson-qq-doc","parcellite"]

0 comments on commit bf53134

Please sign in to comment.