ELIOT Command Line Interface Commit
[eliot.git] / blueprints / common / elcli / elcli / cmd / root.go
1 /*
2 Copyright © 2019 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
17 package cmd
18
19 import (
20   "fmt"
21   "os"
22   "github.com/spf13/cobra"
23
24   homedir "github.com/mitchellh/go-homedir"
25   "github.com/spf13/viper"
26
27 )
28
29
30 var cfgFile string
31
32 var (
33   elcliLongDescription = `
34   +----------------------------------------------------------+
35   | ELCLI                                                    | 
36   | Command Line Interface to Bootsrap ELIOT Cluster         |
37   +----------------------------------------------------------+
38   
39   ELCLI Command is use to setup the ELIOT Cluster. 
40   It installs the ELIOT Manager and the ELIOT Nodes. 
41   ELIOT Manager is the core-controller and the ELIOT Nodes are
42   the edge nodes which acts as IOT Gateway or uCPE, where the 
43   application PODS will be running.
44   `
45   elcliExample = `
46   +-----------------------------------------------------------+
47   |To setup up the ELIOT Cluster the elcli init command has to|
48   |be executed in the ELIOT Manager Node.                     |
49   |                                                           |
50   |Example :                                                  |
51   |elcli init                                                 |
52   |                                                           |
53   +-----------------------------------------------------------+
54
55   `
56 )
57
58
59 // rootCmd represents the base command when called without any subcommands
60 var rootCmd = &cobra.Command{
61   Use:   "elcli",
62   Short: "elcli : Bootstarp ELIOT Cluster",
63   Long:   elcliLongDescription,
64   Example: elcliExample,
65 }
66
67 // Execute adds all child commands to the root command and sets flags appropriately.
68 // This is called by main.main(). It only needs to happen once to the rootCmd.
69 func Execute() {
70   if err := rootCmd.Execute(); err != nil {
71     fmt.Println(err)
72     os.Exit(1)
73   }
74 }
75
76 func init() {
77   cobra.OnInitialize(initConfig)
78
79 }
80
81
82 // initConfig reads in config file and ENV variables if set.
83 func initConfig() {
84   if cfgFile != "" {
85     // Use config file from the flag.
86     viper.SetConfigFile(cfgFile)
87   } else {
88     // Find home directory.
89     home, err := homedir.Dir()
90     if err != nil {
91       fmt.Println(err)
92       os.Exit(1)
93     }
94
95     // Search config in home directory with name ".elcli" (without extension).
96     viper.AddConfigPath(home)
97     viper.SetConfigName(".elcli")
98   }
99
100   viper.AutomaticEnv() // read in environment variables that match
101
102   // If a config file is found, read it in.
103   if err := viper.ReadInConfig(); err == nil {
104     fmt.Println("Using config file:", viper.ConfigFileUsed())
105   }
106 }
107