mirror of
https://github.com/tweag/gomod2nix.git
synced 2024-11-09 12:09:08 +00:00
builder/symlink: Factor out into separate functions to make indents more readable
Implement suggestion from https://github.com/NixOS/nixpkgs/pull/188272#discussion_r958608686.
This commit is contained in:
parent
fc2d392494
commit
9b360feaf2
1 changed files with 59 additions and 52 deletions
|
@ -15,33 +15,33 @@ type Package struct {
|
||||||
ReplacedPath string `json:"replaced,omitempty"`
|
ReplacedPath string `json:"replaced,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Sources map[string]string
|
||||||
|
|
||||||
|
func populateStruct(path string, data interface{}) {
|
||||||
|
pathVal := os.Getenv(path)
|
||||||
|
if len(path) == 0 {
|
||||||
|
panic(fmt.Sprintf("env var '%s' was unset", path))
|
||||||
|
}
|
||||||
|
path = pathVal
|
||||||
|
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(b, &data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
sources := make(map[string]string)
|
sources := make(Sources)
|
||||||
pkgs := make(map[string]*Package)
|
pkgs := make(map[string]*Package)
|
||||||
|
|
||||||
{
|
populateStruct("sourcesPath", &sources)
|
||||||
b, err := os.ReadFile(os.Getenv("sourcesPath"))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(b, &sources)
|
populateStruct("jsonPath", &pkgs)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
b, err := os.ReadFile(os.Getenv("jsonPath"))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(b, &pkgs)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keys := make([]string, 0, len(pkgs))
|
keys := make([]string, 0, len(pkgs))
|
||||||
for key := range pkgs {
|
for key := range pkgs {
|
||||||
|
@ -49,6 +49,10 @@ func main() {
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
makeSymlinks(keys, sources)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeSymlinks(keys []string, sources Sources) {
|
||||||
// Iterate, in reverse order
|
// Iterate, in reverse order
|
||||||
for i := len(keys) - 1; i >= 0; i-- {
|
for i := len(keys) - 1; i >= 0; i-- {
|
||||||
key := keys[i]
|
key := keys[i]
|
||||||
|
@ -57,46 +61,49 @@ func main() {
|
||||||
paths := []string{key}
|
paths := []string{key}
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
|
|
||||||
vendorDir := filepath.Join("vendor", filepath.Dir(path))
|
vendorDir := filepath.Join("vendor", filepath.Dir(path))
|
||||||
if err := os.MkdirAll(vendorDir, 0755); err != nil {
|
if err := os.MkdirAll(vendorDir, 0o755); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(filepath.Join("vendor", path)); err == nil {
|
vendorPath := filepath.Join("vendor", path)
|
||||||
files, err := os.ReadDir(src)
|
if _, err := os.Stat(vendorPath); err == nil {
|
||||||
if err != nil {
|
populateVendorPath(vendorPath, src)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range files {
|
|
||||||
innerSrc := filepath.Join(src, f.Name())
|
|
||||||
dst := filepath.Join("vendor", path, f.Name())
|
|
||||||
if err := os.Symlink(innerSrc, dst); err != nil {
|
|
||||||
// assume it's an existing directory, try to link the directory content instead.
|
|
||||||
// TODO should we do this recursively
|
|
||||||
files, err := os.ReadDir(innerSrc)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for _, f := range files {
|
|
||||||
if err := os.Symlink(filepath.Join(innerSrc, f.Name()), filepath.Join(dst, f.Name())); err != nil {
|
|
||||||
fmt.Println("ignore symlink error", filepath.Join(innerSrc, f.Name()), filepath.Join(dst, f.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the file doesn't already exist, just create a simple symlink
|
// If the file doesn't already exist, just create a simple symlink
|
||||||
err := os.Symlink(src, filepath.Join("vendor", path))
|
err := os.Symlink(src, vendorPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func populateVendorPath(vendorPath string, src string) {
|
||||||
|
files, err := os.ReadDir(src)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
innerSrc := filepath.Join(src, f.Name())
|
||||||
|
dst := filepath.Join(vendorPath, f.Name())
|
||||||
|
if err := os.Symlink(innerSrc, dst); err != nil {
|
||||||
|
// assume it's an existing directory, try to link the directory content instead.
|
||||||
|
// TODO should we do this recursively?
|
||||||
|
files, err := os.ReadDir(innerSrc)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, f := range files {
|
||||||
|
srcFile := filepath.Join(innerSrc, f.Name())
|
||||||
|
dstFile := filepath.Join(dst, f.Name())
|
||||||
|
if err := os.Symlink(srcFile, dstFile); err != nil {
|
||||||
|
fmt.Println("ignoring symlink error", srcFile, dstFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue