"james/dotfiles" did not exist on "1042df79ab6e547769d2f35b7f58142943690f3c"
refresh-dotfiles 3.17 KB
Newer Older
1
#!/usr/bin/env bash
James T. Lee's avatar
James T. Lee committed
2
3
4
#
# refresh-dotfiles
#
5
# Pull the latest configuration from Git and process ERB templates with Puppet
James T. Lee's avatar
James T. Lee committed
6
7
#

8
9
# Process files relative to this script.
# This needs to be an absolute path for use in the generated Puppet manifest.
10
BASEDIR="$(cd "$(dirname "$0")/.." && echo "$PWD")"
11

12
# Just download and extract compiled repository in non-git-based directories
13
14
if [[ ! -d "${BASEDIR}/.git" ]]; then
    curl -s https://gitlab.james.tl/james/dotfiles/-/archive/preprocessed/dotfiles-preprocessed.tar.gz | tar -C "$BASEDIR" -xvzf - --strip 1
15
16
17
    exit
fi

James T. Lee's avatar
James T. Lee committed
18
# Create place to store generated Puppet manifest
19
20
MANIFEST="$(mktemp)"
trap "rm -f '$MANIFEST'" EXIT INT TERM
James T. Lee's avatar
James T. Lee committed
21

22
# Puppet on Windows is not a Cygwin app--it needs Windows paths
James T. Lee's avatar
James T. Lee committed
23
24
if [[ $OSTYPE == 'cygwin' ]]; then
    BASEDIR="$(cygpath -am "$BASEDIR")"
25
    MANIFEST="$(cygpath -am "$MANIFEST")"
James T. Lee's avatar
James T. Lee committed
26
27
28
29
30
    PUPPET='puppet.bat'
else
    PUPPET='puppet'
fi

31
32
33
34
# A place to keep track of what this script manages
DOT_TEMPLATES="${BASEDIR}/.templates"


James T. Lee's avatar
James T. Lee committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#
# Read and parse command line options
#
usage() {
    cat >&2 <<END
Usage: refresh-dotfiles [options]

Options:
  --no-fetch    don't fetch updates from git
  -h, --help    display this help message and exit

END
}

49
ARGS=$(getopt -o 'h' -l 'no-fetch,help' -n 'refresh-dotfiles' -- "$@")
James T. Lee's avatar
James T. Lee committed
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

if [ $? -ne 0 ]; then
    usage
    exit 1
fi

eval set -- "$ARGS"

while true; do
    case "$1" in
        '--no-fetch')
            shift
            no_fetch="yes"
            ;;
        '-h'|'--help')
            shift
            usage
            exit 0
            ;;
        '--')
            shift
            break
            ;;
    esac
done

generate_manifest() {
    # Create/update dotfiles from templates in git
    git ls-files '*.erb' | while read template; do
79
        mode="$(stat -c '%a' "$template")"
James T. Lee's avatar
James T. Lee committed
80
        cat <<END
81
file { '${BASEDIR}/${template%.erb}':
James T. Lee's avatar
James T. Lee committed
82
  mode    => '${mode}',
83
  content => template('${BASEDIR}/${template}'),
James T. Lee's avatar
James T. Lee committed
84
85
86
87
88
}

file_line { '${template%.erb}':
  path    => '${DOT_TEMPLATES}',
  line    => '${template%.erb}',
89
  require => File['${BASEDIR}/${template%.erb}'],
James T. Lee's avatar
James T. Lee committed
90
91
92
93
94
}
END
    done

    # Remove dotfiles that we've managed in the past that are not in git
James T. Lee's avatar
James T. Lee committed
95
96
97
98
    # (processed through 'tr' because file_line generates Windows line
    # endings on Windows which 'read' doesn't strip off)
    < "$DOT_TEMPLATES" tr -d '\r' | while read dotfile; do
        if [[ ! -f "${BASEDIR}/${dotfile}.erb" ]]; then
James T. Lee's avatar
James T. Lee committed
99
            cat <<END
100
file { '${BASEDIR}/${dotfile}':
James T. Lee's avatar
James T. Lee committed
101
102
103
104
105
106
107
  ensure => absent,
}

file_line { '${dotfile}':
  ensure  => absent,
  path    => '${DOT_TEMPLATES}',
  line    => '${dotfile}',
108
  require => File['${BASEDIR}/${dotfile}'],
James T. Lee's avatar
James T. Lee committed
109
110
111
}
END
        fi
James T. Lee's avatar
James T. Lee committed
112
    done
James T. Lee's avatar
James T. Lee committed
113
114
115
116
117
118
}


#
# Main program
#
119
cd "$BASEDIR"
120
touch "$DOT_TEMPLATES"
James T. Lee's avatar
James T. Lee committed
121

122
123
branch="$(git rev-parse --abbrev-ref HEAD)"

James T. Lee's avatar
James T. Lee committed
124
125
# Grab latest updates from git
if [[ -z $no_fetch ]]; then
James T. Lee's avatar
James T. Lee committed
126
    git fetch
James T. Lee's avatar
James T. Lee committed
127
128
129
    git reset --hard "origin/${branch}"
fi

130
131
# Process templates with Puppet if we're on the main branch or a clone
if [[ $branch == 'main' || $PWD != $HOME ]]; then
132
    generate_manifest > "$MANIFEST"
133

134
135
136
137
138
139
    cat >> "$MANIFEST" <<END_SPECIAL
File <| title == '${BASEDIR}/.ssh/config' |> {
  mode => '0600',
}
END_SPECIAL

140
    "$PUPPET" apply --modulepath="${BASEDIR}/lib/puppet" "$MANIFEST"
141
fi