C-Docs: Set the version of hugo-book theme
[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     fi
70
71     hugo_book_url="github.com/alex-shpak/hugo-book"
72     hugo_book_version="v0.0.0-20230424134111-d86d5e70c7c0"
73     hugo_book_link="${hugo_book_url}@${hugo_book_version}"
74     pushd "${CSIT_DIR}"/docs || die "Pushd failed!"
75     export PATH=$PATH:/usr/local/go/bin
76     hugo mod get "${hugo_book_link}" || die "Failed to run Hugo mod!"
77     popd || die "Popd failed!"
78 }
79
80
81 function hugo_install () {
82
83     # Install Hugo Extended.
84
85     OS_ARCH=$(uname -m) || die "Failed to get arch."
86     case "${OS_ARCH}" in
87         x86_64) architecture="amd64" ;;
88         aarch64) architecture="arm64" ;;
89     esac
90
91     hugo_version="v0.111.3/hugo_extended_0.111.3_linux-${architecture}.deb"
92     hugo_url="https://github.com/gohugoio/hugo/releases/download"
93     hugo_link="${hugo_url}/${hugo_version}"
94     wget -O "hugo.deb" "${hugo_link}" || die "Failed to install Hugo!"
95     dpkg -i "hugo.deb" || die "Failed to install Hugo!"
96     rm "hugo.deb" || die "Failed to install Hugo!"
97 }
98
99
100 function installed () {
101
102     # Check if the given utility is installed. Fail if not installed.
103     #
104     # Arguments:
105     # - ${1} - Utility to check.
106     # Returns (implicitly):
107     # - 0 - If command is installed.
108     # - 1 - If command is not installed.
109
110     set -exuo pipefail
111
112     command -v "${1}"
113 }