From 5a9a1ba57fc45df63cff123a42905360c3a7bddb Mon Sep 17 00:00:00 2001
From: Dennis Camera <dennis.camera@ssrq-sds-fds.ch>
Date: Wed, 15 Jan 2020 22:00:56 +0100
Subject: [PATCH] [__line] Produce error when file does not exist

---
 cdist/conf/type/__line/explorer/state |  5 +----
 cdist/conf/type/__line/gencode-remote | 17 +++++++++++------
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/cdist/conf/type/__line/explorer/state b/cdist/conf/type/__line/explorer/state
index 6ff0a798..e8fc3630 100755
--- a/cdist/conf/type/__line/explorer/state
+++ b/cdist/conf/type/__line/explorer/state
@@ -25,10 +25,7 @@ else
    file="/$__object_id"
 fi
 
-if [ ! -f "$file" ]; then
-  echo "file_missing"
-  exit 0
-fi
+[ -f "$file" ] || exit 0
 
 if [ -f "$__object/parameter/before" ]; then
    position="before"
diff --git a/cdist/conf/type/__line/gencode-remote b/cdist/conf/type/__line/gencode-remote
index c8c90c38..88cae68b 100755
--- a/cdist/conf/type/__line/gencode-remote
+++ b/cdist/conf/type/__line/gencode-remote
@@ -24,9 +24,20 @@ if [ -f "$__object/parameter/before" ] && [ -f "$__object/parameter/after" ]; th
    exit 1
 fi
 
+if [ -f "$__object/parameter/file" ]; then
+   file="$(cat "$__object/parameter/file")"
+else
+   file="/$__object_id"
+fi
+
 state_should="$(cat "$__object/parameter/state")"
 state_is="$(cat "$__object/explorer/state")"
 
+if [ -z "$state_is" ]; then
+   printf 'The file "%s" is missing. Please create it before using %s on it.\n' "$file" "${__type##*/}" >&2
+   exit 1
+fi
+
 if [ "$state_should" = "$state_is" ]; then
    # nothing to do
    exit 0
@@ -47,12 +58,6 @@ else
    needle="line"
 fi
 
-if [ -f "$__object/parameter/file" ]; then
-   file="$(cat "$__object/parameter/file")"
-else
-   file="/$__object_id"
-fi
-
 add=0
 remove=0
 case "$state_should" in