🎨 CLI UX enhancements

This commit is contained in:
Ettore Di Giacinto
2022-04-22 11:41:02 +02:00
parent 6674abb256
commit 388a3e4471
3 changed files with 31 additions and 41 deletions

View File

@@ -35,9 +35,10 @@ const (
) )
var license = []string{ var license = []string{
"Luet Copyright (C) 2019-2022 Ettore Di Giacinto", "Copyright (C) 2019-2022 Ettore Di Giacinto",
"This program comes with ABSOLUTELY NO WARRANTY.", "This program comes with ABSOLUTELY NO WARRANTY.",
"This is free software, and you are welcome to redistribute it under certain conditions.", "This is free software, and you are welcome to redistribute it under certain conditions.",
"For documentation, visit https://luet.io.",
} }
// Build time and commit information. // Build time and commit information.
@@ -56,7 +57,9 @@ func version() string {
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "luet", Use: "luet",
Short: "Container based package manager", Short: "Container based package manager",
Long: `Luet is a single-binary package manager based on containers to build packages. Long: `Luet is a single-binary package manager based on containers to build packages.
For documentation, visit https://luet.io.
To install a package: To install a package:
@@ -89,7 +92,7 @@ To build a package, from a tree definition:
util.DefaultContext = ctx util.DefaultContext = ctx
util.DisplayVersionBanner(util.DefaultContext, util.IntroScreen, version, license) util.DisplayVersionBanner(util.DefaultContext, version, license)
viper.BindPFlag("plugin", cmd.Flags().Lookup("plugin")) viper.BindPFlag("plugin", cmd.Flags().Lookup("plugin"))
@@ -116,7 +119,7 @@ To build a package, from a tree definition:
// Execute adds all child commands to the root command sets flags appropriately. // Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
util.HandleLock(util.DefaultContext) util.HandleLock()
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)

View File

@@ -16,6 +16,7 @@
package util package util
import ( import (
"fmt"
"os" "os"
"strings" "strings"
@@ -26,7 +27,6 @@ import (
"github.com/mudler/luet/pkg/api/core/context" "github.com/mudler/luet/pkg/api/core/context"
"github.com/mudler/luet/pkg/api/core/template" "github.com/mudler/luet/pkg/api/core/template"
"github.com/mudler/luet/pkg/api/core/types"
"github.com/mudler/luet/pkg/installer" "github.com/mudler/luet/pkg/installer"
) )
@@ -54,38 +54,33 @@ func TemplateFolders(ctx *context.Context, i installer.BuildTreeResult, treePath
return templateFolders return templateFolders
} }
func IntroScreen() { func HandleLock() {
luetLogo, _ := pterm.DefaultBigText.WithLetters( if os.Getenv("LUET_NOLOCK") == "true" {
pterm.NewLettersFromStringWithStyle("LU", pterm.NewStyle(pterm.FgLightMagenta)), return
pterm.NewLettersFromStringWithStyle("ET", pterm.NewStyle(pterm.FgLightBlue))). }
Srender()
pterm.DefaultCenter.Print(luetLogo) if len(os.Args) == 0 {
return
}
pterm.DefaultCenter.Print(pterm.DefaultHeader.WithFullWidth().WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(10).Sprint("Luet - 0-deps container-based package manager")) for _, lockedCmd := range lockedCommands {
} if os.Args[1] == lockedCmd {
s := single.New("luet")
func HandleLock(c types.Context) { if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
if os.Getenv("LUET_NOLOCK") != "true" { fmt.Println("another instance of the app is already running, exiting")
if len(os.Args) > 1 { os.Exit(1)
for _, lockedCmd := range lockedCommands { } else if err != nil {
if os.Args[1] == lockedCmd { // Another error occurred, might be worth handling it as well
s := single.New("luet") fmt.Println("failed to acquire exclusive app lock:", err.Error())
if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning { os.Exit(1)
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
}
} }
defer s.TryUnlock()
break
} }
} }
} }
func DisplayVersionBanner(c *context.Context, banner func(), version func() string, license []string) { func DisplayVersionBanner(c *context.Context, version func() string, license []string) {
display := false display := false
if len(os.Args) > 1 { if len(os.Args) > 1 {
for _, c := range bannerCommands { for _, c := range bannerCommands {
@@ -95,15 +90,7 @@ func DisplayVersionBanner(c *context.Context, banner func(), version func() stri
} }
} }
if display { if display {
if c.Config.General.Quiet { pterm.Info.Printf("Luet %s\n", version())
pterm.Info.Printf("Luet %s\n", version()) pterm.Info.Println(strings.Join(license, "\n"))
pterm.Info.Println(strings.Join(license, "\n"))
} else {
banner()
pterm.DefaultCenter.Print(version())
for _, l := range license {
pterm.DefaultCenter.Print(l)
}
}
} }
} }

View File

@@ -334,7 +334,7 @@ func (l *Logger) Spinner() {
} }
func (l *Logger) Screen(text string) { func (l *Logger) Screen(text string) {
pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(2).Println(text) l.Infof(":::> %s", text)
} }
func (l *Logger) SpinnerText(suffix, prefix string) { func (l *Logger) SpinnerText(suffix, prefix string) {