aboutsummaryrefslogtreecommitdiff
path: root/internal/rtree/init.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2020-03-28 13:53:10 +0100
committerStefan Majewsky <majewsky@gmx.net>2020-03-28 13:53:10 +0100
commited2b9374599cc00467b1c27121318748cc0d3f49 (patch)
tree65e3200a0b69817b24ae772223d1d852a52458af /internal/rtree/init.go
parent1ff86053ddef8de3247909434e0bd7000c060f2a (diff)
downloadgofu-ed2b9374599cc00467b1c27121318748cc0d3f49.tar.gz
rename pkg/ to internal/
Diffstat (limited to 'internal/rtree/init.go')
-rw-r--r--internal/rtree/init.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/internal/rtree/init.go b/internal/rtree/init.go
new file mode 100644
index 0000000..569f042
--- /dev/null
+++ b/internal/rtree/init.go
@@ -0,0 +1,99 @@
+/*******************************************************************************
+*
+* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
+*
+* This program is free software: you can redistribute it and/or modify it under
+* the terms of the GNU General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later
+* version.
+*
+* This program is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along with
+* this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*******************************************************************************/
+
+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
+
+//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, ".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.Split(out, "\n") {
+ match := rx.FindStringSubmatch(line)
+ if match == nil {
+ continue
+ }
+ RemoteAliases = append(RemoteAliases, &RemoteAlias{
+ Alias: match[2],
+ Replacement: match[1],
+ })
+ }
+ }
+
+ return ok
+}