Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit e5d06dd

Browse files
committed
Describe and list commands added
1 parent 311c68a commit e5d06dd

17 files changed

Lines changed: 387 additions & 69 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ Working name is `tmcli`.
77

88
## Available commands and scenarios
99

10+
Commands without the context:
11+
12+
```
13+
tmcli config *
14+
tmcli list
15+
tmcli create broker <broker>
16+
```
17+
18+
Commands with optional context:
19+
20+
```
21+
tmcli dump [broker]
22+
tmcli describe [broker]
23+
tmcli start [broker]
24+
tmcli stop [broker]
25+
tmcli watch [broker]
26+
```
27+
28+
Commands with context from config:
29+
30+
```
31+
tmcli create source *
32+
tmcli create target *
33+
tmcli create transformation *
34+
```
35+
1036
### Installation
1137

1238
Checkout the code:

cmd/cmd.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ import (
2727
"github.com/triggermesh/tmcli/pkg/docker"
2828
"github.com/triggermesh/tmcli/pkg/triggermesh/crd"
2929

30-
configcmd "github.com/triggermesh/tmcli/cmd/config"
31-
createcmd "github.com/triggermesh/tmcli/cmd/create"
32-
dumpcmd "github.com/triggermesh/tmcli/cmd/dump"
33-
startcmd "github.com/triggermesh/tmcli/cmd/start"
34-
stopcmd "github.com/triggermesh/tmcli/cmd/stop"
35-
watchcmd "github.com/triggermesh/tmcli/cmd/watch"
30+
"github.com/triggermesh/tmcli/cmd/config"
31+
"github.com/triggermesh/tmcli/cmd/create"
32+
"github.com/triggermesh/tmcli/cmd/describe"
33+
"github.com/triggermesh/tmcli/cmd/dump"
34+
"github.com/triggermesh/tmcli/cmd/list"
35+
"github.com/triggermesh/tmcli/cmd/start"
36+
"github.com/triggermesh/tmcli/cmd/stop"
37+
"github.com/triggermesh/tmcli/cmd/watch"
3638
)
3739

3840
var (
@@ -77,12 +79,14 @@ func NewRootCommand() *cobra.Command {
7779
viper.BindPFlag("triggermesh.version", rootCmd.PersistentFlags().Lookup("version"))
7880

7981
// commands
80-
rootCmd.AddCommand(createcmd.NewCmd())
81-
rootCmd.AddCommand(configcmd.NewCmd())
82-
rootCmd.AddCommand(dumpcmd.NewCmd())
83-
rootCmd.AddCommand(startcmd.NewCmd())
84-
rootCmd.AddCommand(stopcmd.NewCmd())
85-
rootCmd.AddCommand(watchcmd.NewCmd())
82+
rootCmd.AddCommand(create.NewCmd())
83+
rootCmd.AddCommand(config.NewCmd())
84+
rootCmd.AddCommand(dump.NewCmd())
85+
rootCmd.AddCommand(describe.NewCmd())
86+
rootCmd.AddCommand(list.NewCmd())
87+
rootCmd.AddCommand(start.NewCmd())
88+
rootCmd.AddCommand(stop.NewCmd())
89+
rootCmd.AddCommand(watch.NewCmd())
8690

8791
return rootCmd
8892
}

cmd/create/cmd.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ func NewCmd() *cobra.Command {
5454
createCmd.AddCommand(o.NewTargetCmd())
5555
createCmd.AddCommand(o.NewTransformationCmd())
5656

57-
// createCmd.Flags().StringVarP(&o.Context, "broker", "b", "", "Connect components to this broker")
58-
5957
return createCmd
6058
}
6159

cmd/create/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (o *CreateOptions) target(name, kind string, args []string, eventSourceFilt
8080
}
8181
producer, ok := c.(triggermesh.Producer)
8282
if !ok {
83-
return fmt.Errorf("%q is not an event producer", eventSourceFilter)
83+
return fmt.Errorf("event producer %q is not available", eventSourceFilter)
8484
}
8585
et, err := producer.GetEventTypes()
8686
if err != nil {

cmd/create/transformation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (o *CreateOptions) transformation(name, eventSourceFilter string, eventType
9797
}
9898
producer, ok := c.(triggermesh.Producer)
9999
if !ok {
100-
return fmt.Errorf("%q is not an event producer", eventSourceFilter)
100+
return fmt.Errorf("event producer %q is not available", eventSourceFilter)
101101
}
102102
et, err := producer.GetEventTypes()
103103
if err != nil {

cmd/describe/describe.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright 2022 TriggerMesh Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package describe
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"path"
23+
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
27+
"github.com/triggermesh/tmcli/pkg/docker"
28+
"github.com/triggermesh/tmcli/pkg/manifest"
29+
"github.com/triggermesh/tmcli/pkg/output"
30+
"github.com/triggermesh/tmcli/pkg/triggermesh"
31+
tmbroker "github.com/triggermesh/tmcli/pkg/triggermesh/broker"
32+
"github.com/triggermesh/tmcli/pkg/triggermesh/source"
33+
"github.com/triggermesh/tmcli/pkg/triggermesh/target"
34+
"github.com/triggermesh/tmcli/pkg/triggermesh/transformation"
35+
)
36+
37+
const manifestFile = "manifest.yaml"
38+
39+
type integration struct {
40+
Broker component
41+
Source component
42+
Transformation component
43+
Target component
44+
}
45+
46+
type component struct {
47+
object []triggermesh.Component
48+
container []*docker.Container
49+
}
50+
51+
type DescribeOptions struct {
52+
ConfigDir string
53+
CRD string
54+
Version string
55+
}
56+
57+
func NewCmd() *cobra.Command {
58+
o := &DescribeOptions{}
59+
return &cobra.Command{
60+
Use: "describe <broker>",
61+
Short: "Show broker status",
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
broker := viper.GetString("context")
64+
if len(args) == 1 {
65+
broker = args[0]
66+
}
67+
configDir, err := cmd.Flags().GetString("config")
68+
if err != nil {
69+
return err
70+
}
71+
o.ConfigDir = configDir
72+
o.Version = viper.GetString("triggermesh.version")
73+
o.CRD = viper.GetString("triggermesh.servedCRD")
74+
return o.describe(broker)
75+
},
76+
}
77+
}
78+
79+
func (o DescribeOptions) describe(broker string) error {
80+
ctx := context.Background()
81+
configDir := path.Join(o.ConfigDir, broker)
82+
manifestFile := path.Join(configDir, manifestFile)
83+
manifest := manifest.New(manifestFile)
84+
if err := manifest.Read(); err != nil {
85+
return fmt.Errorf("cannot parse manifest: %w", err)
86+
}
87+
88+
var intg integration
89+
for _, object := range manifest.Objects {
90+
switch {
91+
case object.Kind == "Broker":
92+
co, err := tmbroker.New(object.Metadata.Name, configDir)
93+
if err != nil {
94+
return fmt.Errorf("creating broker object: %v", err)
95+
}
96+
cc, err := triggermesh.Info(ctx, co)
97+
if err != nil {
98+
// ignore the error
99+
cc = nil
100+
}
101+
intg.Broker = component{
102+
object: []triggermesh.Component{co},
103+
container: []*docker.Container{cc},
104+
}
105+
case object.Kind == "Transformation":
106+
co := transformation.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec)
107+
cc, err := triggermesh.Info(ctx, co)
108+
if err != nil {
109+
// ignore the error
110+
cc = nil
111+
}
112+
intg.Transformation.object = append(intg.Transformation.object, co)
113+
intg.Transformation.container = append(intg.Transformation.container, cc)
114+
case object.APIVersion == "sources.triggermesh.io/v1alpha1":
115+
co := source.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec)
116+
cc, err := triggermesh.Info(ctx, co)
117+
if err != nil {
118+
// ignore the error
119+
cc = nil
120+
}
121+
intg.Source.object = append(intg.Source.object, co)
122+
intg.Source.container = append(intg.Source.container, cc)
123+
case object.APIVersion == "targets.triggermesh.io/v1alpha1":
124+
co := target.New(object.Metadata.Name, o.CRD, object.Kind, broker, o.Version, object.Spec)
125+
cc, err := triggermesh.Info(ctx, co)
126+
if err != nil {
127+
// ignore the error
128+
cc = nil
129+
}
130+
intg.Target.object = append(intg.Target.object, co)
131+
intg.Target.container = append(intg.Target.container, cc)
132+
default:
133+
continue
134+
}
135+
}
136+
137+
output.DescribeBroker(intg.Broker.object, intg.Broker.container)
138+
output.DescribeSource(intg.Source.object, intg.Source.container)
139+
output.DescribeTransformation(intg.Transformation.object, intg.Transformation.container)
140+
output.DescribeTarget(intg.Target.object, intg.Target.container)
141+
142+
return nil
143+
}

cmd/dump/dump.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path"
2323

2424
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
2526
"github.com/triggermesh/tmcli/pkg/manifest"
2627
"gopkg.in/yaml.v3"
2728
)
@@ -30,27 +31,25 @@ const manifestFile = "manifest.yaml"
3031

3132
type DumpOptions struct {
3233
ConfigDir string
33-
Context string
3434
Format string
3535
}
3636

3737
func NewCmd() *cobra.Command {
3838
o := &DumpOptions{}
3939
dumpCmd := &cobra.Command{
40-
Use: "dump <broker>",
40+
Use: "dump [broker]",
4141
Short: "Generate Kubernetes manifest",
4242
RunE: func(cmd *cobra.Command, args []string) error {
43-
c, err := cmd.Flags().GetString("config")
44-
if err != nil {
45-
return err
43+
broker := viper.GetString("context")
44+
if len(args) == 1 {
45+
broker = args[0]
4646
}
47-
broker, err := parseArgs(args)
47+
configDir, err := cmd.Flags().GetString("config")
4848
if err != nil {
4949
return err
5050
}
51-
o.ConfigDir = c
52-
o.Context = broker
53-
return o.Dump()
51+
o.ConfigDir = configDir
52+
return o.dump(broker)
5453
},
5554
}
5655

@@ -59,15 +58,8 @@ func NewCmd() *cobra.Command {
5958
return dumpCmd
6059
}
6160

62-
func parseArgs(args []string) (string, error) {
63-
if l := len(args); l != 1 {
64-
return "", fmt.Errorf("expected 1 arguments, got %d", l)
65-
}
66-
return args[0], nil
67-
}
68-
69-
func (o *DumpOptions) Dump() error {
70-
manifest := manifest.New(path.Join(o.ConfigDir, o.Context, manifestFile))
61+
func (o *DumpOptions) dump(broker string) error {
62+
manifest := manifest.New(path.Join(o.ConfigDir, broker, manifestFile))
7163
if err := manifest.Read(); err != nil {
7264
return err
7365
}

cmd/list/list.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2022 TriggerMesh Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package list
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path"
23+
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
26+
)
27+
28+
const manifestFile = "manifest.yaml"
29+
30+
func NewCmd() *cobra.Command {
31+
return &cobra.Command{
32+
Use: "list",
33+
Short: "Show brokers list",
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
configDir, err := cmd.Flags().GetString("config")
36+
if err != nil {
37+
return err
38+
}
39+
return list(configDir, viper.GetString("context"))
40+
},
41+
}
42+
}
43+
44+
func list(configDir, currentContext string) error {
45+
dirs, err := os.ReadDir(configDir)
46+
if err != nil {
47+
return fmt.Errorf("listing dirs: %w", err)
48+
}
49+
for _, dir := range dirs {
50+
if !dir.IsDir() {
51+
continue
52+
}
53+
files, err := os.ReadDir(path.Join(configDir, dir.Name()))
54+
if err != nil {
55+
return fmt.Errorf("listing files: %w", err)
56+
}
57+
for _, file := range files {
58+
if file.Name() == manifestFile {
59+
if dir.Name() == currentContext {
60+
fmt.Printf("*")
61+
}
62+
fmt.Println(dir.Name())
63+
}
64+
}
65+
}
66+
return nil
67+
}

0 commit comments

Comments
 (0)