matrix-dsl

A simple DSL for fast generation of Matrix Bots or Application Services
git clone git://archive.git.mtrnord.blog/Nordgedanken/matrix-dsl.git
Log | Files | Refs | LICENSE

root.go (2804B)


      1 // Copyright © 2018 NAME HERE <EMAIL ADDRESS>
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package cmd
     16 
     17 import (
     18 	"fmt"
     19 	"os"
     20 
     21 	homedir "github.com/mitchellh/go-homedir"
     22 	"github.com/spf13/cobra"
     23 	"github.com/spf13/viper"
     24 )
     25 
     26 var cfgFile string
     27 
     28 // rootCmd represents the base command when called without any subcommands
     29 var rootCmd = &cobra.Command{
     30 	Use:   "matrix_dsl",
     31 	Short: "A brief description of your application",
     32 	Long: `A longer description that spans multiple lines and likely contains
     33 examples and usage of using your application. For example:
     34 
     35 Cobra is a CLI library for Go that empowers applications.
     36 This application is a tool to generate the needed files
     37 to quickly create a Cobra application.`,
     38 	// Uncomment the following line if your bare application
     39 	// has an action associated with it:
     40 	//	Run: func(cmd *cobra.Command, args []string) { },
     41 }
     42 
     43 // Execute adds all child commands to the root command and sets flags appropriately.
     44 // This is called by main.main(). It only needs to happen once to the rootCmd.
     45 func Execute() {
     46 	if err := rootCmd.Execute(); err != nil {
     47 		fmt.Println(err)
     48 		os.Exit(1)
     49 	}
     50 }
     51 
     52 func init() {
     53 	cobra.OnInitialize(initConfig)
     54 
     55 	// Here you will define your flags and configuration settings.
     56 	// Cobra supports persistent flags, which, if defined here,
     57 	// will be global for your application.
     58 	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.matrix_dsl.yaml)")
     59 
     60 	// Cobra also supports local flags, which will only run
     61 	// when this action is called directly.
     62 	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
     63 }
     64 
     65 // initConfig reads in config file and ENV variables if set.
     66 func initConfig() {
     67 	if cfgFile != "" {
     68 		// Use config file from the flag.
     69 		viper.SetConfigFile(cfgFile)
     70 	} else {
     71 		// Find home directory.
     72 		home, err := homedir.Dir()
     73 		if err != nil {
     74 			fmt.Println(err)
     75 			os.Exit(1)
     76 		}
     77 
     78 		// Search config in home directory with name ".matrix_dsl" (without extension).
     79 		viper.AddConfigPath(home)
     80 		viper.SetConfigName(".matrix_dsl")
     81 	}
     82 
     83 	viper.AutomaticEnv() // read in environment variables that match
     84 
     85 	// If a config file is found, read it in.
     86 	if err := viper.ReadInConfig(); err == nil {
     87 		fmt.Println("Using config file:", viper.ConfigFileUsed())
     88 	}
     89 }