Skip to content

Commit

Permalink
recently used skill
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jun 15, 2019
1 parent 4f0a812 commit d8dfaf2
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 25 deletions.
56 changes: 36 additions & 20 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,59 @@ package here

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

var dirCache = &infoMap{}

// Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) {
i := newInfo()
var err error
i, ok := dirCache.LoadOr(p, func(m *infoMap) (Info, bool) {
i := newInfo()

fi, err := os.Stat(p)
if err != nil {
return i, err
}
fi, err := os.Stat(p)
if err != nil {
return i, false
}

if !fi.IsDir() {
p = filepath.Dir(p)
}
if !fi.IsDir() {
p = filepath.Dir(p)
}

pwd, err := os.Getwd()
if err != nil {
return i, err
}
pwd, err := os.Getwd()
if err != nil {
return i, false
}

defer os.Chdir(pwd)

os.Chdir(p)

b, err := run("go", "list", "-json")
if err != nil {
return i, false
}

defer os.Chdir(pwd)
if err := json.Unmarshal(b, &i); err != nil {
return i, false
}

os.Chdir(p)
if err := setEnv(&i); err != nil {
return i, false
}
return i, true
})

b, err := run("go", "list", "-json")
if err != nil {
return i, err
}

if err := json.Unmarshal(b, &i); err != nil {
return i, err
if !ok {
return i, fmt.Errorf("an error occurred %s", p)
}

if err := setEnv(&i); err != nil {
return i, err
}
return i, nil
}
87 changes: 87 additions & 0 deletions info_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package here
import "encoding/json"

type Module struct {
Path string `json:"Path"`
Main bool `json:"Main"`
Dir string `json:"Dir"`
GoMod string `json:"GoMod"`
GoVersion string `json:"GoVersion"`
Path string `json:"Path"`
Main bool `json:"Main"`
Dir string `json:"Dir"`
GoMod string `json:"GoMod"`
GoVersion string `json:"GoVersion"`
}

func (i Module) String() string {
Expand Down

0 comments on commit d8dfaf2

Please sign in to comment.