1+ #! /bin/bash
2+
3+ # Shared variables
4+ BINARY_NAME=" meshsync-snapshot"
5+ TAR_NAME=" meshsync-snapshot.tar.gz"
6+ BINARY_PATH=" bin/${BINARY_NAME} "
7+ TAR_PATH=" bin/${TAR_NAME} "
8+ SHA256_PATH=" bin/${TAR_NAME} .sha256"
9+ TEMPLATE_FILE=" meshsync-snapshot-local-template.yaml"
10+
11+ # Check dependencies function
12+ check_dependencies () {
13+ echo " Checking dependencies..."
14+ local missing_deps=()
15+
16+ # Check for required commands
17+ if ! command -v go & > /dev/null; then
18+ missing_deps+=(" go" )
19+ fi
20+
21+ if ! command -v kubectl & > /dev/null; then
22+ missing_deps+=(" kubectl" )
23+ fi
24+
25+ if ! command -v tar & > /dev/null; then
26+ missing_deps+=(" tar" )
27+ fi
28+
29+ if ! command -v sha256sum & > /dev/null; then
30+ missing_deps+=(" sha256sum" )
31+ fi
32+
33+ # Check for kubectl krew plugin
34+ if ! kubectl krew version & > /dev/null; then
35+ missing_deps+=(" kubectl-krew" )
36+ fi
37+
38+ # Report results
39+ if [ ${# missing_deps[@]} -eq 0 ]; then
40+ echo " ✓ All dependencies are available"
41+ return 0
42+ else
43+ echo " ✗ Missing dependencies:"
44+ for dep in " ${missing_deps[@]} " ; do
45+ echo " - $dep "
46+ done
47+ echo " "
48+ echo " Please install the missing dependencies before proceeding."
49+ echo " For kubectl krew installation: https://krew.sigs.k8s.io/docs/user-guide/setup/install/"
50+ return 1
51+ fi
52+ }
53+
54+ # Build function
55+ build () {
56+ echo " build"
57+ go build -o ${BINARY_PATH} cmd/meshsync/* .go
58+
59+ echo " tar"
60+ tar -czvf ${TAR_PATH} ${BINARY_PATH}
61+
62+ echo " sha256"
63+ sha256sum ${TAR_PATH} > ${SHA256_PATH}
64+ cat ${SHA256_PATH}
65+ }
66+
67+ # Install function
68+ install () {
69+ kubectl krew install --manifest=meshsync-snapshot.yaml
70+ }
71+
72+ # Install local function
73+ install_local () {
74+ # Check if tar file exists
75+ if [ ! -f " ${TAR_PATH} " ]; then
76+ echo " ⚠️ Warning: ${TAR_PATH} not found. Building first..."
77+ build
78+ elif [ ! -f " ${SHA256_PATH} " ]; then
79+ echo " ⚠️ Warning: ${SHA256_PATH} not found. Building first..."
80+ build
81+ else
82+ # Check if current hash matches stored hash
83+ current_hash=$( sha256sum " ${TAR_PATH} " | cut -d' ' -f1)
84+ stored_hash=$( cut -d' ' -f1 " ${SHA256_PATH} " )
85+
86+ if [ " ${current_hash} " != " ${stored_hash} " ]; then
87+ echo " ⚠️ Warning: Hash mismatch detected!"
88+ echo " Stored hash: ${stored_hash} "
89+ echo " Real hash: ${current_hash} "
90+ echo " Rebuilding..."
91+ build
92+ fi
93+ fi
94+
95+ # Get the current hash for template replacement
96+ current_hash=$( sha256sum " ${TAR_PATH} " | cut -d' ' -f1)
97+
98+ # Check if template file exists
99+ if [ ! -f " ${TEMPLATE_FILE} " ]; then
100+ echo " ❌ Error: Template file ${TEMPLATE_FILE} not found"
101+ exit 1
102+ fi
103+
104+ # Create temporary manifest file (cross-platform)
105+ local_manifest=$( mktemp -t meshsync-manifest.XXXXXX.yaml)
106+
107+ # Ensure cleanup on exit
108+ trap " rm -f '${local_manifest} '" EXIT
109+
110+ # Create manifest from template with real hash
111+ echo " 📝 Generating manifest from template..."
112+ sed " s/<sha256 hash placeholder, populated from make, DO NOT UPDATE>/${current_hash} /g" " ${TEMPLATE_FILE} " > " ${local_manifest} "
113+
114+ echo " 🚀 Installing with generated manifest..."
115+ kubectl krew install --manifest=" ${local_manifest} " --archive=" ${TAR_PATH} "
116+ }
117+
118+ # Uninstall function
119+ uninstall () {
120+ kubectl krew uninstall meshsync-snapshot 2> /dev/null
121+ }
122+
123+ # Help function
124+ help () {
125+ echo " Usage: $0 {check_dependencies|build|install|install_local|uninstall}"
126+ echo " "
127+ echo " Commands:"
128+ echo " check_dependencies Check if all required dependencies are installed"
129+ echo " build Build binary and create tar archive"
130+ echo " install Install using krew with remote manifest"
131+ echo " install_local Install using krew with local manifest and archive"
132+ echo " uninstall Uninstall the plugin"
133+ }
134+
135+ # Main script logic
136+ case " $1 " in
137+ check_dependencies)
138+ check_dependencies
139+ ;;
140+ build)
141+ build
142+ ;;
143+ install)
144+ install
145+ ;;
146+ install_local)
147+ install_local
148+ ;;
149+ uninstall)
150+ uninstall
151+ ;;
152+ help|--help|-h)
153+ help
154+ ;;
155+ * )
156+ echo " Error: Unknown command '$1 '"
157+ help
158+ exit 1
159+ ;;
160+ esac
0 commit comments