github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/modules/package.go (about)

     1  package modules
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/lmorg/murex/config/profile"
     9  	"github.com/lmorg/murex/utils/json"
    10  )
    11  
    12  type packageDb struct {
    13  	Protocol string
    14  	URI      string
    15  	Package  string
    16  }
    17  
    18  func readPackagesFile(path string) ([]packageDb, error) {
    19  	var db []packageDb
    20  
    21  	err := profile.ReadJson(path, &db)
    22  	return db, err
    23  }
    24  
    25  func writePackagesFile(db *[]packageDb) error {
    26  	path := profile.ModulePath() + profile.PackagesFile
    27  
    28  	file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0640)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	defer file.Close()
    33  
    34  	b, err := json.Marshal(db, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	_, err = file.Write(b)
    40  	return err
    41  }
    42  
    43  func readPackageFile(path string) (profile.Package, error) {
    44  	var pack profile.Package
    45  
    46  	err := profile.ReadJson(path, &pack)
    47  	return pack, err
    48  }
    49  
    50  func mvPackagePath(path string) (string, error) {
    51  	if !filepath.IsAbs(path) {
    52  		panic("path should be absolute")
    53  	}
    54  
    55  	pack, err := readPackageFile(path + "/package.json")
    56  	if err != nil {
    57  		return path, err
    58  	}
    59  
    60  	if path != profile.ModulePath()+pack.Name {
    61  		err = os.Rename(path, profile.ModulePath()+pack.Name)
    62  		if err != nil {
    63  			os.Stdout.WriteString(fmt.Sprintf(
    64  				"WARNING: unable to do post-install tidy up: %s.\n         To manually apply the changes please run the following commands:\n             mv %s %s\n             murex-package reload\n",
    65  				err, path, profile.ModulePath()+pack.Name,
    66  			))
    67  		}
    68  	}
    69  
    70  	return pack.Name, nil
    71  }