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

Commit

Permalink
Use interfaces between main and dep packages
Browse files Browse the repository at this point in the history
  • Loading branch information
spenczar committed Mar 28, 2017
1 parent aaf8a8b commit 7bb4c77
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 25 deletions.
14 changes: 9 additions & 5 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type ensureCommand struct {
overrides stringSlice
}

func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
func (cmd *ensureCommand) Run(ctx dep.BuildContext, args []string) error {
if cmd.examples {
fmt.Fprintln(os.Stderr, strings.TrimSpace(ensureExamples))
return nil
Expand All @@ -115,8 +115,12 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
sm.UseDefaultSignalHandling()
defer sm.Release()
if sr, ok := sm.(signalReceiver); ok {
useDefaultSignalHandling(sr)
}
if r, ok := sm.(releaser); ok {
defer r.Release()
}

params := p.MakeParams()
if *verbose {
Expand Down Expand Up @@ -186,7 +190,7 @@ func applyUpdateArgs(args []string, params *gps.SolveParameters) {
}
}

func applyEnsureArgs(args []string, overrides stringSlice, p *dep.Project, sm *gps.SourceMgr, params *gps.SolveParameters) error {
func applyEnsureArgs(args []string, overrides stringSlice, p *dep.Project, sm gps.SourceManager, params *gps.SolveParameters) error {
var errs []error
for _, arg := range args {
pc, err := getProjectConstraint(arg, sm)
Expand Down Expand Up @@ -259,7 +263,7 @@ func (s *stringSlice) Set(value string) error {
return nil
}

func getProjectConstraint(arg string, sm *gps.SourceMgr) (gps.ProjectConstraint, error) {
func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstraint, error) {
constraint := gps.ProjectConstraint{
Constraint: gps.Any(), // default to any; avoids panics later
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/dep/hash_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (cmd *hashinCommand) Register(fs *flag.FlagSet) {}

type hashinCommand struct{}

func (hashinCommand) Run(ctx *dep.Ctx, args []string) error {
func (hashinCommand) Run(ctx dep.BuildContext, args []string) error {
p, err := ctx.LoadProject("")
if err != nil {
return err
Expand All @@ -33,8 +33,12 @@ func (hashinCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
sm.UseDefaultSignalHandling()
defer sm.Release()
if sr, ok := sm.(signalReceiver); ok {
useDefaultSignalHandling(sr)
}
if r, ok := sm.(releaser); ok {
defer r.Release()
}

params := p.MakeParams()
cpr, err := ctx.SplitAbsoluteProjectRoot(p.AbsRoot)
Expand Down
16 changes: 10 additions & 6 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func trimPathPrefix(p1, p2 string) string {
return p1
}

func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
func (cmd *initCommand) Run(ctx dep.BuildContext, args []string) error {
if len(args) > 1 {
return errors.Errorf("too many args (%d)", len(args))
}
Expand Down Expand Up @@ -98,8 +98,12 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return errors.Wrap(err, "getSourceManager")
}
sm.UseDefaultSignalHandling()
defer sm.Release()
if sr, ok := sm.(signalReceiver); ok {
useDefaultSignalHandling(sr)
}
if r, ok := sm.(releaser); ok {
defer r.Release()
}

pd, err := getProjectData(ctx, pkgT, cpr, sm)
if err != nil {
Expand Down Expand Up @@ -214,14 +218,14 @@ type projectData struct {
ondisk map[gps.ProjectRoot]gps.Version // projects that were found on disk
}

func getProjectData(ctx *dep.Ctx, pkgT gps.PackageTree, cpr string, sm *gps.SourceMgr) (projectData, error) {
func getProjectData(ctx dep.BuildContext, pkgT gps.PackageTree, cpr string, sm gps.SourceManager) (projectData, error) {
constraints := make(gps.ProjectConstraints)
dependencies := make(map[gps.ProjectRoot][]string)
packages := make(map[string]bool)
notondisk := make(map[gps.ProjectRoot]bool)
ondisk := make(map[gps.ProjectRoot]gps.Version)

syncDep := func(pr gps.ProjectRoot, sm *gps.SourceMgr) {
syncDep := func(pr gps.ProjectRoot, sm gps.SourceManager) {
message := "Cached"
if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil {
message = "Unable to cache"
Expand Down Expand Up @@ -313,7 +317,7 @@ func getProjectData(ctx *dep.Ctx, pkgT gps.PackageTree, cpr string, sm *gps.Sour
// It's fine if the root does not exist - it indicates that this
// project is not present in the workspace, and so we need to
// solve to deal with this dep.
r := filepath.Join(ctx.GOPATH, "src", string(pr))
r := filepath.Join(ctx.Root(), "src", string(pr))
fi, err := os.Stat(r)
if os.IsNotExist(err) || !fi.IsDir() {
colors[pkg] = black
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type command interface {
LongHelp() string // "Foo the first bar meeting the following conditions..."
Register(*flag.FlagSet) // command-specific flags
Hidden() bool // indicates whether the command should be hidden from help output
Run(*dep.Ctx, []string) error
Run(dep.BuildContext, []string) error
}

func main() {
Expand Down
10 changes: 7 additions & 3 deletions cmd/dep/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type removeCommand struct {
keepSource bool
}

func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
func (cmd *removeCommand) Run(ctx dep.BuildContext, args []string) error {
p, err := ctx.LoadProject("")
if err != nil {
return err
Expand All @@ -53,8 +53,12 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
sm.UseDefaultSignalHandling()
defer sm.Release()
if sr, ok := sm.(signalReceiver); ok {
useDefaultSignalHandling(sr)
}
if r, ok := sm.(releaser); ok {
defer r.Release()
}

cpr, err := ctx.SplitAbsoluteProjectRoot(p.AbsRoot)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions cmd/dep/signals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"os"
"os/signal"
)

// A signalReceiver takes action in response to os.Signals.
type signalReceiver interface {
HandleSignals(chan os.Signal)
}

// useDefaultSignalHandling sets up typical os.Interrupt signal handling.
func useDefaultSignalHandling(r signalReceiver) {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
r.HandleSignals(sigch)
}

// A releaser has a Release() method to let go of any locks.
type releaser interface {
Release()
}
14 changes: 9 additions & 5 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (out *jsonOutput) MissingFooter() {
json.NewEncoder(out.w).Encode(out.missing)
}

func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
func (cmd *statusCommand) Run(ctx dep.BuildContext, args []string) error {
p, err := ctx.LoadProject("")
if err != nil {
return err
Expand All @@ -160,8 +160,12 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
sm.UseDefaultSignalHandling()
defer sm.Release()
if sr, ok := sm.(signalReceiver); ok {
useDefaultSignalHandling(sr)
}
if r, ok := sm.(releaser); ok {
defer r.Release()
}

var out outputter
switch {
Expand Down Expand Up @@ -195,7 +199,7 @@ type MissingStatus struct {
MissingPackages []string
}

func runStatusAll(out outputter, p *dep.Project, sm *gps.SourceMgr) error {
func runStatusAll(out outputter, p *dep.Project, sm gps.SourceManager) error {
if p.Lock == nil {
// TODO if we have no lock file, do...other stuff
return nil
Expand Down Expand Up @@ -368,7 +372,7 @@ func formatVersion(v gps.Version) string {
return v.String()
}

func collectConstraints(ptree gps.PackageTree, p *dep.Project, sm *gps.SourceMgr) map[string][]gps.Constraint {
func collectConstraints(ptree gps.PackageTree, p *dep.Project, sm gps.SourceManager) map[string][]gps.Constraint {
// TODO
return map[string][]gps.Constraint{}
}
33 changes: 31 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,41 @@ import (
"github.com/sdboyer/gps"
)

type BuildContext interface {
SourceManager() (gps.SourceManager, error)

// LoadProject takes a path and searches up the directory tree for a project
// root. If an absolute path is given, the search begins in that directory. If
// a relative or empty path is given, the search start is computed from the
// current working directory. The search stops when a file with the name
// ManifestName (manifest.json, by default) is located.
//
// The Project contains the parsed manifest as well as a parsed lock file, if
// present. The import path is calculated as the remaining path segment below
// Ctx.GOPATH/src.
LoadProject(path string) (*Project, error)

// SplitAbsoluteProjectRoot takes an absolute path and compares it against
// declared GOPATH(s) to determine what portion of the input path should be
// treated as an import path - as a project root.
//
// The second returned string indicates which GOPATH value was used.
SplitAbsoluteProjectRoot(path string) (string, error)

VersionInWorkspace(root gps.ProjectRoot) (gps.Version, error)

// Returns the root directory where packages can be found.
Root() string
}

// Ctx defines the supporting context of the tool.
type Ctx struct {
GOPATH string // Go path
}

// NewContext creates a struct with the project's GOPATH. It assumes
// that of your "GOPATH"'s we want the one we are currently in.
func NewContext() (*Ctx, error) {
func NewContext() (BuildContext, error) {
// this way we get the default GOPATH that was added in 1.8
buildContext := build.Default
wd, err := os.Getwd()
Expand All @@ -41,7 +68,7 @@ func NewContext() (*Ctx, error) {
return nil, errors.New("project not in a GOPATH")
}

func (c *Ctx) SourceManager() (*gps.SourceMgr, error) {
func (c *Ctx) SourceManager() (gps.SourceManager, error) {
return gps.NewSourceManager(analyzer{}, filepath.Join(c.GOPATH, "pkg", "dep"))
}

Expand Down Expand Up @@ -207,3 +234,5 @@ func contains(a []string, b string) bool {
}
return false
}

func (ctx *Ctx) Root() string { return ctx.GOPATH }

0 comments on commit 7bb4c77

Please sign in to comment.