50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/warkanum/unitdore/config"
|
|
"github.com/warkanum/unitdore/sysuser"
|
|
)
|
|
|
|
var version = "0.0.11"
|
|
|
|
var configPath string
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "unitdore",
|
|
Short: "A door you open and close for container units",
|
|
Long: `Unitdore manages container units via systemd.
|
|
|
|
It discovers running containers and podman-compose stacks, stores them in a
|
|
config file, and generates + manages systemd .service units for each one.
|
|
|
|
As root: system units, plus user units for users listed in 'users:'.
|
|
As a normal user: own config and user units only.`,
|
|
Version: version,
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
// Normal users get their own config and manage only their user units
|
|
if !cmd.Flags().Changed("config") && !sysuser.IsRoot() {
|
|
home, err := sysuser.HomeDir(sysuser.Current())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
configPath = config.UserConfigPath(home)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.PersistentFlags().StringVar(&configPath, "config", config.DefaultConfigPath, "path to units config file (default for non-root: ~/.config/unitdore/units.yaml)")
|
|
}
|