func NewAlertmanagerClient()

in cli/root.go [93:146]


func NewAlertmanagerClient(amURL *url.URL) *client.AlertmanagerAPI {
	address := defaultAmHost + ":" + defaultAmPort
	schemes := []string{"http"}

	if amURL.Host != "" {
		address = amURL.Host // URL documents host as host or host:port
	}
	if amURL.Scheme != "" {
		schemes = []string{amURL.Scheme}
	}

	cr := clientruntime.New(address, path.Join(amURL.Path, defaultAmApiv2path), schemes)

	if amURL.User != nil && httpConfigFile != "" {
		kingpin.Fatalf("basic authentication and http.config.file are mutually exclusive")
	}

	if amURL.User != nil {
		password, _ := amURL.User.Password()
		cr.DefaultAuthentication = clientruntime.BasicAuth(amURL.User.Username(), password)
	}

	if httpConfigFile != "" {
		var err error
		httpConfig, _, err := promconfig.LoadHTTPConfigFile(httpConfigFile)
		if err != nil {
			kingpin.Fatalf("failed to load HTTP config file: %v", err)
		}

		httpclient, err := promconfig.NewClientFromConfig(*httpConfig, "amtool")
		if err != nil {
			kingpin.Fatalf("failed to create a new HTTP client: %v", err)
		}
		cr = clientruntime.NewWithClient(address, path.Join(amURL.Path, defaultAmApiv2path), schemes, httpclient)
	}

	c := client.New(cr, strfmt.Default)

	if !versionCheck {
		return c
	}

	status, err := c.General.GetStatus(nil)
	if err != nil || status.Payload.VersionInfo == nil || version.Version == "" {
		// We can not get version info, or we do not know our own version. Let amtool continue.
		return c
	}

	if semver.MajorMinor("v"+*status.Payload.VersionInfo.Version) != semver.MajorMinor("v"+version.Version) {
		fmt.Fprintf(os.Stderr, "Warning: amtool version (%s) and alertmanager version (%s) are different.\n", version.Version, *status.Payload.VersionInfo.Version)
	}

	return c
}