Skip to content

Commit ea34896

Browse files
Merge pull request #3 from thecloudnatives/add-prune-unused-dry-run
Add --dry-run flag instead of prompt
2 parents eddd6e7 + 0b2eed4 commit ea34896

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Usage:
3333
3434
Options:
3535
-n, --namespace='': If present, the namespace scope for this CLI request
36+
--dry-run: If true, only print the object that would be pruned, without deleting it.
3637
-h, --help: Display this help
3738
```
3839

prune-unused/kubectl-prune_unused.sh

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function usage() {
1616
echo ""
1717
echo "Options:"
1818
echo " -n, --namespace='': If present, the namespace scope for this CLI request"
19+
echo " --dry-run: If true, only print the object that would be pruned, without deleting it."
1920
echo " -h, --help: Display this help"
2021
exit 0
2122
}
@@ -26,6 +27,7 @@ then
2627
fi
2728

2829
resource=$1
30+
dry_run="false"
2931

3032
while test $# -gt 1
3133
do
@@ -40,6 +42,10 @@ do
4042
fi
4143
shift
4244
;;
45+
--dry-run)
46+
dry_run="true"
47+
shift
48+
;;
4349
-h|--help)
4450
usage
4551
;;
@@ -138,14 +144,12 @@ then
138144
exit 0
139145
fi
140146

141-
echo "About to delete the following $resource: ${resource_name_list}"
142-
143-
# confirmation prompt
144-
read -p "Delete listed resource(s)? [yes/no]: " -r
145-
if [[ $REPLY =~ ^[Yy]es$ ]]
146-
then
147-
for resource_name in $resource_name_list
148-
do
147+
for resource_name in $resource_name_list
148+
do
149+
if [ "$dry_run" == "true" ]
150+
then
151+
echo "${resource} ${resource_name} deleted (dry run)"
152+
else
149153
kubectl delete $resource $resource_name $namespace_arg
150-
done
151-
fi
154+
fi
155+
done

0 commit comments

Comments
 (0)