Skip to content

Commit

Permalink
"glock sync -n" reads GLOCKFILE content from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
robfig committed Sep 14, 2014
1 parent e0d44e4 commit 3072357
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runCmd(_ *Command, args []string) {
var (
cmds = append(readCmds(importPath), cmd)
depRoots = calcDepRoots(importPath, cmds)
output = getOutput(importPath, *cmdN)
output = glockfileWriter(importPath, *cmdN)
)
outputCmds(output, cmds)
outputDeps(output, depRoots)
Expand Down
22 changes: 2 additions & 20 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,12 @@ func runSave(cmd *Command, args []string) {
depRoots = calcDepRoots(importPath, cmds)
)

output := getOutput(importPath, *saveN)
output := glockfileWriter(importPath, *saveN)
outputCmds(output, cmds)
outputDeps(output, depRoots)
output.Close()
}

func glockFilename(importPath string) string {
return path.Join(os.Getenv("GOPATH"), "src", importPath, "GLOCKFILE")
}

func getOutput(importPath string, n bool) io.WriteCloser {
if n {
return os.Stdout
}

var f, err = os.Create(glockFilename(importPath))
if err != nil {
perror(fmt.Errorf("error creating %s: %v", glockFilename, err))
}
return f
}

func outputCmds(w io.Writer, cmds []string) {
sort.Strings(cmds)
var prev string
Expand All @@ -82,9 +66,8 @@ func outputCmds(w io.Writer, cmds []string) {

func outputDeps(w io.Writer, depRoots []*repoRoot) {
for _, repoRoot := range depRoots {
// TODO: Work with multi-element gopaths
revision, err := repoRoot.vcs.head(
path.Join(os.Getenv("GOPATH"), "src", repoRoot.root),
path.Join(gopath(), "src", repoRoot.root),
repoRoot.repo)
if err != nil {
perror(err)
Expand Down Expand Up @@ -205,7 +188,6 @@ func run(name string, args ...string) ([]byte, error) {
fmt.Println(name, args)
}
var cmd = exec.Command(name, args...)
// cmd.Env = []string{"GOPATH=" + os.Getenv("GOPATH")}
return cmd.CombinedOutput()
}

Expand Down
38 changes: 19 additions & 19 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"

Expand All @@ -25,11 +22,17 @@ For example:
It verifies that each entry in the GLOCKFILE is at the expected revision.
If a dependency is not at the expected revision, it is re-downloaded and synced.
Commands are built if necessary.
Options:
-n read GLOCKFILE from stdin
`,
}

var (
color = flag.Bool("color", true, "if true, colorize terminal output")
syncColor = cmdSync.Flag.Bool("color", true, "if true, colorize terminal output")
syncN = cmdSync.Flag.Bool("n", false, "Read GLOCKFILE from stdin")

info = gocolorize.NewColor("green").Paint
warning = gocolorize.NewColor("yellow").Paint
Expand All @@ -43,26 +46,23 @@ func init() {
}

func runSync(cmd *Command, args []string) {
if len(args) == 0 {
if len(args) == 0 && !*syncN {
cmdSync.Usage()
return
}
if !*color {

var importPath string
if len(args) > 0 {
importPath = args[0]
}
var glockfile = glockfileReader(importPath, *syncN)
defer glockfile.Close()

if !*syncColor {
info = disabled
warning = disabled
critical = disabled
}
var importPath = args[0]
var repo, err = glockRepoRootForImportPath(importPath)
if err != nil {
perror(err)
}

var gopath = filepath.SplitList(build.Default.GOPATH)[0]
glockfile, err := os.Open(filepath.Join(gopath, "src", repo.root, "GLOCKFILE"))
if err != nil {
perror(err)
}

var cmds []string
var scanner = bufio.NewScanner(glockfile)
Expand All @@ -74,7 +74,7 @@ func runSync(cmd *Command, args []string) {
}

var importPath, expectedRevision = fields[0], truncate(fields[1])
var importDir = filepath.Join(gopath, "src", importPath)
var importDir = filepath.Join(gopath(), "src", importPath)

// Try to find the repo.
// go get it beforehand just in case it doesn't exist. (no-op if it does exist)
Expand All @@ -91,7 +91,7 @@ func runSync(cmd *Command, args []string) {
maybeGot = warning("get ")
}

actualRevision, err := repo.vcs.head(filepath.Join(gopath, "src", repo.root), repo.repo)
actualRevision, err := repo.vcs.head(filepath.Join(gopath(), "src", repo.root), repo.repo)
if err != nil {
fmt.Println("error determining revision of", repo.root, err)
continue
Expand Down
33 changes: 33 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"go/build"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -49,3 +50,35 @@ func glockRepoRootForImportPath(importPath string) (*repoRoot, error) {

return nil, fmt.Errorf("no version control directory found for %q", importPath)
}

func gopath() string {
return filepath.SplitList(build.Default.GOPATH)[0]
}

func glockFilename(importPath string) string {
return path.Join(gopath(), "src", importPath, "GLOCKFILE")
}

func glockfileReader(importPath string, n bool) io.ReadCloser {
if n {
return os.Stdin
}

var glockfile, err = os.Open(glockFilename(importPath))
if err != nil {
perror(err)
}
return glockfile
}

func glockfileWriter(importPath string, n bool) io.WriteCloser {
if n {
return os.Stdout
}

var f, err = os.Create(glockFilename(importPath))
if err != nil {
perror(fmt.Errorf("error creating %s: %v", glockFilename, err))
}
return f
}

0 comments on commit 3072357

Please sign in to comment.