add set-ref command and refactor tests

This commit is contained in:
2025-05-16 20:48:14 +03:00
parent eb2356458c
commit f92bd7089a
18 changed files with 207 additions and 274 deletions

49
repo.go
View File

@ -40,6 +40,7 @@ func RepoCmd() *cli.Command {
Subcommands: []*cli.Command{
RemoveRepoCmd(),
AddRepoCmd(),
SetRepoRefCmd(),
},
}
}
@ -180,6 +181,54 @@ func AddRepoCmd() *cli.Command {
}
}
func SetRepoRefCmd() *cli.Command {
return &cli.Command{
Name: "set-ref",
Usage: gotext.Get("Set the reference of the repository"),
ArgsUsage: gotext.Get("<name> <ref>"),
Action: utils.RootNeededAction(func(c *cli.Context) error {
if c.Args().Len() < 2 {
return cliutils.FormatCliExit("missing args", nil)
}
name := c.Args().Get(0)
ref := c.Args().Get(1)
deps, err := appbuilder.
New(c.Context).
WithConfig().
WithDB().
WithReposNoPull().
Build()
if err != nil {
return err
}
defer deps.Defer()
repos := deps.Cfg.Repos()
newRepos := []types.Repo{}
for _, repo := range repos {
if repo.Name == name {
repo.Ref = ref
}
newRepos = append(newRepos, repo)
}
deps.Cfg.SetRepos(newRepos)
err = deps.Cfg.SaveUserConfig()
if err != nil {
return cliutils.FormatCliExit(gotext.Get("Error saving config"), err)
}
err = deps.Repos.Pull(c.Context, newRepos)
if err != nil {
return cliutils.FormatCliExit(gotext.Get("Error pulling repositories"), err)
}
return nil
}),
}
}
// TODO: remove
//
// Deprecated: use "alr repo add"