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/cmd/new: initialize git on new #250

Merged
merged 1 commit into from
May 18, 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
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