Skip to content

Commit 84ab8b4

Browse files
authored
'operator-sdk print-deps' command (#772)
* commands/operator-sdk/cmd/print_deps.go: 'operator-sdk print-deps' command pkg/scaffold/gopkgtoml.go: printing utilities for print-deps
1 parent 6456140 commit 84ab8b4

File tree

6 files changed

+163
-3
lines changed

6 files changed

+163
-3
lines changed

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
19+
20+
log "github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var asFile bool
25+
26+
func NewPrintDepsCmd() *cobra.Command {
27+
printDepsCmd := &cobra.Command{
28+
Use: "print-deps",
29+
Short: "Print Golang packages and versions required to run the operator",
30+
Long: `The operator-sdk print-deps command prints all Golang packages and versions expected
31+
by this version of the Operator SDK. Versions for these packages should match
32+
those in an operators' Gopkg.toml file.
33+
34+
print-deps prints in columnar format by default. Use the --as-file flag to
35+
print in Gopkg.toml file format.
36+
`,
37+
Run: printDepsFunc,
38+
}
39+
40+
printDepsCmd.Flags().BoolVar(&asFile, "as-file", false, "Print dependencies in Gopkg.toml file format.")
41+
42+
return printDepsCmd
43+
}
44+
45+
func printDepsFunc(cmd *cobra.Command, args []string) {
46+
if len(args) != 0 {
47+
log.Fatal("print-deps command does not take any arguments")
48+
}
49+
if asFile {
50+
scaffold.PrintDepsAsFile()
51+
} else {
52+
if err := scaffold.PrintDeps(); err != nil {
53+
log.Fatalf("print deps: (%v)", err)
54+
}
55+
}
56+
}

commands/operator-sdk/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func NewRootCmd() *cobra.Command {
3434
cmd.AddCommand(NewUpCmd())
3535
cmd.AddCommand(NewCompletionCmd())
3636
cmd.AddCommand(NewTestCmd())
37+
cmd.AddCommand(NewPrintDepsCmd())
3738

3839
return cmd
3940
}

doc/sdk-cli-reference.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,17 @@ operator-sdk completion bash
8888
# ex: ts=4 sw=4 et filetype=sh
8989
```
9090

91+
## print-deps
92+
93+
Prints the most recent Golang packages and versions required by operators. Prints in columnar format by default.
94+
95+
### Flags
96+
97+
* `--as-file` Print packages and versions in Gopkg.toml format.
98+
9199
## generate
92100

93-
### k8s
101+
### k8s
94102

95103
Runs the Kubernetes [code-generators][k8s-code-generator] for all Custom Resource Definitions (CRD) apis under `pkg/apis/...`.
96104
Currently only runs `deepcopy-gen` to generate the required `DeepCopy()` functions for all custom resource types.

pkg/scaffold/gopkgtoml.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
package scaffold
1616

1717
import (
18+
"bytes"
19+
"encoding/json"
20+
"errors"
21+
"fmt"
22+
"strings"
23+
"text/tabwriter"
24+
1825
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
26+
27+
"github.com/BurntSushi/toml"
1928
)
2029

2130
const GopkgTomlFile = "Gopkg.toml"
@@ -82,8 +91,85 @@ required = [
8291
[prune]
8392
go-tests = true
8493
non-go = true
85-
94+
8695
[[prune.project]]
8796
name = "k8s.io/code-generator"
8897
non-go = false
8998
`
99+
100+
func PrintDepsAsFile() {
101+
fmt.Println(gopkgTomlTmpl)
102+
}
103+
104+
func PrintDeps() error {
105+
gopkgData := make(map[string]interface{})
106+
_, err := toml.Decode(gopkgTomlTmpl, &gopkgData)
107+
if err != nil {
108+
return err
109+
}
110+
111+
buf := &bytes.Buffer{}
112+
w := tabwriter.NewWriter(buf, 16, 8, 0, '\t', 0)
113+
_, err = w.Write([]byte("NAME\tVERSION\tBRANCH\tREVISION\t\n"))
114+
if err != nil {
115+
return err
116+
}
117+
118+
constraintList, ok := gopkgData["constraint"]
119+
if !ok {
120+
return errors.New("constraints not found")
121+
}
122+
for _, dep := range constraintList.([]map[string]interface{}) {
123+
err = writeDepRow(w, dep)
124+
if err != nil {
125+
return err
126+
}
127+
}
128+
overrideList, ok := gopkgData["override"]
129+
if !ok {
130+
return errors.New("overrides not found")
131+
}
132+
for _, dep := range overrideList.([]map[string]interface{}) {
133+
err = writeDepRow(w, dep)
134+
if err != nil {
135+
return err
136+
}
137+
}
138+
if err := w.Flush(); err != nil {
139+
return err
140+
}
141+
142+
requiredList, ok := gopkgData["required"]
143+
if !ok {
144+
return errors.New("required list not found")
145+
}
146+
pl, err := json.MarshalIndent(requiredList, "", " ")
147+
if err != nil {
148+
return err
149+
}
150+
_, err = buf.Write([]byte(fmt.Sprintf("\nrequired = %v", string(pl))))
151+
if err != nil {
152+
return err
153+
}
154+
155+
fmt.Println(buf.String())
156+
157+
return nil
158+
}
159+
160+
func writeDepRow(w *tabwriter.Writer, dep map[string]interface{}) error {
161+
name := dep["name"].(string)
162+
ver, col := "", 0
163+
if v, ok := dep["version"]; ok {
164+
ver, col = v.(string), 1
165+
} else if v, ok = dep["branch"]; ok {
166+
ver, col = v.(string), 2
167+
} else if v, ok = dep["revision"]; ok {
168+
ver, col = v.(string), 3
169+
} else {
170+
return fmt.Errorf("no version, revision, or branch found for %s", name)
171+
}
172+
173+
_, err := w.Write([]byte(name + strings.Repeat("\t", col) + ver + "\t\n"))
174+
return err
175+
}

pkg/scaffold/gopkgtoml_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ required = [
8383
[prune]
8484
go-tests = true
8585
non-go = true
86-
86+
8787
[[prune.project]]
8888
name = "k8s.io/code-generator"
8989
non-go = false

0 commit comments

Comments
 (0)