github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/machine/connection.go (about)

     1  //go:build amd64 || arm64
     2  // +build amd64 arm64
     3  
     4  package machine
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/containers/common/pkg/config"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) error {
    14  	if len(identity) < 1 {
    15  		return errors.New("identity must be defined")
    16  	}
    17  	cfg, err := config.ReadCustomConfig()
    18  	if err != nil {
    19  		return err
    20  	}
    21  	if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
    22  		return errors.New("cannot overwrite connection")
    23  	}
    24  	if isDefault {
    25  		cfg.Engine.ActiveService = name
    26  	}
    27  	dst := config.Destination{
    28  		URI: uri.String(),
    29  	}
    30  	dst.Identity = identity
    31  	if cfg.Engine.ServiceDestinations == nil {
    32  		cfg.Engine.ServiceDestinations = map[string]config.Destination{
    33  			name: dst,
    34  		}
    35  		cfg.Engine.ActiveService = name
    36  	} else {
    37  		cfg.Engine.ServiceDestinations[name] = dst
    38  	}
    39  	return cfg.Write()
    40  }
    41  
    42  func AnyConnectionDefault(name ...string) (bool, error) {
    43  	cfg, err := config.ReadCustomConfig()
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  	for _, n := range name {
    48  		if n == cfg.Engine.ActiveService {
    49  			return true, nil
    50  		}
    51  	}
    52  
    53  	return false, nil
    54  }
    55  
    56  func ChangeDefault(name string) error {
    57  	cfg, err := config.ReadCustomConfig()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	cfg.Engine.ActiveService = name
    63  
    64  	return cfg.Write()
    65  }
    66  
    67  func RemoveConnection(name string) error {
    68  	cfg, err := config.ReadCustomConfig()
    69  	if err != nil {
    70  		return err
    71  	}
    72  	if _, ok := cfg.Engine.ServiceDestinations[name]; ok {
    73  		delete(cfg.Engine.ServiceDestinations, name)
    74  	} else {
    75  		return errors.Errorf("unable to find connection named %q", name)
    76  	}
    77  	return cfg.Write()
    78  }