lure/install.go

123 lines
3.1 KiB
Go
Raw Permalink Normal View History

2022-09-26 19:28:21 +00:00
/*
* LURE - Linux User REpository
2023-09-20 22:38:22 +00:00
* Copyright (C) 2023 Elara Musayelyan
2022-09-26 19:28:21 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-09-25 21:00:15 +00:00
package main
import (
2023-09-19 21:28:05 +00:00
"fmt"
2022-09-25 21:00:15 +00:00
"github.com/urfave/cli/v2"
2023-10-08 00:34:39 +00:00
"lure.sh/lure/internal/cliutils"
"lure.sh/lure/internal/config"
"lure.sh/lure/internal/db"
"lure.sh/lure/internal/types"
"lure.sh/lure/pkg/build"
"lure.sh/lure/pkg/loggerctx"
"lure.sh/lure/pkg/manager"
"lure.sh/lure/pkg/repos"
2022-09-25 21:00:15 +00:00
)
2023-09-19 21:28:05 +00:00
var installCmd = &cli.Command{
Name: "install",
Usage: "Install a new package",
Aliases: []string{"in"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clean",
Aliases: []string{"c"},
Usage: "Build package from scratch even if there's an already built package available",
},
},
Action: func(c *cli.Context) error {
2023-10-06 21:21:12 +00:00
ctx := c.Context
log := loggerctx.From(ctx)
2023-09-19 21:28:05 +00:00
args := c.Args()
if args.Len() < 1 {
log.Fatalf("Command install expected at least 1 argument, got %d", args.Len()).Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
mgr := manager.Detect()
if mgr == nil {
log.Fatal("Unable to detect a supported package manager on the system").Send()
}
2022-09-25 21:00:15 +00:00
2023-10-06 21:21:12 +00:00
err := repos.Pull(ctx, config.Config(ctx).Repos)
2022-09-25 21:00:15 +00:00
if err != nil {
2023-09-19 21:28:05 +00:00
log.Fatal("Error pulling repositories").Err(err).Send()
2022-09-25 21:00:15 +00:00
}
2023-10-06 21:21:12 +00:00
found, notFound, err := repos.FindPkgs(ctx, args.Slice())
2022-09-25 21:00:15 +00:00
if err != nil {
2023-09-19 21:28:05 +00:00
log.Fatal("Error finding packages").Err(err).Send()
2022-09-25 21:00:15 +00:00
}
2023-10-06 21:21:12 +00:00
pkgs := cliutils.FlattenPkgs(ctx, found, "install", c.Bool("interactive"))
build.InstallPkgs(ctx, pkgs, notFound, types.BuildOpts{
2023-09-19 21:28:05 +00:00
Manager: mgr,
Clean: c.Bool("clean"),
Interactive: c.Bool("interactive"),
})
return nil
},
BashComplete: func(c *cli.Context) {
2023-10-06 21:21:12 +00:00
log := loggerctx.From(c.Context)
result, err := db.GetPkgs(c.Context, "true")
2022-09-25 21:00:15 +00:00
if err != nil {
2023-09-19 21:28:05 +00:00
log.Fatal("Error getting packages").Err(err).Send()
}
defer result.Close()
for result.Next() {
var pkg db.Package
err = result.StructScan(&pkg)
if err != nil {
log.Fatal("Error iterating over packages").Err(err).Send()
}
fmt.Println(pkg.Name)
2022-09-25 21:00:15 +00:00
}
2023-09-19 21:28:05 +00:00
},
2022-09-25 21:00:15 +00:00
}
2023-09-19 21:28:05 +00:00
var removeCmd = &cli.Command{
Name: "remove",
Usage: "Remove an installed package",
Aliases: []string{"rm"},
Action: func(c *cli.Context) error {
2023-10-06 21:21:12 +00:00
log := loggerctx.From(c.Context)
2023-09-19 21:28:05 +00:00
args := c.Args()
if args.Len() < 1 {
log.Fatalf("Command remove expected at least 1 argument, got %d", args.Len()).Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
mgr := manager.Detect()
if mgr == nil {
log.Fatal("Unable to detect a supported package manager on the system").Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
err := mgr.Remove(nil, c.Args().Slice()...)
if err != nil {
log.Fatal("Error removing packages").Err(err).Send()
}
2022-09-25 21:00:15 +00:00
2023-09-19 21:28:05 +00:00
return nil
},
2022-09-25 21:00:15 +00:00
}