From 8ca6051a049b9c77c15df29bdab49cd2d6994acf Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Tue, 29 Oct 2019 17:36:48 +0100 Subject: [PATCH] Add cmd/ and main.go --- cmd/import.go | 41 ++++++++++++++++++++++++ cmd/root.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 11 +++++++ 3 files changed, 139 insertions(+) create mode 100644 cmd/import.go create mode 100644 cmd/root.go create mode 100644 main.go diff --git a/cmd/import.go b/cmd/import.go new file mode 100644 index 00000000..bdbebb79 --- /dev/null +++ b/cmd/import.go @@ -0,0 +1,41 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// 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 . +package cmd + +import ( + "log" + + "github.com/spf13/cobra" +) + +var importCmd = &cobra.Command{ + Use: "import", + Short: "imports other package manager tree into luet", + Long: `Parses external PM and produces a luet parsable tree`, + Run: func(cmd *cobra.Command, args []string) { + //Output := viper.GetString("output") + + if len(args) == 0 { + log.Fatalln("Insufficient arguments") + } + + //input := args[0] + + }, +} + +func init() { + RootCmd.AddCommand(importCmd) +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 00000000..53e9ef45 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,87 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// 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 . + +package cmd + +import ( + "fmt" + "log" + "os" + "path" + "path/filepath" + + "github.com/fsnotify/fsnotify" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var cfgFile string +var Verbose bool + +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ + Use: "luet", + Short: "Package manager for the XXth century!", + Long: `Package manager which supports Gentoo and Entropy packages`, +} + +// 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. +func Execute() { + if err := RootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(-1) + } +} + +func init() { + cobra.OnInitialize(initConfig) + RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luet.yaml)") + RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") +} + +// initConfig reads in config file and ENV variables if set. +func initConfig() { + dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + log.Fatal(err) + } + + viper.SetConfigName(".luet") // name of config file (without extension) + if cfgFile != "" { // enable ability to specify config file via flag + fmt.Println(">>> cfgFile: ", cfgFile) + viper.SetConfigFile(cfgFile) + configDir := path.Dir(cfgFile) + if configDir != "." && configDir != dir { + viper.AddConfigPath(configDir) + } + } + + viper.AddConfigPath(dir) + viper.AddConfigPath(".") + viper.AddConfigPath("$HOME") + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + fmt.Println("Using config file:", viper.ConfigFileUsed()) + } else { + fmt.Println(err) + } + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + fmt.Println("Config file changed:", e.Name) + }) +} diff --git a/main.go b/main.go new file mode 100644 index 00000000..0e74155b --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +package main + +import "github.com/mudler/luet/cmd" + +var ( + version string +) + +func main() { + cmd.Execute() +}