Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands/operator-sdk: pull deps after code generation #81

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions commands/operator-sdk/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -47,8 +48,10 @@ var (
)

const (
gopath = "GOPATH"
src = "src"
gopath = "GOPATH"
src = "src"
dep = "dep"
ensureCmd = "ensure"
)

func newFunc(cmd *cobra.Command, args []string) {
Expand All @@ -62,6 +65,7 @@ func newFunc(cmd *cobra.Command, args []string) {
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create project %v: %v", projectName, err))
}
pullDep()
}

func parse(args []string) {
Expand All @@ -77,10 +81,7 @@ func repoPath() string {
if len(gp) == 0 {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("$GOPATH env not set"))
}
wd, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
}
wd := mustGetwd()
// check if this project's repository path is rooted under $GOPATH
if !strings.HasPrefix(wd, gp) {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp))
Expand All @@ -94,3 +95,21 @@ func repoPath() string {
func verifyFlags() {
// TODO: verify format of input flags.
}

func pullDep() {
dc := exec.Command(dep, ensureCmd)
dc.Dir = filepath.Join(mustGetwd(), projectName)
o, err := dc.CombinedOutput()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to ensure dependencies: (%v)", string(o)))
}
fmt.Fprintln(os.Stdout, string(o))
}

func mustGetwd() string {
wd, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
}
return wd
}
10 changes: 3 additions & 7 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (g *Generator) Render() error {
if err := g.renderTmp(); err != nil {
return err
}
return g.pullDep()
return g.renderGoDep()
}

func (g *Generator) pullDep() error {
func (g *Generator) renderGoDep() error {
buf := &bytes.Buffer{}
if err := renderGopkgTomlFile(buf); err != nil {
return err
Expand All @@ -103,11 +103,7 @@ func (g *Generator) pullDep() error {
if err := renderGopkgLockFile(buf); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(g.projectName, gopkglock), buf.Bytes(), defaultFileMode); err != nil {
return err
}
// TODO: `dep ensure`
return nil
return ioutil.WriteFile(filepath.Join(g.projectName, gopkglock), buf.Bytes(), defaultFileMode)
}

func (g *Generator) renderCmd() error {
Expand Down