annotate cmd/root.go @ 1:bebb292171ee

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