summaryrefslogtreecommitdiff
path: root/pkg/machine/connection.go
blob: 3edcbd10e03ea87b3a21b0a8d7f34bb3a0e3acb6 (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
// +build amd64,linux arm64,linux amd64,darwin arm64,darwin

package machine

import (
	"fmt"

	"github.com/containers/common/pkg/config"
	"github.com/pkg/errors"
)

func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) error {
	if len(identity) < 1 {
		return errors.New("identity must be defined")
	}
	cfg, err := config.ReadCustomConfig()
	if err != nil {
		return err
	}
	if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
		return errors.New("cannot overwrite connection")
	}
	if isDefault {
		cfg.Engine.ActiveService = name
	}
	dst := config.Destination{
		URI: uri.String(),
	}
	dst.Identity = identity
	if cfg.Engine.ServiceDestinations == nil {
		cfg.Engine.ServiceDestinations = map[string]config.Destination{
			name: dst,
		}
		cfg.Engine.ActiveService = name
	} else {
		cfg.Engine.ServiceDestinations[name] = dst
	}
	return cfg.Write()
}

func RemoveConnection(name string) error {
	cfg, err := config.ReadCustomConfig()
	if err != nil {
		return err
	}
	if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
		delete(cfg.Engine.ServiceDestinations, name)
	} else {
		return errors.Errorf("unable to find connection named %q", name)
	}
	return cfg.Write()
}