Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
check if there a go.mod in a directory without any go files
Browse files Browse the repository at this point in the history
  • Loading branch information
aperezg committed Nov 9, 2019
1 parent 6cf141c commit 71d2094
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
40 changes: 31 additions & 9 deletions here/dir.go
Expand Up @@ -2,6 +2,7 @@ package here

import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -39,20 +40,30 @@ func Dir(p string) (Info, error) {
return i, err
}

if strings.Contains(es, "can't load package: package .") {
if _, err := os.Stat(fmt.Sprintf("%s/go.mod", p)); err == nil {
var mod Module
bm, err := run ("go", "list", "-m", "-json")
if err != nil {
return i, err
}

if err := json.Unmarshal(bm, &mod); err != nil {
return i, err
}
info := NewInfoFromPath(p, mod)
prepareInfo(p, info, &i)

return i, err
}
}

info, err := Dir(filepath.Dir(p))
if err != nil {
return info, err
}
i.Module = info.Module

ph := strings.TrimPrefix(p, info.Module.Dir)

i.ImportPath = path.Join(info.Module.Path, ph)
i.Name = path.Base(i.ImportPath)

ph = filepath.Join(info.Module.Dir, ph)
i.Dir = ph

prepareInfo(p, info, &i)
return i, err
}

Expand All @@ -73,3 +84,14 @@ func Dir(p string) (Info, error) {

return i, nil
}

func prepareInfo(p string, info Info, target *Info) {
target.Module = info.Module
ph := strings.TrimPrefix(p, target.Module.Dir)

target.ImportPath = path.Join(info.Module.Path, ph)
target.Name = path.Base(target.ImportPath)

ph = filepath.Join(info.Module.Dir, ph)
target.Dir = ph
}
10 changes: 10 additions & 0 deletions here/info.go
Expand Up @@ -16,6 +16,16 @@ type Info struct {
Module Module
}

// NewInfoFromPath initialize a Info with a basic information and his module
// this method could be used when the Unmarshal information is not possible
func NewInfoFromPath(path string, m Module) Info {
return Info{
Dir: path,
ImportPath: "command-line-arguments",
Module: m,
}
}

func (fi Info) MarshalJSON() ([]byte, error) {
mm := map[string]interface{}{
"ImportPath": fi.ImportPath,
Expand Down

0 comments on commit 71d2094

Please sign in to comment.