luet/cmd/tree/bump.go

89 lines
2.8 KiB
Go
Raw Normal View History

2020-03-16 07:33:51 +00:00
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
// Daniele Rondina <geaaru@sabayonlinux.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_tree
import (
"fmt"
"github.com/mudler/luet/cmd/util"
spectooling "github.com/mudler/luet/pkg/spectooling"
2020-03-16 07:33:51 +00:00
tree "github.com/mudler/luet/pkg/tree"
version "github.com/mudler/luet/pkg/versioner"
2020-03-16 07:33:51 +00:00
"github.com/spf13/cobra"
)
func NewTreeBumpCommand() *cobra.Command {
var ans = &cobra.Command{
Use: "bump [OPTIONS]",
Short: "Bump a new package build version.",
Args: cobra.OnlyValidArgs,
PreRun: func(cmd *cobra.Command, args []string) {
df, _ := cmd.Flags().GetString("definition-file")
if df == "" {
util.DefaultContext.Fatal("Mandatory definition.yaml path missing.")
2020-03-16 07:33:51 +00:00
}
},
Run: func(cmd *cobra.Command, args []string) {
spec, _ := cmd.Flags().GetString("definition-file")
2020-04-04 11:42:25 +00:00
toStdout, _ := cmd.Flags().GetBool("to-stdout")
pkgVersion, _ := cmd.Flags().GetString("pkg-version")
2020-03-16 07:33:51 +00:00
pack, err := tree.ReadDefinitionFile(spec)
if err != nil {
util.DefaultContext.Fatal(err.Error())
2020-03-16 07:33:51 +00:00
}
if pkgVersion != "" {
validator := &version.WrappedVersioner{}
err := validator.Validate(pkgVersion)
if err != nil {
util.DefaultContext.Fatal("Invalid version string: " + err.Error())
}
pack.SetVersion(pkgVersion)
} else {
// Retrieve version build section with Gentoo parser
err = pack.BumpBuildVersion()
if err != nil {
util.DefaultContext.Fatal("Error on increment build version: " + err.Error())
}
2020-03-16 07:33:51 +00:00
}
2020-04-04 11:42:25 +00:00
if toStdout {
data, err := spectooling.NewDefaultPackageSanitized(&pack).Yaml()
2020-04-04 11:42:25 +00:00
if err != nil {
util.DefaultContext.Fatal("Error on yaml conversion: " + err.Error())
2020-04-04 11:42:25 +00:00
}
fmt.Println(string(data))
} else {
2020-03-16 07:33:51 +00:00
2020-04-04 11:42:25 +00:00
err = tree.WriteDefinitionFile(&pack, spec)
if err != nil {
util.DefaultContext.Fatal("Error on write definition file: " + err.Error())
2020-04-04 11:42:25 +00:00
}
fmt.Printf("Bumped package %s/%s-%s.\n", pack.Category, pack.Name, pack.Version)
}
2020-03-16 07:33:51 +00:00
},
}
ans.Flags().StringP("pkg-version", "p", "", "Set a specific package version")
2020-03-16 07:33:51 +00:00
ans.Flags().StringP("definition-file", "f", "", "Path of the definition to bump.")
2020-04-04 11:42:25 +00:00
ans.Flags().BoolP("to-stdout", "o", false, "Bump package to output.")
2020-03-16 07:33:51 +00:00
return ans
}