aboutsummaryrefslogtreecommitdiff
path: root/lib/keyringer/functions
blob: 22f16f85fecf88e2c32fe4ce1bf8dbc46523d806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
#
# Common functions.
#

# Setup main configuration and load preferences
function keyringer_config_load {
  if [ -f "$HOME/.$NAME" ]; then
    echo "Converting legacy configuration scheme..."
    mv $HOME/.$NAME $HOME/.$NAME.tmp
    mkdir $HOME/.$NAME
    mv $HOME/.$NAME.tmp $CONFIG
  fi

  if [ ! -e "$CONFIG" ]; then
    echo "Creating $CONFIG..."
    mkdir `dirname $CONFIG`
    touch $CONFIG
    chmod 600 $CONFIG
    echo "# Keyringer config file." > $CONFIG
    echo "" >> $CONFIG
  fi

  keyringer_config_load_preferences
}

function keyringer_config_load_preferences {
  # Load custom keyring preferences
  if [ ! -z "$PREFERENCES" ] && [ -f "$PREFERENCES" ]; then
    source $PREFERENCES
  fi
}

# Load a parameter from config
function keyringer_config {
  if [ -z "$CONFIG" ]; then
    echo "Your have to set CONFIG variable in the code"
    exit 1
  elif [ -e "$CONFIG" ]; then
    grep -e "^$1=" $CONFIG | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'
  else
    echo "Config file not found: $CONFIG"
    exit 1
  fi
}

# Return the list of recipients
function keyringer_recipients {
  grep -v '^#' $1 | grep -v '^$' | awk '{ print "-r " $2 }' | xargs
}

# Check if keyringer has a given action
function keyringer_has_action {
 if [ -z "$ACTIONS" ]; then
   echo "Your have to set ACTIONS variable in the code"
   exit 1
 fi

 if [ -e "$ACTIONS/$1" ]; then
   true
 else
   false
 fi
}

# Execute an action
function keyringer_exec {
  # Setup
  action="$1"
  basedir="$2"
  shift 2
  
  # Dispatch
  if keyringer_has_action $action; then
    $ACTIONS/$action $basedir $*
  fi
}

# Return a filename with correct extension
function keyringer_filename {
  if [ -z "$1" ]; then
    return
  else
    echo `dirname $1`/`basename $1 .asc`.asc
  fi
}

# Check if a folder is inside a git repository
function keyringer_is_git {
  if [ -z "$1" ]; then
    false
  elif [ ! -d "$1" ]; then
    false
  elif [ -d "$1/.git" ]; then
    true
  else
    cwd="`pwd`"
    cd $1 && git="`git status &> /dev/null`" && cd $cwd

    if [ "$git" != "128" ]; then
      true
    else
      false
    fi
  fi
}

# Setup a temporary file
function keyringer_set_tmpfile {
  if [ -z "$BASEDIR" ]; then
    echo "Please set BASEDIR before creating a tmp file"
    exit 1
  fi

  if [ -z "$1" ]; then
    template="$BASEDIR/tmp/keyringer.XXXXXXXXXX"
  else
    template="$BASEDIR/tmp/$1.XXXXXXXXXX"
  fi

  mkdir -p $BASEDIR/tmp
  keyringer_git_ignore 'tmp/*'

  tmpfile="`mktemp $template`"
  
  if [ "$?" != "0" ]; then
    echo "Error: can't set tmpfile $tmpfile"
    exit 1
  fi

  echo $tmpfile
}

# Remove a temporary file
function keyringer_unset_tmpfile {
  if [ -z "$1" ]; then
    echo "No tmp file set"
  fi

  rm -f $1

  if [ "$?" != "0" ]; then
    echo "Warning: could not delete file $1. Please delete it manually as it might have sensitive information."
    exit 1
  fi
}

# Add a pattern into gitignore
function keyringer_git_ignore {
  if [ ! -z "$BASEDIR/.gitignore" ]; then
    echo $1 > $BASEDIR/.gitignore
    keyringer_exec git $BASEDIR add .gitignore
  else
    if ! grep -q -e "^$1$" $BASEDIR/.gitignore; then
      echo $1 >> $BASEDIR/.gitignore
    fi
  fi
}