0
|
1 /*
|
|
2 Copyright © 2020 NAME HERE <EMAIL ADDRESS>
|
|
3
|
|
4 Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 you may not use this file except in compliance with the License.
|
|
6 You may obtain a copy of the License at
|
|
7
|
|
8 http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
|
10 Unless required by applicable law or agreed to in writing, software
|
|
11 distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 See the License for the specific language governing permissions and
|
|
14 limitations under the License.
|
|
15 */
|
|
16 package cmd
|
|
17
|
|
18 import (
|
|
19 "fmt"
|
|
20 "os"
|
|
21
|
|
22 "github.com/spf13/cobra"
|
|
23
|
|
24 homedir "github.com/mitchellh/go-homedir"
|
|
25 "github.com/spf13/viper"
|
|
26 )
|
|
27
|
|
28 var cfgFile string
|
|
29
|
|
30 func NewAraganeCmd() *cobra.Command {
|
|
31 return rootCmd
|
|
32 }
|
|
33
|
|
34 // rootCmd represents the base command when called without any subcommands
|
|
35 var rootCmd = &cobra.Command{
|
|
36 Use: "aragane",
|
|
37 Short: "util tools for Gears Project",
|
|
38 Long: `a tool kit of useful commands for Gears projects using CbC`,
|
|
39 }
|
|
40
|
|
41 func init() {
|
|
42 cobra.OnInitialize(initConfig)
|
|
43
|
|
44 // Here you will define your flags and configuration settings.
|
|
45 // Cobra supports persistent flags, which, if defined here,
|
|
46 // will be global for your application.
|
|
47
|
|
48 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.aragane.yaml)")
|
|
49
|
|
50 // Cobra also supports local flags, which will only run
|
|
51 // when this action is called directly.
|
|
52 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
53
|
1
|
54 rootCmd.AddCommand(
|
|
55 newCbc2cCmd(),
|
|
56 newCompletionCmd(),
|
|
57 )
|
0
|
58 }
|
|
59
|
|
60 // initConfig reads in config file and ENV variables if set.
|
|
61 func initConfig() {
|
|
62 if cfgFile != "" {
|
|
63 // Use config file from the flag.
|
|
64 viper.SetConfigFile(cfgFile)
|
|
65 } else {
|
|
66 // Find home directory.
|
|
67 home, err := homedir.Dir()
|
|
68 if err != nil {
|
|
69 fmt.Println(err)
|
|
70 os.Exit(1)
|
|
71 }
|
|
72
|
|
73 // Search config in home directory with name ".aragane" (without extension).
|
|
74 viper.AddConfigPath(home)
|
|
75 viper.SetConfigName(".aragane")
|
|
76 }
|
|
77
|
|
78 viper.AutomaticEnv() // read in environment variables that match
|
|
79
|
|
80 // If a config file is found, read it in.
|
|
81 if err := viper.ReadInConfig(); err == nil {
|
|
82 fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
83 }
|
|
84 }
|