Lock only on commands that aren't meant to run in parallel

This commit is contained in:
Ettore Di Giacinto 2020-03-22 15:19:08 +01:00
parent 7de5f6656d
commit d5166c55ab
No known key found for this signature in database
GPG Key ID: 1ADA699B145A2D1C

View File

@ -35,6 +35,7 @@ import (
var cfgFile string var cfgFile string
var Verbose bool var Verbose bool
var LockedCommands = []string{"install", "uninstall", "upgrade"}
const ( const (
LuetCLIVersion = "0.8-dev" LuetCLIVersion = "0.8-dev"
@ -91,16 +92,21 @@ func LoadConfig(c *config.LuetConfig) error {
// 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() {
// XXX: This is mostly from scratch images.
if os.Getenv("LUET_NOLOCK") != "true" { if os.Getenv("LUET_NOLOCK") != "true" {
s := single.New("luet") for _, lockedCmd := range LockedCommands {
if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning { if os.Args[1] == lockedCmd {
Fatal("another instance of the app is already running, exiting") s := single.New("luet")
} else if err != nil { if err := s.CheckLock(); err != nil && err == single.ErrAlreadyRunning {
// Another error occurred, might be worth handling it as well Fatal("another instance of the app is already running, exiting")
Fatal("failed to acquire exclusive app lock:", err.Error()) } else if err != nil {
// Another error occurred, might be worth handling it as well
Fatal("failed to acquire exclusive app lock:", err.Error())
}
defer s.TryUnlock()
break
}
} }
defer s.TryUnlock()
} }
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {