ALR
/
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
19package main
20
21import (
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
35var addrepoCmd = &cli.Command{
36Name: "addrepo",
37Usage: "Add a new repository",
38Aliases: []string{"ar"},
39Flags: []cli.Flag{
40&cli.StringFlag{
41Name: "name",
42Aliases: []string{"n"},
43Required: true,
44Usage: "Name of the new repo",
45},
46&cli.StringFlag{
47Name: "url",
48Aliases: []string{"u"},
49Required: true,
50Usage: "URL of the new repo",
51},
52},
53Action: func(c *cli.Context) error {
54ctx := c.Context
55log := loggerctx.From(ctx)
56
57name := c.String("name")
58repoURL := c.String("url")
59
60cfg := config.Config(ctx)
61
62for _, repo := range cfg.Repos {
63if repo.URL == repoURL {
64log.Fatal("Repo already exists").Str("name", repo.Name).Send()
65}
66}
67
68cfg.Repos = append(cfg.Repos, types.Repo{
69Name: name,
70URL: repoURL,
71})
72
73cfgFl, err := os.Create(config.GetPaths(ctx).ConfigPath)
74if err != nil {
75log.Fatal("Error opening config file").Err(err).Send()
76}
77
78err = toml.NewEncoder(cfgFl).Encode(cfg)
79if err != nil {
80log.Fatal("Error encoding config").Err(err).Send()
81}
82
83err = repos.Pull(ctx, cfg.Repos)
84if err != nil {
85log.Fatal("Error pulling repos").Err(err).Send()
86}
87
88return nil
89},
90}
91
92var removerepoCmd = &cli.Command{
93Name: "removerepo",
94Usage: "Remove an existing repository",
95Aliases: []string{"rr"},
96Flags: []cli.Flag{
97&cli.StringFlag{
98Name: "name",
99Aliases: []string{"n"},
100Required: true,
101Usage: "Name of the repo to be deleted",
102},
103},
104Action: func(c *cli.Context) error {
105ctx := c.Context
106log := loggerctx.From(ctx)
107
108name := c.String("name")
109cfg := config.Config(ctx)
110
111found := false
112index := 0
113for i, repo := range cfg.Repos {
114if repo.Name == name {
115index = i
116found = true
117}
118}
119if !found {
120log.Fatal("Repo does not exist").Str("name", name).Send()
121}
122
123cfg.Repos = slices.Delete(cfg.Repos, index, index+1)
124
125cfgFl, err := os.Create(config.GetPaths(ctx).ConfigPath)
126if err != nil {
127log.Fatal("Error opening config file").Err(err).Send()
128}
129
130err = toml.NewEncoder(cfgFl).Encode(&cfg)
131if err != nil {
132log.Fatal("Error encoding config").Err(err).Send()
133}
134
135err = os.RemoveAll(filepath.Join(config.GetPaths(ctx).RepoDir, name))
136if err != nil {
137log.Fatal("Error removing repo directory").Err(err).Send()
138}
139
140err = db.DeletePkgs(ctx, "repository = ?", name)
141if err != nil {
142log.Fatal("Error removing packages from database").Err(err).Send()
143}
144
145return nil
146},
147}
148
149var refreshCmd = &cli.Command{
150Name: "refresh",
151Usage: "Pull all repositories that have changed",
152Aliases: []string{"ref"},
153Action: func(c *cli.Context) error {
154ctx := c.Context
155log := loggerctx.From(ctx)
156err := repos.Pull(ctx, config.Config(ctx).Repos)
157if err != nil {
158log.Fatal("Error pulling repos").Err(err).Send()
159}
160return nil
161},
162}
163