summaryrefslogtreecommitdiff
path: root/cmd/podman/remoteclientconfig/configfile.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2019-06-04 20:02:01 +0200
committerGitHub <noreply@github.com>2019-06-04 20:02:01 +0200
commit3fb9669a63558955766e7bd9152b8b542b00d9dd (patch)
tree1a714fe12c96023f390592f3d93501102d6fd72a /cmd/podman/remoteclientconfig/configfile.go
parent2a4e5aac197d86c836d9026e97b089744cc609c5 (diff)
parentdc7ae3117146aa4e91d454b4b1afa03058638b13 (diff)
downloadpodman-3fb9669a63558955766e7bd9152b8b542b00d9dd.tar.gz
podman-3fb9669a63558955766e7bd9152b8b542b00d9dd.tar.bz2
podman-3fb9669a63558955766e7bd9152b8b542b00d9dd.zip
Merge pull request #3231 from baude/remoteclientconf
podman-remote.conf enablement
Diffstat (limited to 'cmd/podman/remoteclientconfig/configfile.go')
-rw-r--r--cmd/podman/remoteclientconfig/configfile.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/cmd/podman/remoteclientconfig/configfile.go b/cmd/podman/remoteclientconfig/configfile.go
new file mode 100644
index 000000000..aa3e82a31
--- /dev/null
+++ b/cmd/podman/remoteclientconfig/configfile.go
@@ -0,0 +1,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 %s 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)
+}