summaryrefslogtreecommitdiff
path: root/cmd/podman/remoteclientconfig/configfile.go
blob: 06e82b1869aa24a9df55b8c597909aff87a01734 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package remoteclientconfig

import (
	"io"

	"github.com/BurntSushi/toml"
	"github.com/pkg/errors"
)

// ReadRemoteConfig takes an io.Reader representing the remote configuration
// file and returns a remoteconfig
func ReadRemoteConfig(reader io.Reader) (*RemoteConfig, error) {
	var remoteConfig RemoteConfig
	// the configuration file does not exist
	if reader == nil {
		return &remoteConfig, ErrNoConfigationFile
	}
	_, err := toml.DecodeReader(reader, &remoteConfig)
	if err != nil {
		return nil, err
	}
	// We need to validate each remote connection has fields filled out
	for name, conn := range remoteConfig.Connections {
		if len(conn.Destination) < 1 {
			return nil, errors.Errorf("connection %q has no destination defined", name)
		}
	}
	return &remoteConfig, err
}

// GetDefault returns the default RemoteConnection. If there is only one
// connection, we assume it is the default as well
func (r *RemoteConfig) GetDefault() (*RemoteConnection, error) {
	if len(r.Connections) == 0 {
		return nil, ErrNoDefinedConnections
	}
	for _, v := range r.Connections {
		if len(r.Connections) == 1 {
			// if there is only one defined connection, we assume it is
			// the default whether tagged as such or not
			return &v, nil
		}
		if v.IsDefault {
			return &v, nil
		}
	}
	return nil, ErrNoDefaultConnection
}

// GetRemoteConnection "looks up" a remote connection by name and returns it in the
// form of a RemoteConnection
func (r *RemoteConfig) GetRemoteConnection(name string) (*RemoteConnection, error) {
	if len(r.Connections) == 0 {
		return nil, ErrNoDefinedConnections
	}
	for k, v := range r.Connections {
		if k == name {
			return &v, nil
		}
	}
	return nil, errors.Wrap(ErrConnectionNotFound, name)
}