Skip to content
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
13 changes: 7 additions & 6 deletions commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"

cmdError "github.com/coreos/operator-sdk/commands/operator-sdk/error"
"github.com/coreos/operator-sdk/pkg/generator"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,13 +41,13 @@ const (

func buildFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
ExitWithError(ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
}

bcmd := exec.Command(build)
o, err := bcmd.CombinedOutput()
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
}
fmt.Fprintln(os.Stdout, string(o))

Expand All @@ -55,19 +56,19 @@ func buildFunc(cmd *cobra.Command, args []string) {
dbcmd.Env = append(os.Environ(), fmt.Sprintf("IMAGE=%v", image))
o, err = dbcmd.CombinedOutput()
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
}
fmt.Fprintln(os.Stdout, string(o))

c := &generator.Config{}
fp, err := ioutil.ReadFile(configYaml)
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to read config file %v: (%v)", configYaml, err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to read config file %v: (%v)", configYaml, err))
}
if err = yaml.Unmarshal(fp, c); err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to unmarshal config file %v: (%v)", configYaml, err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to unmarshal config file %v: (%v)", configYaml, err))
}
if err = generator.RenderDeployFiles(c, image); err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to generate deploy/operator.yaml: (%v)", err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate deploy/operator.yaml: (%v)", err))
}
}
32 changes: 28 additions & 4 deletions commands/operator-sdk/cmd/generate/k8s.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package generate

import "github.com/spf13/cobra"
import (
"errors"
"fmt"
"os"
"os/exec"

cmdError "github.com/coreos/operator-sdk/commands/operator-sdk/error"

"github.com/spf13/cobra"
)

const (
k8sGenerated = "./tmp/codegen/update-generated.sh"
)

func NewGenerateK8SCmd() *cobra.Command {
return &cobra.Command{
Expand All @@ -9,8 +22,19 @@ func NewGenerateK8SCmd() *cobra.Command {
Long: `k8s generator generates code for custom resource given the API spec
to comply with kube-API requirements.
`,
Run: func(cmd *cobra.Command, args []string) {
panic("UNIMPLEMENTED")
},
Run: k8sFunc,
}
}

func k8sFunc(cmd *cobra.Command, args []string) {
if len(args) != 0 {
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("k8s command doesn't accept any arguments."))
}

kcmd := exec.Command(k8sGenerated)
o, err := kcmd.CombinedOutput()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to perform code-generation for CustomResources: (%v)", string(o)))
}
fmt.Fprintln(os.Stdout, string(o))
}
13 changes: 7 additions & 6 deletions commands/operator-sdk/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

cmdError "github.com/coreos/operator-sdk/commands/operator-sdk/error"
"github.com/coreos/operator-sdk/pkg/generator"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,37 +53,37 @@ const (

func newFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
ExitWithError(ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
}
parse(args)
verifyFlags()
g := generator.NewGenerator(apiVersion, kind, projectName, repoPath())
err := g.Render()
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to create project %v: %v", projectName, err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create project %v: %v", projectName, err))
}
}

func parse(args []string) {
projectName = args[0]
if len(projectName) == 0 {
ExitWithError(ExitBadArgs, fmt.Errorf("project-name must not be empty"))
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("project-name must not be empty"))
}
}

// repoPath checks if this project's repository path is rooted under $GOPATH and returns project's repository path.
func repoPath() string {
gp := os.Getenv(gopath)
if len(gp) == 0 {
ExitWithError(ExitError, fmt.Errorf("$GOPATH env not set"))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("$GOPATH env not set"))
}
wd, err := os.Getwd()
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
}
// check if this project's repository path is rooted under $GOPATH
if !strings.HasPrefix(wd, gp) {
ExitWithError(ExitError, fmt.Errorf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp))
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp))
}
// compute the repo path by stripping "$GOPATH/src/" from the path of the current directory.
rp := filepath.Join(string(wd[len(filepath.Join(gp, src)):]), projectName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package error

import (
"fmt"
Expand Down