From eddfcec0f548d1512945c54c39e9b6766b6bad88 Mon Sep 17 00:00:00 2001 From: Matthias Loibl Date: Wed, 8 Aug 2018 10:35:20 +0200 Subject: [PATCH] Check for existing jsonnetfile.json before init --- Makefile | 2 +- cmd/jb/main.go | 14 ++++++++++++-- pkg/packages.go | 7 +++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index a30a009..face73e 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ build: install: build @$(eval OUTPUT=$(OUT_DIR)/$(GOOS)/$(GOARCH)/$(BIN)) - @echo ">> copying $(BIN) into $(GOPATH)/$(BIN)" + @echo ">> copying $(BIN) into $(GOPATH)/bin/$(BIN)" @cp $(OUTPUT) $(GOPATH)/bin/$(BIN) test: diff --git a/cmd/jb/main.go b/cmd/jb/main.go index 082d4a9..e4077f6 100644 --- a/cmd/jb/main.go +++ b/cmd/jb/main.go @@ -101,9 +101,19 @@ func Main() int { } func initCommand() int { - err := ioutil.WriteFile(pkg.JsonnetFile, []byte("{}"), 0644) + exists, err := pkg.FileExists(pkg.JsonnetFile) if err != nil { - kingpin.Fatalf("Failed to write new jsonnetfile.json: %v", err) + kingpin.Errorf("Failed to check for jsonnetfile.json: %v", err) + return 1 + } + + if exists { + kingpin.Errorf("jsonnetfile.json already exists") + return 1 + } + + if err := ioutil.WriteFile(pkg.JsonnetFile, []byte("{}"), 0644); err != nil { + kingpin.Errorf("Failed to write new jsonnetfile.json: %v", err) return 1 } diff --git a/pkg/packages.go b/pkg/packages.go index 25e8464..b886b28 100644 --- a/pkg/packages.go +++ b/pkg/packages.go @@ -147,9 +147,8 @@ func insertDependency(deps []spec.Dependency, newDep spec.Dependency) ([]spec.De return res, nil } -func LockExists(dir string) (bool, error) { - lockfile := path.Join(dir, JsonnetLockFile) - _, err := os.Stat(lockfile) +func FileExists(path string) (bool, error) { + _, err := os.Stat(path) if os.IsNotExist(err) { return false, nil } @@ -166,7 +165,7 @@ func ChooseJsonnetFile(dir string) (string, bool, error) { filename := lockfile isLock := true - lockExists, err := LockExists(dir) + lockExists, err := FileExists(filepath.Join(dir, JsonnetLockFile)) if err != nil { return "", false, err }