github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cache/ingresses.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/telepresenceio/telepresence/rpc/v2/manager"
     8  )
     9  
    10  const ingressesFile = "ingresses.json"
    11  
    12  // SaveIngressesToUserCache saves the provided ingresses to user cache and returns an error if
    13  // something goes wrong while marshalling or persisting.
    14  func SaveIngressesToUserCache(ctx context.Context, ingresses map[string]*manager.IngressInfo) error {
    15  	if len(ingresses) == 0 {
    16  		return DeleteIngressesFromUserCache(ctx)
    17  	}
    18  	return SaveToUserCache(ctx, ingresses, ingressesFile, Public)
    19  }
    20  
    21  // LoadIngressesFromUserCache gets the ingresses from cache. An empty map is returned if the
    22  // file does not exist. An error is returned if something goes wrong while loading or unmarshalling.
    23  func LoadIngressesFromUserCache(ctx context.Context) (map[string]*manager.IngressInfo, error) {
    24  	var ingresses map[string]*manager.IngressInfo
    25  	err := LoadFromUserCache(ctx, &ingresses, ingressesFile)
    26  	if err != nil {
    27  		if !os.IsNotExist(err) {
    28  			return nil, err
    29  		}
    30  		return make(map[string]*manager.IngressInfo), nil
    31  	}
    32  	return ingresses, nil
    33  }
    34  
    35  // DeleteIngressesFromUserCache removes the ingresses cache if exists or returns an error. An attempt
    36  // to remove a non-existing cache is a no-op and the function returns nil.
    37  func DeleteIngressesFromUserCache(ctx context.Context) error {
    38  	return DeleteFromUserCache(ctx, ingressesFile)
    39  }