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

'operator-sdk print-deps' command #772

Merged
merged 6 commits into from
Nov 26, 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
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions commands/operator-sdk/cmd/print_deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/operator-framework/operator-sdk/pkg/scaffold"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var asFile bool

func NewPrintDepsCmd() *cobra.Command {
printDepsCmd := &cobra.Command{
Use: "print-deps",
Short: "Print Golang packages and versions required to run the operator",
Long: `The operator-sdk print-deps command prints all Golang packages and versions expected
by this version of the Operator SDK. Versions for these packages should match
those in an operators' Gopkg.toml file.

print-deps prints in columnar format by default. Use the --as-file flag to
print in Gopkg.toml file format.
`,
Run: printDepsFunc,
}

printDepsCmd.Flags().BoolVar(&asFile, "as-file", false, "Print dependencies in Gopkg.toml file format.")

return printDepsCmd
}

func printDepsFunc(cmd *cobra.Command, args []string) {
if len(args) != 0 {
log.Fatal("print-deps command does not take any arguments")
}
if asFile {
scaffold.PrintDepsAsFile()
} else {
if err := scaffold.PrintDeps(); err != nil {
log.Fatalf("print deps: (%v)", err)
}
}
}
1 change: 1 addition & 0 deletions commands/operator-sdk/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(NewUpCmd())
cmd.AddCommand(NewCompletionCmd())
cmd.AddCommand(NewTestCmd())
cmd.AddCommand(NewPrintDepsCmd())

return cmd
}
10 changes: 9 additions & 1 deletion doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,17 @@ operator-sdk completion bash
# ex: ts=4 sw=4 et filetype=sh
```

## print-deps

Prints the most recent Golang packages and versions required by operators. Prints in columnar format by default.

### Flags

* `--as-file` Print packages and versions in Gopkg.toml format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@estroz Sorry for the late review but it might be good to have a truncated example output here for a particular version, as we do for other commands.
We can show the --as-file output.

## generate

### k8s
### k8s

Runs the Kubernetes [code-generators][k8s-code-generator] for all Custom Resource Definitions (CRD) apis under `pkg/apis/...`.
Currently only runs `deepcopy-gen` to generate the required `DeepCopy()` functions for all custom resource types.
Expand Down
88 changes: 87 additions & 1 deletion pkg/scaffold/gopkgtoml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
package scaffold

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"text/tabwriter"

"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

"github.com/BurntSushi/toml"
)

const GopkgTomlFile = "Gopkg.toml"
Expand Down Expand Up @@ -82,8 +91,85 @@ required = [
[prune]
go-tests = true
non-go = true

[[prune.project]]
name = "k8s.io/code-generator"
non-go = false
`

func PrintDepsAsFile() {
fmt.Println(gopkgTomlTmpl)
}

func PrintDeps() error {
gopkgData := make(map[string]interface{})
_, err := toml.Decode(gopkgTomlTmpl, &gopkgData)
if err != nil {
return err
}

buf := &bytes.Buffer{}
w := tabwriter.NewWriter(buf, 16, 8, 0, '\t', 0)
_, err = w.Write([]byte("NAME\tVERSION\tBRANCH\tREVISION\t\n"))
if err != nil {
return err
}

constraintList, ok := gopkgData["constraint"]
if !ok {
return errors.New("constraints not found")
}
for _, dep := range constraintList.([]map[string]interface{}) {
err = writeDepRow(w, dep)
if err != nil {
return err
}
}
overrideList, ok := gopkgData["override"]
if !ok {
return errors.New("overrides not found")
}
for _, dep := range overrideList.([]map[string]interface{}) {
err = writeDepRow(w, dep)
if err != nil {
return err
}
}
if err := w.Flush(); err != nil {
return err
}

requiredList, ok := gopkgData["required"]
if !ok {
return errors.New("required list not found")
}
pl, err := json.MarshalIndent(requiredList, "", " ")
if err != nil {
return err
}
_, err = buf.Write([]byte(fmt.Sprintf("\nrequired = %v", string(pl))))
if err != nil {
return err
}

fmt.Println(buf.String())

return nil
}

func writeDepRow(w *tabwriter.Writer, dep map[string]interface{}) error {
name := dep["name"].(string)
ver, col := "", 0
if v, ok := dep["version"]; ok {
ver, col = v.(string), 1
} else if v, ok = dep["branch"]; ok {
ver, col = v.(string), 2
} else if v, ok = dep["revision"]; ok {
ver, col = v.(string), 3
} else {
return fmt.Errorf("no version, revision, or branch found for %s", name)
}

_, err := w.Write([]byte(name + strings.Repeat("\t", col) + ver + "\t\n"))
return err
}
2 changes: 1 addition & 1 deletion pkg/scaffold/gopkgtoml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ required = [
[prune]
go-tests = true
non-go = true

[[prune.project]]
name = "k8s.io/code-generator"
non-go = false
Expand Down