|
2 | 2 | # |
3 | 3 | # sets up environment in new server |
4 | 4 |
|
5 | | -INTERACTIVE= |
6 | | -HOME_DIRECTORY=~ |
7 | 5 |
|
| 6 | +# asks user before executing command |
| 7 | +# default is FALSE |
| 8 | +declare INTERACTIVE= |
| 9 | + |
| 10 | +# defaults to home of logged in user |
| 11 | +# but can be specified with -d flag |
| 12 | +declare HOME_DIRECTORY=~ |
| 13 | + |
| 14 | +#################################### |
| 15 | +# List of Commands to be Executed # |
| 16 | +#################################### |
| 17 | +declare -r COMMANDS=( |
| 18 | + # my .vimrc |
| 19 | + "curl https://gist.githubusercontent.com/sblack4/e5a8dda8c8b0f78984657c05f6712ae3/raw/c21eb0fa7d0b4e3e550ce484fd5773fe40ed5a21/.vimrc >> $HOME_DIRECTORY/.vimrc", |
| 20 | + |
| 21 | + # my .bashrc |
| 22 | + "curl https://gist.githubusercontent.com/sblack4/e5a8dda8c8b0f78984657c05f6712ae3/raw/c21eb0fa7d0b4e3e550ce484fd5773fe40ed5a21/.bashrc >> $HOME_DIRECTORY/.bashrc", |
| 23 | + |
| 24 | + # install my favs |
| 25 | + "yum install -y vim wget mlocate" |
| 26 | +) |
| 27 | +#################################### |
| 28 | + |
| 29 | + |
| 30 | +function loop_through_commands() { |
| 31 | + for command in "${COMMANDS[@]}" |
| 32 | + do |
| 33 | + if [ $INTERACTIVE ]; then |
| 34 | + echo "Execute the below command? Type n to skip" |
| 35 | + echo " $command" |
| 36 | + read ouput |
| 37 | + if [$output == "n"]; then |
| 38 | + continue |
| 39 | + fi |
| 40 | + fi |
| 41 | + |
| 42 | + eval_command $command |
| 43 | + done |
8 | 44 |
|
9 | | -usage() { |
10 | | -cat << EOF |
11 | | -Usage: $0 [-ih] [-d directory] |
12 | | - -d --directory $HOME_DIRECTORY set up in $HOME_DIRECTORY |
13 | | - -i interactive |
14 | | - -h displays help / usage |
15 | | -EOF |
16 | 45 | } |
17 | 46 |
|
18 | | -err() { |
19 | | - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2 |
20 | | - exit 1 |
| 47 | +# takes in command to be executed, |
| 48 | +# and then executes it |
| 49 | +# @param {string} bash command |
| 50 | +function eval_command() { |
| 51 | + local command=$1 |
| 52 | + local command_out=$( command 2>&1 ) |
| 53 | + local command_rc=$? |
| 54 | + |
| 55 | + { # try |
| 56 | + echo "Running $command..." |
| 57 | + eval $command |
| 58 | + echo "Success, $command_out" |
| 59 | + } || { # catch |
| 60 | + echo "Command '$command' failed " |
| 61 | + echo "stder $command_rc" |
| 62 | + } |
21 | 63 | } |
22 | 64 |
|
23 | | -get_dot_files() { |
| 65 | +function get_yes_no() { |
| 66 | + local command=$1 |
| 67 | + local valid_input= |
24 | 68 |
|
25 | | - cd $HOME_DIRECTORY |
| 69 | + echo "Execute the below command? Type n to skip" |
| 70 | + echo " $command" |
| 71 | + read ouput |
| 72 | + if [$output == "n"]; then |
26 | 73 |
|
27 | | - echo "setting up dotfiles for $USER in $HOME_DIRECTORY" |
| 74 | + fi |
28 | 75 |
|
29 | | - echo "$(PWD)" |
30 | | - |
31 | | - curl https://gist.githubusercontent.com/sblack4/e5a8dda8c8b0f78984657c05f6712ae3/raw/c21eb0fa7d0b4e3e550ce484fd5773fe40ed5a21/.vimrc >> .vimrc |
32 | | - |
33 | | - curl https://gist.githubusercontent.com/sblack4/e5a8dda8c8b0f78984657c05f6712ae3/raw/c21eb0fa7d0b4e3e550ce484fd5773fe40ed5a21/.bashrc >> .bashrc |
34 | | - |
35 | | - |
| 76 | +} |
36 | 77 |
|
| 78 | +function usage() { |
| 79 | +cat << EOF |
| 80 | +Usage: $0 [-ih] [-d directory] |
| 81 | + -d $HOME_DIRECTORY set up in $HOME_DIRECTORY |
| 82 | + -i interactive |
| 83 | + -h displays help / usage |
| 84 | +EOF |
37 | 85 | } |
38 | 86 |
|
39 | | -install_necessary_programs() { |
40 | | - # I'll be using yum, this can be changed |
| 87 | +function err() { |
| 88 | + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2 |
| 89 | + exit 1 |
| 90 | +} |
41 | 91 |
|
42 | | - sudo yum install -y vim wget mlocate |
| 92 | +function stop_on_error() { |
| 93 | + set -e |
43 | 94 | } |
44 | 95 |
|
45 | 96 | get_opts() { |
46 | 97 | while [ "$1" != "" ]; do |
47 | 98 | case $1 in |
48 | | - -d | --directory ) shift # shift to get following arg |
| 99 | + -d | --directory ) |
| 100 | + shift # shift to get following arg |
49 | 101 | HOME_DIRECTORY=$1 |
50 | 102 | ;; |
51 | | - -i | --interactive ) INTERACTIVE=1 |
| 103 | + |
| 104 | + -i | --interactive ) |
| 105 | + INTERACTIVE=1 |
| 106 | + ;; |
| 107 | + |
| 108 | + -e | --errors ) |
| 109 | + stop_on_error |
52 | 110 | ;; |
53 | | - -h | --help ) usage |
| 111 | + |
| 112 | + -h | --help ) |
| 113 | + usage |
54 | 114 | exit |
55 | 115 | ;; |
56 | | - * ) usage |
57 | | - # exit 1 |
| 116 | + |
| 117 | + * ) usage |
| 118 | + exit 1 |
58 | 119 | esac |
59 | 120 | shift |
60 | 121 | done |
|
0 commit comments