b8d4c87df2260767a7499a18812446f000e66997
[csit.git] / resources / libraries / bash / function / hugo.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2023 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function go_install () {
20
21     # Install Go.
22
23     go_version="go1.20.2.linux-arm64.tar.gz"
24     go_url="https://go.dev/dl"
25     wget "${go_url}/${go_version}"
26     rm -rf "/usr/local/go"
27     tar -C "/usr/local" -xzf "go1.20.2.linux-arm64.tar.gz"
28     rm "go1.20.2.linux-arm64.tar.gz"
29     export PATH=$PATH:/usr/local/go/bin
30 }
31
32
33 function hugo_build_site () {
34
35     # Build site via Hugo.
36     #
37     # Variable read:
38     # - ${CSIT_DIR} - CSIT main directory.
39     # Functions called:
40     # - die - Print to stderr and exit.
41
42     if ! installed hugo; then
43         die "Please install Hugo!"
44     fi
45
46     pushd "${CSIT_DIR}"/docs || die "Pushd failed!"
47     hugo || die "Failed to run Hugo build!"
48     popd || die "Popd failed!"
49 }
50
51
52 function hugo_init_modules () {
53
54     # Initialize Hugo modules.
55     #
56     # Variable read:
57     # - ${CSIT_DIR} - CSIT main directory.
58     # Functions called:
59     # - die - Print to stderr and exit.
60
61     if ! installed hugo; then
62         #die "Please install Hugo!"
63         go_install || die "Failed to install Go!"
64         hugo_install || die "Failed to install Hugo!"
65         terraform_install || die "Failed to install Terraform!"
66     fi
67
68     pushd "${CSIT_DIR}"/docs || die "Pushd failed!"
69     hugo mod get -u || die "Failed to run Hugo mod!"
70     popd || die "Popd failed!"
71 }
72
73
74 function hugo_install () {
75
76     # Install Hugo Extended.
77
78     hugo_version="v0.111.3/hugo_extended_0.111.3_linux-arm64.deb"
79     hugo_url="https://github.com/gohugoio/hugo/releases/download"
80     hugo_link="${hugo_url}/${hugo_version}"
81     wget -O "hugo.deb" "${hugo_link}" || die "Failed to install Hugo!"
82     dpkg -i "hugo.deb" || die "Failed to install Hugo!"
83     rm "hugo.deb" || die "Failed to install Hugo!"
84 }
85
86
87 function installed () {
88
89     # Check if the given utility is installed. Fail if not installed.
90     #
91     # Arguments:
92     # - ${1} - Utility to check.
93     # Returns (implicitly):
94     # - 0 - If command is installed.
95     # - 1 - If command is not installed.
96
97     set -exuo pipefail
98
99     command -v "${1}"
100 }