diff options
| -rw-r--r-- | main.go | 9 | ||||
| -rw-r--r-- | pkg/earlyerrors/errors.go | 42 | ||||
| -rw-r--r-- | pkg/rtree/index.go | 14 | ||||
| -rw-r--r-- | pkg/rtree/remote.go | 4 | ||||
| -rw-r--r-- | pkg/rtree/repo.go | 7 |
5 files changed, 67 insertions, 9 deletions
@@ -24,6 +24,7 @@ import ( "path/filepath" "github.com/majewsky/gofu/pkg/cli" + "github.com/majewsky/gofu/pkg/earlyerrors" "github.com/majewsky/gofu/pkg/rtree" ) @@ -33,6 +34,14 @@ func main() { fmt.Fprintln(os.Stderr, "FATAL: initialization failed") os.Exit(255) } + + if len(earlyerrors.Get()) > 0 { + for _, msg := range earlyerrors.Get() { + ci.ShowError(msg) + } + os.Exit(255) + } + os.Exit(execApplet(ci, filepath.Base(os.Args[0]), os.Args[1:], true)) } diff --git a/pkg/earlyerrors/errors.go b/pkg/earlyerrors/errors.go new file mode 100644 index 0000000..edde0f8 --- /dev/null +++ b/pkg/earlyerrors/errors.go @@ -0,0 +1,42 @@ +/******************************************************************************* +* +* 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 earlyerrors can be used to cache errors that occurred during func +//init(). These errors can then be displayed by func main(). +package earlyerrors + +import "fmt" + +var errs []string + +//Put submits an error message. +func Put(err string) { + if err != "" { + errs = append(errs, err) + } +} + +//Putf builds and submits an error message. +func Putf(err string, args ...interface{}) { + Put(fmt.Sprintf(err, args...)) +} + +//Get returns all collected errors, or an empty slice if there were none. +func Get() []string { + return errs +} diff --git a/pkg/rtree/index.go b/pkg/rtree/index.go index c2c0202..d0813fe 100644 --- a/pkg/rtree/index.go +++ b/pkg/rtree/index.go @@ -29,6 +29,7 @@ import ( "strings" "github.com/majewsky/gofu/pkg/cli" + "github.com/majewsky/gofu/pkg/earlyerrors" yaml "gopkg.in/yaml.v2" ) @@ -38,18 +39,21 @@ type Index struct { Repos []*Repo `yaml:"repos"` } -func indexPath() string { +var indexPath string + +func init() { homeDir := os.Getenv("HOME") if homeDir == "" { - panic(errors.New("$HOME is not set (rtree needs the HOME variable to locate its index file)")) + earlyerrors.Put("$HOME is not set (rtree needs the HOME variable to locate its index file)") + } else { + indexPath = filepath.Join(homeDir, ".rtree/index.yaml") } - return filepath.Join(homeDir, ".rtree/index.yaml") } //ReadIndex reads the index file. func ReadIndex() (*Index, []error) { //read contents of index file - path := indexPath() + path := indexPath buf, err := ioutil.ReadFile(path) if err != nil { if os.IsNotExist(err) { @@ -106,7 +110,7 @@ func (i *Index) Write(ci *cli.Interface) error { return err } - path := indexPath() + path := indexPath err = os.MkdirAll(filepath.Dir(path), 0755) if err != nil { return err diff --git a/pkg/rtree/remote.go b/pkg/rtree/remote.go index 380fc80..353916d 100644 --- a/pkg/rtree/remote.go +++ b/pkg/rtree/remote.go @@ -25,6 +25,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/majewsky/gofu/pkg/earlyerrors" ) //remoteAlias describes an alias that can be used in a Git remote URL (as @@ -42,7 +44,7 @@ func init() { cmd.Stdout = &buf err := cmd.Run() if err != nil { - panic(err) + earlyerrors.Put("exec `git config --global -l` failed: " + err.Error()) } rx := regexp.MustCompile(`^url\.([^=]+)\.insteadof=(.+)$`) diff --git a/pkg/rtree/repo.go b/pkg/rtree/repo.go index fba658b..99df30d 100644 --- a/pkg/rtree/repo.go +++ b/pkg/rtree/repo.go @@ -19,7 +19,6 @@ package rtree import ( - "errors" "fmt" "os" "path/filepath" @@ -27,6 +26,7 @@ import ( "strings" "github.com/majewsky/gofu/pkg/cli" + "github.com/majewsky/gofu/pkg/earlyerrors" ) //RootPath is the directory below which all repositories are located. Its value @@ -36,9 +36,10 @@ var RootPath string func init() { gopath := os.Getenv("GOPATH") if gopath == "" { - panic(errors.New("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)")) + earlyerrors.Put("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)") + } else { + RootPath = filepath.Join(gopath, "src") } - RootPath = filepath.Join(gopath, "src") } //Repo describes the entry for a repository in the index file. |
