Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wslulciuc committed Jul 2, 2017
1 parent 452040d commit e19b78a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {

var root string
if len(args) <= 0 {
// Warn if a project manifest file is already present.
if checkRootProjectManifestExists(ctx) {
ctx.Loggers.Err.Println("WARNING: found manifest file in another directory.")
}
// Warn if new project initialization is being performed in a project subdirectory.
subdir, err := checkInSubdir(ctx)
if err != nil {
return errors.Wrap(err, "checkInSubDir")
}
if subdir {
ctx.Loggers.Err.Println("WARNING: it is recommended that project initialization be performed at the project root, not a project subdirectory.")
}
// Set project root to current working directory.
root = ctx.WorkingDir
} else {
root = args[0]
Expand Down Expand Up @@ -236,6 +249,45 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return nil
}

// checkRootProjectManifestExists returns true if a project manifest exists in
// another directory (somewhere above the current directory); otherwise returns false.
func checkRootProjectManifestExists(ctx *dep.Ctx) bool {
pdir := filepath.Dir(ctx.WorkingDir)
// On attempts to load the project, we want to ignore errors returned by ctx.LoadProject()
// and perform a nil-check on the project root returned.
// TODO: Defining an error type `NoRootProjectFound` would allow for explicit checks against
// this type, handling it specifically. Thoughts?
pr, _ := ctx.LoadProject(pdir)
exists := pr != nil
return exists
}

// checkInSubdir returns true if project initialization is being performed in a subdirectory
// relative to the project root directory `$GOPATH/src/github.com/user/project`; otherwise returns false.
func checkInSubdir(ctx *dep.Ctx) (bool, error) {
sr, err := ctx.SplitAbsoluteProjectRoot(ctx.WorkingDir)
if err != nil {
return false, err
}

sm, err := ctx.SourceManager()
if err != nil {
return false, err
}
sm.UseDefaultSignalHandling()
defer sm.Release()

pr, err := sm.DeduceProjectRoot(sr)
if err != nil {
return false, err
}

// TODO: This is unlikely the cleanest way to determine if we are in a subdirectory
// of the project root path.
in := !strings.HasSuffix(ctx.WorkingDir, string(pr))
return in, nil
}

// contains checks if a array of strings contains a value
func contains(a []string, b string) bool {
for _, v := range a {
Expand Down

0 comments on commit e19b78a

Please sign in to comment.