2021-08-03 14:48:29 +00:00
|
|
|
// Copyright © 2021 Ettore Di Giacinto <mudler@mocaccino.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 util
|
|
|
|
|
|
|
|
import (
|
2021-08-11 09:18:16 +00:00
|
|
|
"errors"
|
2021-10-20 22:13:02 +00:00
|
|
|
"os"
|
2021-08-11 10:52:16 +00:00
|
|
|
"path/filepath"
|
2021-08-11 09:18:16 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
"github.com/marcsauter/single"
|
2021-10-19 20:26:23 +00:00
|
|
|
"github.com/pterm/pterm"
|
2021-08-03 14:48:29 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-10-19 20:26:23 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-08-03 14:48:29 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2021-08-11 10:52:16 +00:00
|
|
|
"github.com/mudler/luet/pkg/installer"
|
2021-08-03 14:48:29 +00:00
|
|
|
)
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
var DefaultContext = types.NewContext()
|
|
|
|
|
|
|
|
var lockedCommands = []string{"install", "uninstall", "upgrade"}
|
|
|
|
var bannerCommands = []string{"install", "build", "uninstall", "upgrade"}
|
|
|
|
|
2021-08-03 14:48:29 +00:00
|
|
|
func BindSystemFlags(cmd *cobra.Command) {
|
2021-10-19 20:26:23 +00:00
|
|
|
viper.BindPFlag("system.database_path", cmd.Flags().Lookup("system-dbpath"))
|
|
|
|
viper.BindPFlag("system.rootfs", cmd.Flags().Lookup("system-target"))
|
|
|
|
viper.BindPFlag("system.database_engine", cmd.Flags().Lookup("system-engine"))
|
2021-08-03 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BindSolverFlags(cmd *cobra.Command) {
|
2021-10-19 20:26:23 +00:00
|
|
|
viper.BindPFlag("solver.type", cmd.Flags().Lookup("solver-type"))
|
|
|
|
viper.BindPFlag("solver.discount", cmd.Flags().Lookup("solver-discount"))
|
|
|
|
viper.BindPFlag("solver.rate", cmd.Flags().Lookup("solver-rate"))
|
|
|
|
viper.BindPFlag("solver.max_attempts", cmd.Flags().Lookup("solver-attempts"))
|
2021-08-03 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 10:57:36 +00:00
|
|
|
func BindValuesFlags(cmd *cobra.Command) {
|
2021-10-19 20:26:23 +00:00
|
|
|
viper.BindPFlag("values", cmd.Flags().Lookup("values"))
|
2021-08-09 10:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ValuesFlags() []string {
|
2021-10-19 20:26:23 +00:00
|
|
|
return viper.GetStringSlice("values")
|
2021-08-09 10:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func SetSystemConfig(ctx *types.Context) {
|
2021-10-19 20:26:23 +00:00
|
|
|
dbpath := viper.GetString("system.database_path")
|
|
|
|
rootfs := viper.GetString("system.rootfs")
|
|
|
|
engine := viper.GetString("system.database_engine")
|
2021-08-03 14:48:29 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Config.System.DatabaseEngine = engine
|
|
|
|
ctx.Config.System.DatabasePath = dbpath
|
|
|
|
ctx.Config.System.SetRootFS(rootfs)
|
2021-08-03 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func SetSolverConfig(ctx *types.Context) (c *types.LuetSolverOptions) {
|
2021-10-19 20:26:23 +00:00
|
|
|
stype := viper.GetString("solver.type")
|
|
|
|
discount := viper.GetFloat64("solver.discount")
|
|
|
|
rate := viper.GetFloat64("solver.rate")
|
|
|
|
attempts := viper.GetInt("solver.max_attempts")
|
2021-08-03 14:48:29 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Config.GetSolverOptions().Type = stype
|
|
|
|
ctx.Config.GetSolverOptions().LearnRate = float32(rate)
|
|
|
|
ctx.Config.GetSolverOptions().Discount = float32(discount)
|
|
|
|
ctx.Config.GetSolverOptions().MaxAttempts = attempts
|
2021-08-03 14:48:29 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
return &types.LuetSolverOptions{
|
2021-08-03 14:48:29 +00:00
|
|
|
Type: stype,
|
|
|
|
LearnRate: float32(rate),
|
|
|
|
Discount: float32(discount),
|
|
|
|
MaxAttempts: attempts,
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 09:18:16 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func SetCliFinalizerEnvs(ctx *types.Context, finalizerEnvs []string) error {
|
2021-08-11 09:18:16 +00:00
|
|
|
if len(finalizerEnvs) > 0 {
|
|
|
|
for _, v := range finalizerEnvs {
|
|
|
|
idx := strings.Index(v, "=")
|
|
|
|
if idx < 0 {
|
|
|
|
return errors.New("Found invalid runtime finalizer environment: " + v)
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Config.SetFinalizerEnv(v[0:idx], v[idx+1:])
|
2021-08-11 09:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-11 10:52:16 +00:00
|
|
|
|
|
|
|
// TemplateFolders returns the default folders which holds shared template between packages in a given tree path
|
2021-10-20 22:13:02 +00:00
|
|
|
func TemplateFolders(ctx *types.Context, fromRepo bool, treePaths []string) []string {
|
2021-08-11 10:52:16 +00:00
|
|
|
templateFolders := []string{}
|
2021-10-06 09:08:53 +00:00
|
|
|
for _, t := range treePaths {
|
|
|
|
templateFolders = append(templateFolders, filepath.Join(t, "templates"))
|
|
|
|
}
|
|
|
|
if fromRepo {
|
2021-10-20 22:13:02 +00:00
|
|
|
for _, s := range installer.SystemRepositories(ctx.Config.SystemRepositories) {
|
2021-08-11 10:52:16 +00:00
|
|
|
templateFolders = append(templateFolders, filepath.Join(s.TreePath, "templates"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return templateFolders
|
|
|
|
}
|
2021-10-19 20:26:23 +00:00
|
|
|
|
|
|
|
func IntroScreen() {
|
|
|
|
luetLogo, _ := pterm.DefaultBigText.WithLetters(
|
|
|
|
pterm.NewLettersFromStringWithStyle("LU", pterm.NewStyle(pterm.FgLightMagenta)),
|
|
|
|
pterm.NewLettersFromStringWithStyle("ET", pterm.NewStyle(pterm.FgLightBlue))).
|
|
|
|
Srender()
|
|
|
|
|
|
|
|
pterm.DefaultCenter.Print(luetLogo)
|
|
|
|
|
|
|
|
pterm.DefaultCenter.Print(pterm.DefaultHeader.WithFullWidth().WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(10).Sprint("Luet - 0-deps container-based package manager"))
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
|
|
|
|
func HandleLock(c *types.Context) {
|
|
|
|
if os.Getenv("LUET_NOLOCK") != "true" {
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
for _, lockedCmd := range lockedCommands {
|
|
|
|
if os.Args[1] == lockedCmd {
|
|
|
|
s := single.New("luet")
|
|
|
|
if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
|
|
|
|
c.Fatal("another instance of the app is already running, exiting")
|
|
|
|
} else if err != nil {
|
|
|
|
// Another error occurred, might be worth handling it as well
|
|
|
|
c.Fatal("failed to acquire exclusive app lock:", err.Error())
|
|
|
|
}
|
|
|
|
defer s.TryUnlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DisplayVersionBanner(c *types.Context, banner func(), version func() string, license []string) {
|
|
|
|
display := false
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
for _, c := range bannerCommands {
|
|
|
|
if os.Args[1] == c {
|
|
|
|
display = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if display {
|
2021-12-04 20:35:34 +00:00
|
|
|
if c.Config.General.Quiet {
|
|
|
|
pterm.Info.Printf("Luet %s\n", version())
|
|
|
|
pterm.Info.Println(strings.Join(license, "\n"))
|
|
|
|
} else {
|
|
|
|
banner()
|
|
|
|
pterm.DefaultCenter.Print(version())
|
|
|
|
for _, l := range license {
|
|
|
|
pterm.DefaultCenter.Print(l)
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|