summaryrefslogtreecommitdiff
path: root/internal/rtree/init.go
blob: d2118c0947703e85d0a04a08192708a3ad579565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-FileCopyrightText: 2017 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: GPL-3.0-only

package rtree

import (
	"os"
	"path/filepath"
	"regexp"
	"strings"

	"github.com/majewsky/gofu/internal/cli"
)

// RemoteAlias describes an alias that can be used in a Git remote URL (as
// defined by the "url.<base>.insteadOf" directive in man:git-config(1)).
type RemoteAlias struct {
	Alias       string
	Replacement string
}

// IndexPath is where the index file is stored.
var IndexPath string

// OldIndexPath is where the old index file was stored.
// This is only used to detect if a system has not been upgraded to the new index format yet.
// TODO: remove this after some time
var OldIndexPath string

// RootPath is the directory below which all repositories are located. Its value
// is $GOPATH/src to match the repository layout created by `go get`.
var RootPath string

// RemoteAliases is the list of remote aliases that is used by ExpandRemoteURL().
var RemoteAliases []*RemoteAlias

// Init initializes the global variables of this package to their standard
// values, unless they are already populated. Unit tests shall set IndexPath,
// RootPath etc. before calling Exec(), such that this function becomes a no-op
// when called by Exec().
//
// Returns false if initialization failed.
func Init() bool {
	ok := true //until shown otherwise

	if IndexPath == "" {
		homeDir := os.Getenv("HOME")
		if homeDir == "" {
			cli.Interface.ShowError("$HOME is not set (rtree needs the HOME variable to locate its index file)")
			ok = false //but keep going to report all errors at once
		} else {
			IndexPath = filepath.Join(homeDir, ".config/rtree/index.json")
			OldIndexPath = filepath.Join(homeDir, ".rtree/index.yaml")
		}
	}

	if RootPath == "" {
		gopath := os.Getenv("GOPATH")
		if gopath == "" {
			cli.Interface.ShowError("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)")
			ok = false //but keep going to report all errors at once
		} else {
			RootPath = filepath.Join(gopath, "src")
		}
	}

	if RemoteAliases == nil {
		out, err := cli.Interface.CaptureStdout(cli.Command{
			Program: []string{"git", "config", "--global", "-l"},
		})
		if err != nil {
			cli.Interface.ShowError(err.Error())
			ok = false //but keep going to report all errors at once
		}

		rx := regexp.MustCompile(`^url\.([^=]+)\.insteadof=(.+)$`)
		for line := range strings.SplitSeq(out, "\n") {
			match := rx.FindStringSubmatch(line)
			if match == nil {
				continue
			}
			RemoteAliases = append(RemoteAliases, &RemoteAlias{
				Alias:       match[2],
				Replacement: match[1],
			})
		}
	}

	return ok
}