system

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2023 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoAdminsDefined = errors.Business("No admins defined", "SY:001")
	NoRootsDefined  = errors.Business("No roots defined", "SY:002")
	BackupIsEmpty   = errors.Business("🪣 Backup is empty", "SY:003")
)
View Source
var BackupCmd = &cli.Command{
	Name:  "backup",
	Usage: "do system backup",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:     "notify",
			Usage:    "send information to roots",
			Required: true,
		},
		&cli.BoolFlag{
			Name:  "force",
			Usage: "force notify even if got error",
		},
	},
	Action: func(cmd *cli.Context) error {
		if !cmd.Bool("notify") {
			return ErrOnlyNotifyTrue
		}

		force := cmd.Bool("force")

		cfg := *config.Ctx(cmd.Context)

		roots := cfg.Telegram.Roots

		if len(roots) == 0 {
			return NoRootsDefined
		}

		logger := zerolog.Ctx(cmd.Context).
			With().
			Str("context", "system").
			Logger()

		bot, err := telegram.NewBot(cfg)
		if err != nil {
			return err
		}

		notifyOnError := func(err error) {
			_ = system.NotifyBackupErr(cmd.Context, bot, cmd.Args().First(), roots, err)
		}

		db, err := database.Open(cfg.Store.Path)
		if err != nil {
			if force {
				notifyOnError(err)
			}

			return err
		}

		defer func() {
			logger.Warn().Err(db.Close()).Msg("Database closed.")
		}()

		logger.Debug().Msg("Generating backup...")

		file, destroy, err := system.Backup(cmd.Context, db)
		if err != nil {
			if force {
				notifyOnError(err)
			}

			return err
		}

		defer destroy()

		logger.Info().Msgf("Temp backup file created: %s", file.Name())

		st, err := file.Stat()
		if err != nil {
			if force {
				notifyOnError(err)
			}

			return err
		}

		if st.Size() == 0 {
			notifyOnError(BackupIsEmpty)

			return BackupIsEmpty
		}

		return system.NotifyBackup(cmd.Context, bot, cmd.Args().First(), roots, file)
	},
}
View Source
var ErrOnlyNotifyTrue = errors.Business("only notify true is supported", "SY:003")
View Source
var InfoCmd = &cli.Command{
	Name:  "info",
	Usage: "load system info",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:        "notify",
			Usage:       "send information to admins",
			DefaultText: "false",
		},
	},
	Action: func(cmd *cli.Context) error {
		cfg := *config.Ctx(cmd.Context)

		data, err := system.New()
		if err != nil {
			return err
		}

		if cmd.Bool("notify") {
			admins := cfg.Telegram.Admins

			logger := zerolog.Ctx(cmd.Context).
				With().
				Str("context", "system").
				Logger()

			if len(admins) == 0 {
				return NoAdminsDefined
			}

			bot, err := telegram.NewBot(cfg)
			if err != nil {
				return err
			}

			msg := data.MarkdownV2(cmd.Args().First())

			for _, id := range admins {
				_, err = bot.Send(&telebot.User{
					ID: id,
				}, msg, telebot.ModeMarkdownV2)

				if err != nil {
					logger.Warn().Err(err).Int64("userId", id).Msg("Fail to sent info")
				}
			}

			logger.Info().Msgf("System info sent do telegram %v", admins)

			return nil
		}

		return data.Println()
	},
}
View Source
var SystemCmd = &cli.Command{
	Name:        "system",
	Usage:       "load system info",
	Subcommands: []*cli.Command{InfoCmd, BackupCmd},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL