summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/root.go
blob: e48bb74c97fd647f73e28f5a679402d406fa256d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Root Cobra command: vespa
// author: bratseth

package cmd

import (
	"log"
	"os"

	"github.com/logrusorgru/aurora"
	"github.com/mattn/go-colorable"
	"github.com/mattn/go-isatty"
	"github.com/spf13/cobra"
)

var (
	// global flags
	// TODO: add timeout flag
	// TODO: add flag to show http request made
	rootCmd = &cobra.Command{
		Use:   "vespa",
		Short: "A command-line tool for working with Vespa instances",
	}
	color          aurora.Aurora
	targetArg      string
	applicationArg string
)

const (
	applicationFlag = "application"
	targetFlag      = "target"
)

func configureLogger() {
	color = aurora.NewAurora(isatty.IsTerminal(os.Stdout.Fd()))
	log.SetFlags(0) // No timestamps
	log.SetOutput(colorable.NewColorableStdout())
}

func init() {
	configureLogger()
	cobra.OnInitialize(readConfig)
	rootCmd.PersistentFlags().StringVarP(&targetArg, targetFlag, "t", "local", "The name or URL of the recipient of this command")
	rootCmd.PersistentFlags().StringVarP(&applicationArg, applicationFlag, "a", "", "The application to manage")
	bindFlagToConfig(targetFlag, rootCmd)
	bindFlagToConfig(applicationFlag, rootCmd)
}

// Execute executes the root command.
func Execute() error { return rootCmd.Execute() }