summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/vendor/github.com/packer-community/winrmcp/winrmcp/endpoint.go
blob: a39d32159b1ee717f96d14b304a849cfe15c236a (plain)
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
package winrmcp

import (
	"errors"
	"fmt"
	"net"
	"strconv"
	"strings"
	"time"

	"github.com/masterzen/winrm"
)

func parseEndpoint(addr string, https bool, insecure bool, caCert []byte, timeout time.Duration) (*winrm.Endpoint, error) {
	var host string
	var port int

	if addr == "" {
		return nil, errors.New("Couldn't convert \"\" to an address.")
	}
	if !strings.Contains(addr, ":") {
		host = addr
		port = 5985
	} else {
		shost, sport, err := net.SplitHostPort(addr)
		if err != nil {
			return nil, fmt.Errorf("Couldn't convert \"%s\" to an address.", addr)
		}
		host = shost
		port, err = strconv.Atoi(sport)
		if err != nil {
			return nil, errors.New("Couldn't convert \"%s\" to a port number.")
		}
	}

	return &winrm.Endpoint{
		Host:     host,
		Port:     port,
		HTTPS:    https,
		Insecure: insecure,
		CACert:   caCert,
		Timeout:  timeout,
	}, nil
}