b2e4387eddb063e39cfe1655e5722ffacc681240
[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     OS_ARCH=$(uname -m) || die "Failed to get arch."
24     case "${OS_ARCH}" in
25         x86_64) architecture="amd64" ;;
26         aarch64) architecture="arm64" ;;
27     esac
28
29     go_version="go1.20.2.linux-${architecture}.tar.gz"
30     go_url="https://go.dev/dl"
31     wget "${go_url}/${go_version}"
32     rm -rf "/usr/local/go"
33     tar -C "/usr/local" -xzf "go1.20.2.linux-${architecture}.tar.gz"
34     rm "go1.20.2.linux-${architecture}.tar.gz"
35     export PATH=$PATH:/usr/local/go/bin
36 }
37
38
39 function hugo_build_site () {
40
41     # Build site via Hugo.
42     #
43     # Variable read:
44     # - ${CSIT_DIR} - CSIT main directory.
45     # Functions called:
46     # - die - Print to stderr and exit.
47
48     if ! installed hugo; then
49         die "Please install Hugo!"
50     fi
51
52     pushd "${CSIT_DIR}"/docs || die "Pushd failed!"
53     hugo || die "Failed to run Hugo build!"
54     popd || die "Popd failed!"
55 }
56
57
58 function hugo_init_modules () {
59
60     # Initialize Hugo modules.
61     #
62     # Variable read:
63     # - ${CSIT_DIR} - CSIT main directory.
64     # Functions called:
65     # - die - Print to stderr and exit.
66
67     if ! installed hugo; then
68         #die "Please install Hugo!"
69         go_install || die "Failed to install Go!"
70         hugo_install || die "Failed to install Hugo!"
71         terraform_install || die "Failed to install Terraform!"
72     fi
73
74     pushd "${CSIT_DIR}"/docs || die "Pushd failed!"
75     hugo mod get -u || die "Failed to run Hugo mod!"
76     popd || die "Popd failed!"
77 }
78
79
80 function hugo_install () {
81
82     # Install Hugo Extended.
83
84     OS_ARCH=$(uname -m) || die "Failed to get arch."
85     case "${OS_ARCH}" in
86         x86_64) architecture="amd64" ;;
87         aarch64) architecture="arm64" ;;
88     esac
89
90     hugo_version="v0.111.3/hugo_extended_0.111.3_linux-${architecture}.deb"
91     hugo_url="https://github.com/gohugoio/hugo/releases/download"
92     hugo_link="${hugo_url}/${hugo_version}"
93     wget -O "hugo.deb" "${hugo_link}" || die "Failed to install Hugo!"
94     dpkg -i "hugo.deb" || die "Failed to install Hugo!"
95     rm "hugo.deb" || die "Failed to install Hugo!"
96 }
97
98
99 function installed () {
100
101     # Check if the given utility is installed. Fail if not installed.
102     #
103     # Arguments:
104     # - ${1} - Utility to check.
105     # Returns (implicitly):
106     # - 0 - If command is installed.
107     # - 1 - If command is not installed.
108
109     set -exuo pipefail
110
111     command -v "${1}"
112 }