system

package
v0.6.0-beta Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoAdminsDefined = errors.Business("No admins defined", "SY:001")
	NoRootssDefined = errors.Business("No roots defined", "SY:002")
)
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,
		},
	},
	Action: func(cmd *cli.Context) error {
		if !cmd.Bool("notify") {
			return ErrOnlyNotifyTrue
		}

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

		roots := cfg.Telegram.Roots

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

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

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

		defer func() {
			_ = db.Close()
		}()

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

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

		defer destroy()

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

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

		document := telegram.Document(
			file,
			fmt.Sprintf("*System Notify*\n\n*Peristera Backup:\n*%s", time.Now().Format(time.RFC3339)),
		)

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

			if err != nil {
				return err
			}
		}

		logger.Info().Msgf("System backup sent do telegram %v", roots)

		return nil
	},
}
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 {
					return err
				}
			}

			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