...

Source file src/github.com/sassoftware/relic/cmdline/shared/main.go

Documentation: github.com/sassoftware/relic/cmdline/shared

     1  //
     2  // Copyright (c) SAS Institute Inc.
     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 shared
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/sassoftware/relic/config"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var (
    29  	ArgConfig     string
    30  	ArgDebug      uint32
    31  	CurrentConfig *config.Config
    32  
    33  	argVersion bool
    34  )
    35  
    36  var lateHooks []func()
    37  
    38  var RootCmd = &cobra.Command{
    39  	Use:              "relic",
    40  	PersistentPreRun: showVersion,
    41  	RunE:             bailUnlessVersion,
    42  }
    43  
    44  func init() {
    45  	RootCmd.PersistentFlags().StringVarP(&ArgConfig, "config", "c", "", "Configuration file")
    46  	RootCmd.PersistentFlags().BoolVar(&argVersion, "version", false, "Show version and exit")
    47  	RootCmd.PersistentFlags().Uint32VarP(&ArgDebug, "debug", "d", 0, "Log additional diagnostic data. (0-9)")
    48  }
    49  
    50  func showVersion(cmd *cobra.Command, args []string) {
    51  	if argVersion {
    52  		fmt.Printf("relic version %s (%s)\n", config.Version, config.Commit)
    53  		os.Exit(0)
    54  	}
    55  }
    56  
    57  func bailUnlessVersion(cmd *cobra.Command, args []string) error {
    58  	if !argVersion {
    59  		return errors.New("Expected a command")
    60  	}
    61  	return nil
    62  }
    63  
    64  func AddLateHook(f func()) {
    65  	lateHooks = append(lateHooks, f)
    66  }
    67  
    68  func Main() {
    69  	for _, f := range lateHooks {
    70  		f()
    71  	}
    72  	if err := RootCmd.Execute(); err != nil {
    73  		fmt.Fprintln(os.Stderr, err)
    74  		os.Exit(1)
    75  	}
    76  }
    77  

View as plain text