]> us-delaporte.pagekite.me Git - dotfiles.git/blob - bin/dot
Tighten up core help
[dotfiles.git] / bin / dot
1 #!/bin/bash
2 ENVS="$HOME/envs"
3 ANSIBLE="$ENVS/ansible/bin/ansible-playbook"
4 read -d '' HELP << EOF
5 dot - a (mostly) one script dotfiles manager
6
7 Copyright 2026 Edward Delaporte V
8 Licensed under AGPLv3 and later.
9
10 dot setup
11 install ansible CLI to $ANSIBLE
12
13 dot link FILE
14 updates ~/dot.HOSTNAME.yaml
15 to include a symlink from ~/dotfiles/files/FILE to ~/.FILE
16
17 dot check
18 check setup against ~/backup/dot.HOSTNAME.yaml
19
20 dot apply
21 apply config in ~/backup/dot.HOSTNAME.yaml
22
23 dot help FEATURE
24 setup - more information on setup
25 all - additional features
26 EOF
27
28 read -d '' SETUP_HELP << EOF
29 ## Setup
30
31 1. Install this file on your path
32 2. Clone your (or my) 'dotfiles' repository to ~/dotfiles
33 3. Run 'dot help' to get started.
34
35 EOF
36
37
38 read -d '' EXTRA_HELP << EOF
39
40 dot install SCRIPT
41 updates ~/dot.HOSTNAME.yaml
42 to run ~/dotfiles/SCRIPT.sh if it has not yet been run
43 creates ~/dot/SCRIPT.installed after SCRIPT has been run
44
45 EOF
46
47
48 if [ ! -n "$1" ] || [ "$1" == "help" ]; then
49 echo "$HELP"
50 fi
51 if [ "$1" == "help" ] && [ "$2" == "all" ]; then
52 echo ""
53 echo "$EXTRA_HELP"
54 echo ""
55 echo "$SETUP_HELP"
56 fi
57 if [ "$1" == "help" ] && [ "$2" == "setup" ]; then
58 echo ""
59 echo "$SETUP_HELP"
60 fi
61
62 # set -v
63 #
64
65 TASKFILE="$HOME/backup/dot.$HOSTNAME.yaml"
66 LINKFILES="$HOME/dotfiles/files"
67 INSTALLS="$HOME/dotfiles/install"
68 DOT="$HOME/.dot"
69
70 read -d '' EMPTY_TASKFILE << EOF
71 ---
72 - name: Set up dotfiles on $HOSTNAME
73 gather_facts: no
74 hosts: localhost
75 connection: local
76
77 tasks:
78 - name: Check on $TASKFILE
79 ansible.builtin.stat:
80 path: $TASKFILE
81 EOF
82
83 if [[ ! -f "$TASKFILE" ]]; then
84 echo "Creating empty $TASKFILE"
85 mkdir -p "$HOME/backup"
86 echo "$EMPTY_TASKFILE" >> $TASKFILE
87 fi
88
89 if [ "$1" == "setup" ]; then
90 if [[ ! -f "$ANSIBLE" ]]; then
91 mkdir -p "$ENVS"
92 echo "Setting up Ansible in ~/envs/ansible"
93 cd $HOME/envs; python3 -m venv ansible
94 $HOME/envs/ansible/bin/pip install ansible jinja2
95 fi
96 if [[ -f "$ANSIBLE" ]]; then
97 echo "[ ok ]: Ansible setup at $ANSIBLE"
98 fi
99 if [[ -f "$TASKFILE" ]]; then
100 echo "[ ok ]: dot config at $TASKFILE"
101 fi
102 fi
103
104 if [ "$1" == "reset" ]; then
105 read -n1 -p "Reset $TASKFILE? [y,N]" input
106 if [[ ! $input == "Y" && ! $input == "y" ]]; then
107 echo "Aborted by user"
108 exit 1
109 fi
110 echo "$EMPTY_TASKFILE" > $TASKFILE
111 echo ""
112 fi
113
114
115 if [ "$1" == "check" ]; then
116 $ANSIBLE $TASKFILE --check --diff
117 fi
118
119 if [ "$1" == "link" ]; then
120 if [ ! -n "$2" ]; then
121 echo "Can symlink: (from $LINKFILES)"
122 cd $LINKFILES
123 ls -p | grep -v /
124 exit 1
125 fi
126
127 read -d '' LINK_THIS_FILE << EOF
128
129 - name: symlink $2
130 ansible.builtin.file:
131 src: "$LINKFILES/$2"
132 dest: "$HOME/.$2"
133 state: "link"
134 EOF
135
136 if [[ "$2" == *"."* ]];then
137 # File already contains a dot
138 read -d '' LINK_THIS_FILE << EOF
139
140 - name: symlink $2
141 ansible.builtin.file:
142 src: "$LINKFILES/$2"
143 dest: "$HOME/$2"
144 state: "link"
145 EOF
146
147
148 fi
149
150 echo " $LINK_THIS_FILE" >> $TASKFILE
151 echo "$2 added to symlinks in $TASKFILE"
152 echo "Run 'dot apply' to create all configured symlinks"
153 fi
154
155
156 if [ "$1" == "apply" ]; then
157 if [[ ! -f "$TASKFILE" ]]; then
158 echo "$TASKFILE is missing. Run 'dot setup'"
159 exit 1
160 fi
161 $ANSIBLE $TASKFILE
162 fi
163
164 if [ "$1" == "install" ]; then
165 if [ ! -n "$2" ]; then
166 echo "Can install: (.sh scripts in $INSTALLS)"
167 cd $INSTALLS
168 ls -p | grep -v / | grep .sh
169 exit 1
170 fi
171 if [[ ! -f "$INSTALLS/$2.sh" ]]; then
172 echo "No such install script $INSTALLS/$2.sh"
173 echo "Run dot install with no params to list scripts."
174 exit 1
175 fi
176
177 mkdir -p $DOT
178 read -d '' RUN_INSTALL << EOF
179 - name: check $2
180 stat:
181 path: "$DOT/$2.installed"
182 register: $2_installed
183 - name: install $2
184 ansible.builtin.shell: |
185 "$INSTALLS/$2.sh"
186 touch "$DOT/$2.installed"
187 when: not $2_installed.stat.exists
188 - name: verify $2 is installed
189 ansible.builtin.debug:
190 msg: "$2 is installed."
191 when: $2_installed.stat.exists
192 EOF
193 echo " $RUN_INSTALL" >> $TASKFILE
194 echo "Run 'dot apply' to run the install"
195 fi