github.com/pluralsh/plural-cli@v0.9.5/pkg/pluralfile/lock.go (about)

     1  package pluralfile
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/pluralsh/plural-cli/pkg/api"
     9  	"github.com/pluralsh/plural-cli/pkg/config"
    10  	"github.com/pluralsh/plural-cli/pkg/utils/pathing"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  type Lockfile struct {
    15  	Artifact    map[string]string
    16  	Terraform   map[string]string
    17  	Helm        map[string]string
    18  	Recipe      map[string]string
    19  	Integration map[string]string
    20  	Crd         map[string]string
    21  	Ird         map[string]string
    22  	Tag         map[string]string
    23  	Attrs       map[string]string
    24  }
    25  
    26  func lock() *Lockfile {
    27  	return &Lockfile{
    28  		Artifact:    map[string]string{},
    29  		Terraform:   map[string]string{},
    30  		Helm:        map[string]string{},
    31  		Recipe:      map[string]string{},
    32  		Integration: map[string]string{},
    33  		Crd:         map[string]string{},
    34  		Ird:         map[string]string{},
    35  		Tag:         map[string]string{},
    36  		Attrs:       map[string]string{},
    37  	}
    38  }
    39  
    40  func (plrl *Pluralfile) Lock(path string) (*Lockfile, error) {
    41  	client := api.NewClient()
    42  	applyLock, err := client.AcquireLock(plrl.Repo)
    43  	if err != nil {
    44  		return lock(), nil
    45  	}
    46  
    47  	if applyLock == nil {
    48  		return nil, fmt.Errorf("Could not fetch apply lock, do you have publish permissions for this repo?")
    49  	}
    50  
    51  	if applyLock.Lock == "" {
    52  		return Lock(path)
    53  	}
    54  
    55  	lock := lock()
    56  	if err := yaml.Unmarshal([]byte(applyLock.Lock), lock); err != nil {
    57  		return nil, err
    58  	}
    59  	return lock, nil
    60  }
    61  
    62  func (plrl *Pluralfile) Flush(lock *Lockfile) error {
    63  	client := api.NewClient()
    64  	io, err := yaml.Marshal(lock)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	_, err = client.ReleaseLock(plrl.Repo, string(io))
    70  	return api.GetErrorResponse(err, "ReleaseLock")
    71  }
    72  
    73  func Lock(path string) (*Lockfile, error) {
    74  	conf := config.Read()
    75  	lock := lock()
    76  	lockfile := lockPath(path, conf.LockProfile)
    77  	content, err := os.ReadFile(lockfile)
    78  	if err != nil {
    79  		return lock, nil
    80  	}
    81  
    82  	if err := yaml.Unmarshal(content, lock); err != nil {
    83  		return nil, err
    84  	}
    85  	return lock, nil
    86  }
    87  
    88  func lockPath(path string, profile string) string {
    89  	if profile == "" {
    90  		return pathing.SanitizeFilepath(filepath.Join(filepath.Dir(path), "plural.lock"))
    91  	}
    92  
    93  	return pathing.SanitizeFilepath(filepath.Join(filepath.Dir(path), fmt.Sprintf("plural.%s.lock", profile)))
    94  }
    95  
    96  func (lock *Lockfile) Flush(path string) error {
    97  	conf := config.Read()
    98  	io, err := yaml.Marshal(lock)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	return os.WriteFile(lockPath(path, conf.LockProfile), io, 0644)
   103  }
   104  
   105  func (lock *Lockfile) getSha(name ComponentName, key string) string {
   106  	switch name {
   107  	case HELM:
   108  		sha := lock.Helm[key]
   109  		return sha
   110  	case TERRAFORM:
   111  		sha := lock.Terraform[key]
   112  		return sha
   113  	case RECIPE:
   114  		sha := lock.Recipe[key]
   115  		return sha
   116  	case ARTIFACT:
   117  		sha := lock.Artifact[key]
   118  		return sha
   119  	case INTEGRATION:
   120  		sha := lock.Integration[key]
   121  		return sha
   122  	case CRD:
   123  		sha := lock.Crd[key]
   124  		return sha
   125  	case IRD:
   126  		sha := lock.Ird[key]
   127  		return sha
   128  	case TAG:
   129  		sha := lock.Tag[key]
   130  		return sha
   131  	case REPO_ATTRS:
   132  		sha := lock.Attrs[key]
   133  		return sha
   134  	default:
   135  		return ""
   136  	}
   137  }
   138  
   139  func (lock *Lockfile) addSha(name ComponentName, key string, sha string) {
   140  	switch name {
   141  	case HELM:
   142  		lock.Helm[key] = sha
   143  		return
   144  	case TERRAFORM:
   145  		lock.Terraform[key] = sha
   146  	case RECIPE:
   147  		lock.Recipe[key] = sha
   148  	case ARTIFACT:
   149  		lock.Artifact[key] = sha
   150  	case INTEGRATION:
   151  		lock.Integration[key] = sha
   152  	case CRD:
   153  		lock.Crd[key] = sha
   154  	case IRD:
   155  		lock.Ird[key] = sha
   156  	case TAG:
   157  		lock.Tag[key] = sha
   158  	case REPO_ATTRS:
   159  		lock.Attrs[key] = sha
   160  	default:
   161  		return
   162  	}
   163  }