rubric/catalog/devopsbook.go
2026-04-13 14:48:06 +03:00

99 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package catalog
import (
"bufio"
"fmt"
"rubric/modules/devopsmodules"
"rubric/utils"
"strconv"
"strings"
)
var DevOpsBook = Book{
Key: "devops",
Aliases: []string{"do", "dev"},
Title: "Справочник по DevOps",
Description: "Git, Docker, Kubernetes, CI/CD, Ansible, Terraform, мониторинг, Nginx",
Run: runDevOpsBook,
}
func runDevOpsBook(scanner *bufio.Scanner) {
for {
showDevOpsMenu()
input, ok := ReadLine(scanner)
if !ok {
return
}
if input == "" {
continue
}
if IsQuit(input) {
fmt.Printf("\n %s%sДо свидания!%s\n\n", utils.ColorBold, utils.ColorCyan, utils.ColorReset)
return
}
if IsBack(input) {
return
}
if n, err := strconv.Atoi(input); err == nil {
if n >= 1 && n <= len(devopsmodules.All) {
devopsmodules.All[n-1].Run()
waitDevOpsBack(scanner)
continue
}
fmt.Printf("\n %s%sНомер %d вне диапазона (1%d)%s\n",
utils.ColorBold, utils.ColorRed, n, len(devopsmodules.All), utils.ColorReset)
continue
}
if m, ok := devopsmodules.Find(input); ok {
m.Run()
waitDevOpsBack(scanner)
continue
}
fmt.Printf("\n %s%sНе понял '%s'. Номер, ключ раздела, b — назад.%s\n",
utils.ColorBold, utils.ColorRed, input, utils.ColorReset)
}
}
func showDevOpsMenu() {
utils.Header("Справочник DevOps")
fmt.Printf(" %s%sРазделы:%s\n\n", utils.ColorBold, utils.ColorWhite, utils.ColorReset)
for i, m := range devopsmodules.All {
aliases := ""
if len(m.Aliases) > 0 {
aliases = fmt.Sprintf("%s [%s]%s", utils.ColorDim, strings.Join(m.Aliases, ", "), utils.ColorReset)
}
fmt.Printf(" %s%s %2d.%s %s%-14s%s %s%-28s%s%s\n",
utils.ColorBold, utils.ColorCyan, i+1, utils.ColorReset,
utils.ColorYellow+utils.ColorBold, m.Key, utils.ColorReset,
utils.ColorWhite, m.Title, utils.ColorReset, aliases)
fmt.Printf(" %s%s%s\n\n", utils.ColorDim, m.Description, utils.ColorReset)
}
fmt.Printf(" %s%sРаздел (номер / ключ / b — главное меню / q — выход):%s ",
utils.ColorBold, utils.ColorGreen, utils.ColorReset)
}
type devopsBackSignal struct{}
func waitDevOpsBack(scanner *bufio.Scanner) {
fmt.Printf("\n %s%s[Enter — назад в меню DevOps, b — главное меню]%s ",
utils.ColorDim, utils.ColorCyan, utils.ColorReset)
if scanner.Scan() {
if IsBack(strings.TrimSpace(scanner.Text())) {
panic(devopsBackSignal{})
}
}
}
func init() {
orig := DevOpsBook.Run
DevOpsBook.Run = func(scanner *bufio.Scanner) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(devopsBackSignal); !ok {
panic(r)
}
}
}()
orig(scanner)
}
}