Skip to content

Commit dc75adc

Browse files
committed
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 dc75adc

File tree

5 files changed

+138
-2
lines changed

5 files changed

+138
-2
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
func NewPrintDepsCmd() *cobra.Command {
25+
printDepsCmd := &cobra.Command{
26+
Use: "print-deps",
27+
Short: "Print dependencies expected by the Operator SDK",
28+
Long: `The operator-sdk print-deps command prints all dependencies expected by this
29+
version of the Operator SDK. Versions for these dependencies should match those
30+
in an operators' Gopkg.toml file.`,
31+
Run: printDepsFunc,
32+
}
33+
34+
return printDepsCmd
35+
}
36+
37+
func printDepsFunc(cmd *cobra.Command, args []string) {
38+
if len(args) != 0 {
39+
log.Fatal("print-deps command does not take any arguments")
40+
}
41+
if err := scaffold.PrintGopkgDeps(); err != nil {
42+
log.Fatalf("print deps: (%v)", err)
43+
}
44+
}

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
}

pkg/scaffold/gopkgtoml.go

Lines changed: 83 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,81 @@ 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 PrintGopkgDeps() error {
101+
gopkgData := make(map[string]interface{})
102+
_, err := toml.Decode(gopkgTomlTmpl, &gopkgData)
103+
if err != nil {
104+
return err
105+
}
106+
107+
buf := &bytes.Buffer{}
108+
w := tabwriter.NewWriter(buf, 16, 8, 0, '\t', 0)
109+
_, err = w.Write([]byte("NAME\tVERSION\tBRANCH\tREVISION\n"))
110+
if err != nil {
111+
return err
112+
}
113+
114+
constraintList, ok := gopkgData["constraint"]
115+
if !ok {
116+
return errors.New("constraints not found")
117+
}
118+
for _, dep := range constraintList.([]map[string]interface{}) {
119+
err = writeDepRow(w, dep)
120+
if err != nil {
121+
return err
122+
}
123+
}
124+
overrideList, ok := gopkgData["override"]
125+
if !ok {
126+
return errors.New("overrides not found")
127+
}
128+
for _, dep := range overrideList.([]map[string]interface{}) {
129+
err = writeDepRow(w, dep)
130+
if err != nil {
131+
return err
132+
}
133+
}
134+
if err := w.Flush(); err != nil {
135+
return err
136+
}
137+
138+
requiredList, ok := gopkgData["required"]
139+
if !ok {
140+
return errors.New("required list not found")
141+
}
142+
pl, err := json.MarshalIndent(requiredList, "", " ")
143+
if err != nil {
144+
return err
145+
}
146+
_, err = buf.Write([]byte(fmt.Sprintf("\nrequired = %v", string(pl))))
147+
if err != nil {
148+
return err
149+
}
150+
151+
fmt.Println(buf.String())
152+
153+
return nil
154+
}
155+
156+
func writeDepRow(w *tabwriter.Writer, dep map[string]interface{}) error {
157+
name := dep["name"].(string)
158+
pkg, col := "", 0
159+
if v, ok := dep["version"]; ok {
160+
pkg, col = v.(string), 1
161+
} else if v, ok = dep["branch"]; ok {
162+
pkg, col = v.(string), 2
163+
} else if v, ok = dep["revision"]; ok {
164+
pkg, col = v.(string), 3
165+
} else {
166+
return fmt.Errorf("no version, revision, or branch found for %s", name)
167+
}
168+
169+
_, err := w.Write([]byte(name + strings.Repeat("\t", col) + pkg + "\n"))
170+
return err
171+
}

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)