1package cli
2
3import (
4	"crypto/tls"
5	"os"
6
7	"github.com/sensu/sensu-go/api/core/v2"
8	"github.com/sensu/sensu-go/cli/client"
9	"github.com/sensu/sensu-go/cli/client/config"
10	"github.com/sensu/sensu-go/cli/client/config/basic"
11	"github.com/sirupsen/logrus"
12	"github.com/spf13/pflag"
13)
14
15// SensuCmdName name of the command
16const SensuCmdName = "sensuctl"
17
18// TypeError is the string returned in the event of an invalid type assertion
19const TypeError = "TypeError"
20
21// SensuCli is an instance of the Sensu command line client;
22// encapsulates API client, logger & general configuration.
23type SensuCli struct {
24	Config config.Config
25	Client client.APIClient
26	Logger *logrus.Entry
27	InFile *os.File
28}
29
30// New SensuCLI given persistent flags from command
31func New(flags *pflag.FlagSet) *SensuCli {
32	conf := basic.Load(flags)
33	client := client.New(conf)
34	logger := logrus.WithFields(logrus.Fields{
35		"component": "cli-client",
36	})
37
38	tlsConfig := tls.Config{}
39
40	if conf.TrustedCAFile != "" {
41		caCertPool, err := v2.LoadCACerts(conf.TrustedCAFile)
42		if err != nil {
43			logger.Warn(err)
44			logger.Warn("Trying to use the system's default CA certificates")
45		}
46		tlsConfig.RootCAs = caCertPool
47	}
48
49	tlsConfig.InsecureSkipVerify = conf.InsecureSkipTLSVerify
50
51	tlsConfig.BuildNameToCertificate()
52	tlsConfig.CipherSuites = v2.DefaultCipherSuites
53
54	client.SetTLSClientConfig(&tlsConfig)
55
56	return &SensuCli{
57		Client: client,
58		Config: conf,
59		Logger: logger,
60		InFile: os.Stdin,
61	}
62}
63