ALR

Форк
1
/
repo.go 
162 строки · 3.8 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package main
20

21
import (
22
	"os"
23
	"path/filepath"
24

25
	"github.com/pelletier/go-toml/v2"
26
	"github.com/urfave/cli/v2"
27
	"plemya-x.ru/alr/internal/config"
28
	"plemya-x.ru/alr/internal/db"
29
	"plemya-x.ru/alr/internal/types"
30
	"plemya-x.ru/alr/pkg/loggerctx"
31
	"plemya-x.ru/alr/pkg/repos"
32
	"golang.org/x/exp/slices"
33
)
34

35
var addrepoCmd = &cli.Command{
36
	Name:    "addrepo",
37
	Usage:   "Add a new repository",
38
	Aliases: []string{"ar"},
39
	Flags: []cli.Flag{
40
		&cli.StringFlag{
41
			Name:     "name",
42
			Aliases:  []string{"n"},
43
			Required: true,
44
			Usage:    "Name of the new repo",
45
		},
46
		&cli.StringFlag{
47
			Name:     "url",
48
			Aliases:  []string{"u"},
49
			Required: true,
50
			Usage:    "URL of the new repo",
51
		},
52
	},
53
	Action: func(c *cli.Context) error {
54
		ctx := c.Context
55
		log := loggerctx.From(ctx)
56

57
		name := c.String("name")
58
		repoURL := c.String("url")
59

60
		cfg := config.Config(ctx)
61

62
		for _, repo := range cfg.Repos {
63
			if repo.URL == repoURL {
64
				log.Fatal("Repo already exists").Str("name", repo.Name).Send()
65
			}
66
		}
67

68
		cfg.Repos = append(cfg.Repos, types.Repo{
69
			Name: name,
70
			URL:  repoURL,
71
		})
72

73
		cfgFl, err := os.Create(config.GetPaths(ctx).ConfigPath)
74
		if err != nil {
75
			log.Fatal("Error opening config file").Err(err).Send()
76
		}
77

78
		err = toml.NewEncoder(cfgFl).Encode(cfg)
79
		if err != nil {
80
			log.Fatal("Error encoding config").Err(err).Send()
81
		}
82

83
		err = repos.Pull(ctx, cfg.Repos)
84
		if err != nil {
85
			log.Fatal("Error pulling repos").Err(err).Send()
86
		}
87

88
		return nil
89
	},
90
}
91

92
var removerepoCmd = &cli.Command{
93
	Name:    "removerepo",
94
	Usage:   "Remove an existing repository",
95
	Aliases: []string{"rr"},
96
	Flags: []cli.Flag{
97
		&cli.StringFlag{
98
			Name:     "name",
99
			Aliases:  []string{"n"},
100
			Required: true,
101
			Usage:    "Name of the repo to be deleted",
102
		},
103
	},
104
	Action: func(c *cli.Context) error {
105
		ctx := c.Context
106
		log := loggerctx.From(ctx)
107

108
		name := c.String("name")
109
		cfg := config.Config(ctx)
110

111
		found := false
112
		index := 0
113
		for i, repo := range cfg.Repos {
114
			if repo.Name == name {
115
				index = i
116
				found = true
117
			}
118
		}
119
		if !found {
120
			log.Fatal("Repo does not exist").Str("name", name).Send()
121
		}
122

123
		cfg.Repos = slices.Delete(cfg.Repos, index, index+1)
124

125
		cfgFl, err := os.Create(config.GetPaths(ctx).ConfigPath)
126
		if err != nil {
127
			log.Fatal("Error opening config file").Err(err).Send()
128
		}
129

130
		err = toml.NewEncoder(cfgFl).Encode(&cfg)
131
		if err != nil {
132
			log.Fatal("Error encoding config").Err(err).Send()
133
		}
134

135
		err = os.RemoveAll(filepath.Join(config.GetPaths(ctx).RepoDir, name))
136
		if err != nil {
137
			log.Fatal("Error removing repo directory").Err(err).Send()
138
		}
139

140
		err = db.DeletePkgs(ctx, "repository = ?", name)
141
		if err != nil {
142
			log.Fatal("Error removing packages from database").Err(err).Send()
143
		}
144

145
		return nil
146
	},
147
}
148

149
var refreshCmd = &cli.Command{
150
	Name:    "refresh",
151
	Usage:   "Pull all repositories that have changed",
152
	Aliases: []string{"ref"},
153
	Action: func(c *cli.Context) error {
154
		ctx := c.Context
155
		log := loggerctx.From(ctx)
156
		err := repos.Pull(ctx, config.Config(ctx).Repos)
157
		if err != nil {
158
			log.Fatal("Error pulling repos").Err(err).Send()
159
		}
160
		return nil
161
	},
162
}
163

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.