Skip to content

Commit

Permalink
Merge pull request #250 from eparis/init-git
Browse files Browse the repository at this point in the history
commands/operator-sdk/cmd/new: initialize git on `new`
  • Loading branch information
hasbro17 committed May 18, 2018
2 parents 49a0718 + e9ff180 commit 8a1704d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
38 changes: 28 additions & 10 deletions commands/operator-sdk/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ generates a skeletal app-operator application in $GOPATH/src/github.com/example.
newCmd.MarkFlagRequired("api-version")
newCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)")
newCmd.MarkFlagRequired("kind")
newCmd.Flags().BoolVar(&skipGit, "skip-git-init", false, "Do not init the directory as a git repository")

return newCmd
}
Expand All @@ -61,6 +62,7 @@ var (
apiVersion string
kind string
projectName string
skipGit bool
)

const (
Expand All @@ -84,6 +86,7 @@ func newFunc(cmd *cobra.Command, args []string) {
}
pullDep()
generate.K8sCodegen(projectName)
initGit()
}

func parse(args []string) {
Expand Down Expand Up @@ -138,23 +141,38 @@ func verifyFlags() {
}
}

func pullDep() {
fmt.Fprintln(os.Stdout, "Run dep ensure ...")
dc := exec.Command(dep, ensureCmd, "-v")
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
}

func execCmd(stdout *os.File, cmd string, args ...string) {
dc := exec.Command(cmd, args...)
dc.Dir = filepath.Join(mustGetwd(), projectName)
dc.Stdout = os.Stdout
dc.Stdout = stdout
dc.Stderr = os.Stderr
err := dc.Run()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to ensure dependencies: %v", err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to exec %s %#v: %v", cmd, args, err))
}
}

func pullDep() {
fmt.Fprintln(os.Stdout, "Run dep ensure ...")
execCmd(os.Stdout, dep, ensureCmd, "-v")
fmt.Fprintln(os.Stdout, "Run dep ensure done")
}

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))
func initGit() {
if skipGit {
return
}
return wd
fmt.Fprintln(os.Stdout, "Run git init ...")
execCmd(os.Stdout, "git", "init")
execCmd(os.Stdout, "git", "add", "--all")
execCmd(nil, "git", "commit", "-m", "INITIAL COMMIT")
fmt.Fprintln(os.Stdout, "Run git init done")
}
4 changes: 3 additions & 1 deletion doc/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This guide walks through an example of building a simple memcached-operator usin
## Prerequisites

- [dep][dep_tool] version v0.4.1+.
- [git][git_tool]
- [go][go_tool] version v1.10+.
- [docker][docker_tool] version 17.03+.
- [kubectl][kubectl_tool] version v1.9.0+.
Expand Down Expand Up @@ -208,7 +209,8 @@ $ kubectl delete -f deploy/operator.yaml
[memcached_handler]: ../example/memcached-operator/handler.go.tmpl
[layout_doc]:./project_layout.md
[dep_tool]:https://golang.github.io/dep/docs/installation.html
[git_tool]:https://git-scm.com/downloads
[go_tool]:https://golang.org/dl/
[docker_tool]:https://docs.docker.com/install/
[kubectl_tool]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
[minikube_tool]:https://github.com/kubernetes/minikube#installation
[minikube_tool]:https://github.com/kubernetes/minikube#installation

0 comments on commit 8a1704d

Please sign in to comment.