From e9ff180ad8e1571f2529043cb8af651c67764325 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Tue, 15 May 2018 11:05:55 -0400 Subject: [PATCH] commands/operator-sdk/cmd/new: initialize git on `new` I love that `cargo new` from the rust project initializes git after it creates my stock files. I don't have to run that command myself. Make `operator-sdk new` just call git init. --- commands/operator-sdk/cmd/new.go | 38 +++++++++++++++++++++++--------- doc/user-guide.md | 4 +++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/commands/operator-sdk/cmd/new.go b/commands/operator-sdk/cmd/new.go index 7dcf1f1106a..80b744a1944 100644 --- a/commands/operator-sdk/cmd/new.go +++ b/commands/operator-sdk/cmd/new.go @@ -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 } @@ -61,6 +62,7 @@ var ( apiVersion string kind string projectName string + skipGit bool ) const ( @@ -84,6 +86,7 @@ func newFunc(cmd *cobra.Command, args []string) { } pullDep() generate.K8sCodegen(projectName) + initGit() } func parse(args []string) { @@ -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") } diff --git a/doc/user-guide.md b/doc/user-guide.md index 824a4274836..22fa575c392 100644 --- a/doc/user-guide.md +++ b/doc/user-guide.md @@ -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+. @@ -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 \ No newline at end of file +[minikube_tool]:https://github.com/kubernetes/minikube#installation