Files
benadis-rac/cmd/main.go
2025-08-04 11:03:25 +03:00

121 lines
3.4 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 main
import (
"context"
"flag"
"fmt"
"os"
"time"
"git.benadis.ru/gitops/benadis-rac/internal/config"
"git.benadis.ru/gitops/benadis-rac/internal/constants"
"git.benadis.ru/gitops/benadis-rac/internal/logger"
"git.benadis.ru/gitops/benadis-rac/internal/service"
)
func main() {
// Определяем флаги командной строки
var (
configPath = flag.String("config", "config.yaml", "Path to config file")
secretPath = flag.String("secret", "secret.yaml", "Path to secret file")
projectPath = flag.String("project", "project.yaml", "Path to project file")
command = flag.String("command", "", "Command to execute: enable, disable, status")
showVersion = flag.Bool("version", false, "Show version")
showHelp = flag.Bool("help", false, "Show help")
)
flag.Parse()
// Показываем версию
if *showVersion {
fmt.Printf(constants.MsgVersionFormat, constants.AppVersion)
return
}
// Показываем справку
if *showHelp || *command == "" {
showUsage()
return
}
// Загружаем конфигурацию
cfg, err := config.LoadConfig(*configPath, *secretPath, *projectPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
// Валидируем конфигурацию
if err := cfg.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "Config validation error: %v\n", err)
os.Exit(1)
}
// Создаем логгер
log := logger.NewLogger(cfg.Project.ServiceMode.LogLevel)
// Создаем сервис
svc := service.NewServiceModeService(cfg, log)
// Создаем контекст с таймаутом
ctx, cancel := context.WithTimeout(context.Background(), constants.DefaultMainTimeout*time.Minute)
defer cancel()
// Выполняем команду
if err := executeCommand(ctx, svc, log, *command); err != nil {
log.Error("Command execution failed", "command", *command, "error", err)
os.Exit(1)
}
}
// executeCommand выполняет указанную команду
func executeCommand(ctx context.Context, svc *service.ServiceModeService, log logger.Logger, command string) error {
switch command {
case constants.CmdEnable:
return svc.EnableServiceMode(ctx)
case constants.CmdDisable:
return svc.DisableServiceMode(ctx)
case constants.CmdStatus:
status, err := svc.GetServiceModeStatus(ctx)
if err != nil {
return err
}
if status {
log.Info(constants.StatusServiceModeEnabled)
} else {
log.Info(constants.StatusServiceModeDisabled)
}
return nil
default:
return fmt.Errorf(constants.ErrUnknownCommand, command)
}
}
// showUsage показывает справку по использованию
func showUsage() {
fmt.Printf(`GitOps RAC v%s - 1C Service Mode Management Tool
Usage:
benadis-rac [options] -command <command>
Commands:
enable - Enable service mode
disable - Disable service mode
status - Show current service mode status
Options:
-config <path> Path to config file (default: config.yaml)
-secret <path> Path to secret file (default: secret.yaml)
-project <path> Path to project file (default: project.yaml)
-version Show version
-help Show this help
Examples:
benadis-rac -command enable
benadis-rac -command disable
benadis-rac -command status
benadis-rac -config /path/to/config.yaml -secret /path/to/secret.yaml -project /path/to/project.yaml -command enable
`, constants.AppVersion)
}