all repos

mugit @ 5935eaa

🐮 git server that your cow will love
3 files changed, 115 insertions(+), 49 deletions(-)
cli: repo description
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-02-03 00:54:39 +0200
Change ID: qszlnmvzvqvsxnqrnpuvzuoskmpuortr
Parent: 8417128
M internal/cli/cli.go

@@ -2,19 +2,12 @@ package cli

import ( "context" - "log/slog" - "net" - "net/http" - "os" - "os/signal" - "strconv" - "syscall" + "fmt" + securejoin "github.com/cyphar/filepath-securejoin" "github.com/urfave/cli/v3" "olexsmir.xyz/mugit/internal/config" - "olexsmir.xyz/mugit/internal/handlers" - "olexsmir.xyz/mugit/internal/mirror" - "olexsmir.xyz/mugit/internal/ssh" + "olexsmir.xyz/mugit/internal/git" ) type Cli struct {

@@ -68,6 +61,14 @@ Usage: "remote URL(only http/https) to mirror from",

}, }, }, + { + Name: "description", + Usage: "get or set repo description", + Action: c.repoDescriptionAction, + Arguments: []cli.Argument{ + &cli.StringArg{Name: "name"}, + }, + }, }, }, },

@@ -75,47 +76,16 @@ }

return cmd.Run(ctx, args) } -func (c *Cli) serveAction(ctx context.Context, cmd *cli.Command) error { - httpServer := &http.Server{ - Addr: net.JoinHostPort(c.cfg.Server.Host, strconv.Itoa(c.cfg.Server.Port)), - Handler: handlers.InitRoutes(c.cfg), - } - go func() { - slog.Info("starting http server", "host", c.cfg.Server.Host, "port", c.cfg.Server.Port) - if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - slog.Error("HTTP server error", "err", err) - } - }() - - if c.cfg.SSH.Enable { - sshServer := ssh.NewServer(c.cfg) - go func() { - slog.Info("starting ssh server", "port", c.cfg.SSH.Port) - if err := sshServer.Start(); err != nil { - slog.Error("ssh server error", "err", err) - } - }() - } - - if c.cfg.Mirror.Enable { - mirrorer := mirror.NewWorker(c.cfg) - go func() { - slog.Info("starting mirroring worker") - mirrorer.Start(context.TODO()) - }() +func (c *Cli) openRepo(name string) (*git.Repo, error) { + path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name) + if err != nil { + return nil, err } - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - - sig := <-sigChan - slog.Info("received signal, starting graceful shutdown", "signal", sig) - - if err := httpServer.Shutdown(context.TODO()); err != nil { - slog.Error("HTTP server shutdown error", "err", err) - } else { - slog.Info("HTTP server shutdown complete") + repo, err := git.Open(path, "") + if err != nil { + return nil, fmt.Errorf("failed to open repo: %w", err) } - return nil + return repo, nil }
M internal/cli/repo.go

@@ -48,3 +48,37 @@ }

return nil } + +func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error { + name := cmd.StringArg("name") + if name == "" { + return fmt.Errorf("no name provided") + } + + name = strings.TrimRight(name, ".git") + ".git" + + repo, err := c.openRepo(name) + if err != nil { + return fmt.Errorf("failed to open repo: %w", err) + } + + newDesc := cmd.Args().Get(0) + if newDesc != "" { + if err := repo.SetDescription(newDesc); err != nil { + return fmt.Errorf("failed to set description: %w", err) + } + } + + desc, err := repo.Description() + if err != nil { + return fmt.Errorf("failed to get description: %w", err) + } + + if desc == "" { + fmt.Println("No description set") + } else { + fmt.Println(desc) + } + + return nil +}
A internal/cli/serve.go

@@ -0,0 +1,62 @@

+package cli + +import ( + "context" + "log/slog" + "net" + "net/http" + "os" + "os/signal" + "strconv" + "syscall" + + "github.com/urfave/cli/v3" + "olexsmir.xyz/mugit/internal/handlers" + "olexsmir.xyz/mugit/internal/mirror" + "olexsmir.xyz/mugit/internal/ssh" +) + +func (c *Cli) serveAction(ctx context.Context, cmd *cli.Command) error { + httpServer := &http.Server{ + Addr: net.JoinHostPort(c.cfg.Server.Host, strconv.Itoa(c.cfg.Server.Port)), + Handler: handlers.InitRoutes(c.cfg), + } + go func() { + slog.Info("starting http server", "host", c.cfg.Server.Host, "port", c.cfg.Server.Port) + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + slog.Error("HTTP server error", "err", err) + } + }() + + if c.cfg.SSH.Enable { + sshServer := ssh.NewServer(c.cfg) + go func() { + slog.Info("starting ssh server", "port", c.cfg.SSH.Port) + if err := sshServer.Start(); err != nil { + slog.Error("ssh server error", "err", err) + } + }() + } + + if c.cfg.Mirror.Enable { + mirrorer := mirror.NewWorker(c.cfg) + go func() { + slog.Info("starting mirroring worker") + mirrorer.Start(context.TODO()) + }() + } + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + + sig := <-sigChan + slog.Info("received signal, starting graceful shutdown", "signal", sig) + + if err := httpServer.Shutdown(context.TODO()); err != nil { + slog.Error("HTTP server shutdown error", "err", err) + } else { + slog.Info("HTTP server shutdown complete") + } + + return nil +}