Skip to content

Commit

Permalink
Split JSON into smaller parts + tools for its management (#159)
Browse files Browse the repository at this point in the history
* Split JSON into multible files
* split json
* rejointed JSON
* fix code style error
* added tool readme
* added sort order
  • Loading branch information
Tearran authored Oct 7, 2024
1 parent 27455ee commit 4202012
Show file tree
Hide file tree
Showing 9 changed files with 1,285 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/armbian-configng/config.ng.jobs.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@
"description": "Upgrade to latest stable / LTS",
"prompt": "Release upgrade is irriversible operation which upgrades all packages. \n\nResoulted upgrade might break your build beyond repair!",
"command": [
"release_upgrade stable"
],
"release_upgrade stable"
],
"status": "Active",
"author": "Igor Pecovnik",
"condition": "[ -f /etc/armbian-distribution-status ] && release_upgrade stable verify"
Expand All @@ -377,8 +377,8 @@
"description": "Upgrade to rolling unstable",
"prompt": "Release upgrade is irriversible operation which upgrades all packages. \n\nResoulted upgrade might break your build beyond repair!",
"command": [
"release_upgrade rolling"
],
"release_upgrade rolling"
],
"status": "Active",
"author": "Igor Pecovnik",
"condition": "[ -f /etc/armbian-distribution-status ] && release_upgrade rolling verify"
Expand All @@ -397,7 +397,7 @@
"author": "Gunjan Gupta",
"condition": "[ -n $OVERLAYDIR ] && [ -n $BOOT_SOC ]"
}
]
]
},
{
"id": "Network",
Expand Down
20 changes: 20 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Tools Directory

This folder contains scripts for managing files for the armbian-config project.

## Overview

**config-jobs** -h

```
Options:
-s <input_file> Split JSON file into smaller parts
-j <output_file> Join multiple JSON files into one
-h Display this help message
```

## Dependencies

- Requires [jq](https://stedolan.github.io/jq/) for JSON processing.


172 changes: 172 additions & 0 deletions tools/config-jobs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/bin/bash


SCRIPT_DIR="$(dirname "$0")"
LIB_DIR="$SCRIPT_DIR/../lib/armbian-config"

DEFAULT_FILE="$LIB_DIR/config.jobs.json"
DEFAULT_DIR="$SCRIPT_DIR/json"


# Function to display the help message
print_help() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -s <input_file> Split JSON file into smaller parts"
echo " -j <output_file> Join multiple JSON files into one"
echo " -h Display this help message"
}

# Function to split JSON into smaller files
split_json() {
input_file="$1"
output_dir="$DEFAULT_DIR"

# Check if input file exists
if [[ ! -f "$input_file" ]]; then
echo "Error: Input file '$input_file' does not exist."
return 1
fi

# Create output directory if it doesn't exist
if [[ ! -d "$output_dir" ]]; then
mkdir -p "$output_dir"
fi

# Extract the total number of menu items
count=$(jq '.menu | length' "$input_file")

# Iterate over each menu item and save it as a separate JSON file
for ((i=0; i<count; i++)); do
item=$(jq ".menu[$i]" "$input_file")
menu_id=$(jq -r ".menu[$i].id" "$input_file") # Extract the 'id' of the current item
filename=$(echo "$menu_id" | tr '[:upper:]' '[:lower:]') # Convert filename to lowercase

# Save backup file with proper indentation
echo "{\"menu\": [$item]}" | jq --indent 4 '.' > "$output_dir/config.${filename}.json"

# Extract 'sub' and replace it with 'menu' if it exists
# jq ".menu[$i] | if has(\"sub\") then .sub | {menu: .} else {menu: [.] } end" "$input_file" > "$output_dir/config.${filename}.json"
done

echo "Splitting and transformation completed. Files are saved in '$output_dir'."
}

# Function to join JSON files into a single file
join_json_previous_draft() {
input_dir="$DEFAULT_DIR"
output_file="$1"

# Check if input directory exists
if [[ ! -d "$input_dir" ]]; then
echo "Error: Input directory '$input_dir' does not exist."
return 1
fi

# Initialize an empty array for holding the merged JSON data
merged_json="[]"
help_section="null"

# Loop through all JSON files in the directory
for file in "$input_dir"/*.json; do
if [[ -f "$file" ]]; then
# Check if the JSON file is the "Help" section
if jq -e '.menu[] | select(.id == "Help")' "$file" > /dev/null 2>&1; then
# Extract the "Help" item if it's found
help_section=$(jq '.menu[] | select(.id == "Help")' "$file")
else
# Extract the rest of the "menu" array content and add it to the merged array
array_content=$(jq '.menu' "$file")
merged_json=$(jq --argjson new_item "$array_content" '. + $new_item' <<< "$merged_json")
fi
fi
done

# Add the "Help" section to the merged JSON
if [[ "$help_section" != "null" ]]; then
merged_json=$(jq --argjson help_item "$help_section" '. + [$help_item]' <<< "$merged_json")
fi

# Construct the final JSON structure
final_json=$(jq -n --argjson menu "$merged_json" '{"menu": $menu}')

# Write the final JSON structure to a file
echo "$final_json" | jq --indent 4 '.' > "$output_file"

echo "JSON files rejoined into '$output_file'."
}

# Function to join JSON files into a single file with enforced ordering
join_json() {
input_dir="$DEFAULT_DIR"
output_file="$1"

# Check if input directory exists
if [[ ! -d "$input_dir" ]]; then
echo "Error: Input directory '$input_dir' does not exist."
return 1
fi

# Initialize an empty array for holding the merged JSON data
merged_json="[]"

# Define desired order for menu items (IDs)
declare -a ordered_ids=(
"System"
"Network"
"Localisation"
"Software"
"Help"
)

# Loop through ordered IDs to extract menu items in the correct order
for id in "${ordered_ids[@]}"; do
for file in "$input_dir"/*.json; do
if [[ -f "$file" ]]; then
# Find item matching the current ID
item=$(jq --arg id "$id" '.menu[] | select(.id == $id)' "$file" 2>/dev/null)
if [[ -n "$item" ]]; then
merged_json=$(jq --argjson new_item "$item" '. + [$new_item]' <<< "$merged_json")
fi
fi
done
done

# Construct the final JSON structure
final_json=$(jq -n --argjson menu "$merged_json" '{"menu": $menu}')

# Write the final JSON structure to a file
echo "$final_json" | jq --indent 4 '.' > "$output_file"

echo "JSON files rejoined into '$output_file'."
}



# Main script logic with case statement
case "$1" in
-s)
if [[ -z "$2" ]]; then
echo "Error: Missing arguments for -s option."
print_help
exit 1
fi
split_json "$2"
;;
-j)
if [[ -z "$2" ]]; then
echo "Error: Missing arguments for -j option."
print_help
exit 1
fi
join_json "$2"
;;
-h)
print_help
;;
*)
echo "Error: Invalid option."
print_help
exit 1
;;
esac
32 changes: 32 additions & 0 deletions tools/json/config.help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"menu": [
{
"id": "Help",
"description": "About this app",
"sub": [
{
"id": "H00",
"description": "About This system. (WIP)",
"command": [
"show_message <<< \"This app is to help execute procedures to configure your system\n\nSome options may not work on manually modified systems\""
],
"status": "Preview",
"doc_link": "",
"src_reference": "",
"author": ""
},
{
"id": "H02",
"description": "List of Config function(WIP)",
"command": [
"show_message <<< see_use"
],
"status": "Disabled",
"doc_link": "",
"src_reference": "",
"author": ""
}
]
}
]
}
66 changes: 66 additions & 0 deletions tools/json/config.localisation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"menu": [
{
"id": "Localisation",
"description": "Localisation",
"sub": [
{
"id": "L00",
"description": "Change Global timezone (WIP)",
"command": [
"dpkg-reconfigure tzdata"
],
"status": "Preview",
"doc_link": "",
"src_reference": "",
"author": ""
},
{
"id": "L01",
"description": "Change Locales reconfigure the language and character set",
"command": [
"dpkg-reconfigure locales",
"source /etc/default/locale ; sed -i \"s/^LANGUAGE=.*/LANGUAGE=$LANG/\" /etc/default/locale",
"export LANGUAGE=$LANG"
],
"status": "Preview",
"doc_link": "",
"src_reference": "",
"author": ""
},
{
"id": "L02",
"description": "Change Keyboard layout",
"command": [
"dpkg-reconfigure keyboard-configuration ; setupcon ",
"update-initramfs -u"
],
"status": "Preview",
"doc_link": "",
"src_reference": "",
"author": ""
},
{
"id": "L03",
"description": "Change APT mirrors",
"prompt": "This will change the APT mirrors\nWould you like to continue?",
"command": [
"get_user_continue \"This is only a frontend test\" process_input"
],
"status": "Disabled",
"author": ""
},
{
"id": "L04",
"description": "Change System Hostname",
"command": [
"NEW_HOSTNAME=$(whiptail --title \"Enter new hostnane\" --inputbox \"\" 7 50 3>&1 1>&2 2>&3)",
"[ $? -eq 0 ] && [ -n \"${NEW_HOSTNAME}\" ] && hostnamectl set-hostname \"${NEW_HOSTNAME}\""
],
"status": "Preview",
"author": ""
}
]
}
]
}
Loading

0 comments on commit 4202012

Please sign in to comment.