|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "log" |
| 4 | + "fmt" |
| 5 | + "os" |
5 | 6 |
|
6 | | - "github.com/larkinwc/proxmox-lxc-compose/internal/cli" |
| 7 | + "github.com/larkinwc/proxmox-lxc-compose/pkg/logging" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/viper" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + cfgFile string |
| 15 | + debugMode bool |
| 16 | + development bool |
7 | 17 | ) |
8 | 18 |
|
| 19 | +func init() { |
| 20 | + cobra.OnInitialize(initConfig) |
| 21 | + |
| 22 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.lxc-compose.yaml)") |
| 23 | + rootCmd.PersistentFlags().BoolVar(&debugMode, "debug", false, "enable debug logging") |
| 24 | + rootCmd.PersistentFlags().BoolVar(&development, "dev", false, "enable development mode") |
| 25 | +} |
| 26 | + |
| 27 | +func initConfig() { |
| 28 | + // Initialize logging first |
| 29 | + logLevel := "info" |
| 30 | + if debugMode { |
| 31 | + logLevel = "debug" |
| 32 | + } |
| 33 | + |
| 34 | + if err := logging.Init(logging.Config{ |
| 35 | + Level: logLevel, |
| 36 | + Development: development, |
| 37 | + }); err != nil { |
| 38 | + fmt.Printf("Error initializing logger: %v\n", err) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + // Load configuration file if specified |
| 43 | + if cfgFile != "" { |
| 44 | + viper.SetConfigFile(cfgFile) |
| 45 | + } else { |
| 46 | + home, err := os.UserHomeDir() |
| 47 | + cobra.CheckErr(err) |
| 48 | + |
| 49 | + viper.AddConfigPath(home) |
| 50 | + viper.SetConfigType("yaml") |
| 51 | + viper.SetConfigName(".lxc-compose") |
| 52 | + } |
| 53 | + |
| 54 | + if err := viper.ReadInConfig(); err == nil { |
| 55 | + logging.Info("Using config file", "path", viper.ConfigFileUsed()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +var rootCmd = &cobra.Command{ |
| 60 | + Use: "lxc-compose", |
| 61 | + Short: "Manage LXC containers using docker-compose like syntax", |
| 62 | + Long: `lxc-compose is a CLI tool that allows you to manage LXC containers |
| 63 | +using a docker-compose like syntax. It supports creating, starting, stopping, |
| 64 | +and managing containers defined in a YAML configuration file.`, |
| 65 | +} |
| 66 | + |
9 | 67 | func main() { |
10 | | - if err := cli.Execute(); err != nil { |
11 | | - log.Fatal(err) |
| 68 | + if err := rootCmd.Execute(); err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + os.Exit(1) |
12 | 71 | } |
13 | 72 | } |
0 commit comments