aboutsummaryrefslogtreecommitdiff
path: root/zsh/functions/ssh-key-helpers.zsh
blob: e9b7900107fcd30dbfc04b82bd56e893f04ea673 (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
#!/usr/bin/env zsh

set-git-signing-key() {
  local key_path="$1"

  if [[ -z "$key_path" ]]; then
    echo "Error: Please provide a path to the private key to use to sign git commits." >&2
    return 1
  fi

  # Check if key exists
  if [[ ! -f "$key_path" ]]; then
    echo "Error: Key file does not exist: $key_path" >&2
    return 1
  fi

  local absolute_path=$(readlink -f "$key_path")

  ln -sf "$absolute_path" ~/.ssh/git
  ln -sf "${absolute_path}.pub" ~/.ssh/git.pub

  if [[ $? -eq 0 ]]; then
    echo "Successfully linked git signing key"
    return 0
  else
    echo "Error: Failed to create symbolic link" >&2
    return 1
  fi
}

generate-ssh-key() {
  local key_name="$1"
  local key_description="$2"

  if [[ -z "$key_name" ]]; then
    echo "Error: Please provide a name for the key." >&2
    return 1
  fi

  if [[ -z "$key_description" ]]; then
    echo "Error: Please provide a description for the key." >&2
    return 1
  fi

  ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_${key_name}_$(date +%Y-%m-%d) -C ${key_description}
}