added cobra commands

This commit is contained in:
2022-04-08 16:12:35 +02:00
parent 4a60c5f179
commit e3904ae21c
7 changed files with 437 additions and 0 deletions

30
cmd/install.go Normal file
View File

@ -0,0 +1,30 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Short: "installs the database config files for postfix",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("install called")
},
}
func init() {
rootCmd.AddCommand(installCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// installCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// installCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

70
cmd/root.go Normal file
View File

@ -0,0 +1,70 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "manager",
Short: "postfix manager is a simple management UI for postfix domains, mailboxes & transports",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.manager.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".manager" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".manager")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}

29
cmd/ui.go Normal file
View File

@ -0,0 +1,29 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// uiCmd represents the ui command
var uiCmd = &cobra.Command{
Use: "ui",
Short: "serves the management ui over http",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ui called")
},
}
func init() {
rootCmd.AddCommand(uiCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// uiCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// uiCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}