2020-10-10 16:26:37 +00:00
|
|
|
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
//
|
|
|
|
// 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 2 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/>.
|
|
|
|
|
|
|
|
package cmd_database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
2021-08-03 14:48:29 +00:00
|
|
|
"github.com/mudler/luet/cmd/util"
|
2022-01-06 22:57:56 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2021-10-19 15:06:48 +00:00
|
|
|
artifact "github.com/mudler/luet/pkg/api/core/types/artifact"
|
2021-04-12 17:00:36 +00:00
|
|
|
|
2020-10-10 16:26:37 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewDatabaseCreateCommand() *cobra.Command {
|
2021-12-17 14:21:03 +00:00
|
|
|
return &cobra.Command{
|
2020-10-10 16:26:37 +00:00
|
|
|
Use: "create <artifact_metadata1.yaml> <artifact_metadata1.yaml>",
|
|
|
|
Short: "Insert a package in the system DB",
|
2020-12-02 22:15:23 +00:00
|
|
|
Long: `Inserts a package in the system database:
|
|
|
|
|
|
|
|
$ luet database create foo.yaml
|
|
|
|
|
|
|
|
"luet database create" injects a package in the system database without actually installing it, use it with caution.
|
|
|
|
|
|
|
|
This commands takes multiple yaml input file representing package artifacts, that are usually generated while building packages.
|
|
|
|
|
|
|
|
The yaml must contain the package definition, and the file list at least.
|
|
|
|
|
|
|
|
For reference, inspect a "metadata.yaml" file generated while running "luet build"`,
|
|
|
|
Args: cobra.OnlyValidArgs,
|
2020-10-10 16:26:37 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
2022-01-06 22:57:56 +00:00
|
|
|
systemDB := util.SystemDB(util.DefaultContext.Config)
|
2020-10-10 16:26:37 +00:00
|
|
|
|
|
|
|
for _, a := range args {
|
|
|
|
dat, err := ioutil.ReadFile(a)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Failed reading ", a, ": ", err.Error())
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
2021-04-12 17:00:36 +00:00
|
|
|
art, err := artifact.NewPackageArtifactFromYaml(dat)
|
2020-10-10 16:26:37 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Failed reading yaml ", a, ": ", err.Error())
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
files := art.Files
|
2020-10-10 16:26:37 +00:00
|
|
|
|
2021-06-25 11:09:32 +00:00
|
|
|
// Check if the package is already present
|
|
|
|
if p, err := systemDB.FindPackage(art.CompileSpec.GetPackage()); err == nil && p.GetName() != "" {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Package", art.CompileSpec.GetPackage().HumanReadableString(),
|
2021-06-25 11:09:32 +00:00
|
|
|
" already present.")
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
if _, err := systemDB.CreatePackage(art.CompileSpec.GetPackage()); err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Failed to create ", a, ": ", err.Error())
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
2022-01-06 22:57:56 +00:00
|
|
|
if err := systemDB.SetPackageFiles(&types.PackageFile{PackageFingerprint: art.CompileSpec.GetPackage().GetFingerPrint(), Files: files}); err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Failed setting package files for ", a, ": ", err.Error())
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Info(art.CompileSpec.GetPackage().HumanReadableString(), " created")
|
2020-10-10 16:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|