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

Combine redmegen and specgen into conn-sdk-cli #246

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ test:
echo
echo "Running integration tests..."
echo
cd specgen/specgen/tests/parse_specs/ && go test $(GOTEST_FLAGS) -race ./...
cd specgen/specgen/tests/write_and_combine/ && go test $(GOTEST_FLAGS) -race ./...
cd conn-sdk-cli/specgen/tests/parse_specs/ && go test $(GOTEST_FLAGS) -race ./...
cd conn-sdk-cli/specgen/tests/write_and_combine/ && go test $(GOTEST_FLAGS) -race ./...

.PHONY: fmt
fmt:
Expand All @@ -27,20 +27,21 @@ install-tools:

.PHONY: tidy-all
tidy-all:
go mod tidy
@echo "Tidying up module in parse_specs directory"
@(cd specgen/specgen/tests/parse_specs && go mod tidy)
@(cd conn-sdk-cli/specgen/tests/parse_specs && go mod tidy)
@echo "Tidying up subdirectories..."
@for dir in specgen/specgen/tests/parse_specs/*/; do \
@for dir in conn-sdk-cli/specgen/tests/parse_specs/*/; do \
if [ -f "$$dir/go.mod" ]; then \
echo "Processing directory: $$dir"; \
(cd "$$dir" && go mod tidy) || exit 1; \
fi \
done

@echo "Tidying up module in write_and_combine directory"
@(cd specgen/specgen/tests/write_and_combine && go mod tidy)
@(cd conn-sdk-cli/specgen/tests/write_and_combine && go mod tidy)
@echo "Tidying up subdirectories..."
@for dir in specgen/specgen/tests/write_and_combine/*/; do \
@for dir in conn-sdk-cli/specgen/tests/write_and_combine/*/; do \
if [ -f "$$dir/go.mod" ]; then \
echo "Processing directory: $$dir"; \
(cd "$$dir" && go mod tidy) || exit 1; \
Expand Down
6 changes: 3 additions & 3 deletions cmd/readmegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"io"
"os"

"github.com/conduitio/conduit-connector-sdk/cmd/readmegen/util"
"github.com/conduitio/conduit-connector-sdk/conn-sdk-cli/readmegen"
"github.com/conduitio/yaml/v3"
)

Expand Down Expand Up @@ -53,13 +53,13 @@ func mainE() error {
out = buf
}

opts := util.GenerateOptions{
opts := readmegen.GenerateOptions{
Data: specs,
ReadmePath: *readmePath,
Out: out,
}

err = util.Generate(opts)
err = readmegen.Generate(opts)
if err != nil {
return fmt.Errorf("failed to generate readme: %w", err)
}
Expand Down
72 changes: 72 additions & 0 deletions conn-sdk-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright © 2024 Meroxa, Inc.
//
// 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 main

import (
"fmt"
"os"

"github.com/conduitio/conduit-connector-sdk/conn-sdk-cli/readmegen"
"github.com/conduitio/conduit-connector-sdk/conn-sdk-cli/specgen"
"github.com/spf13/cobra"
)

func main() {
cmdRoot := &cobra.Command{
Use: "conn-sdk-cli",
Short: "Tooling around generating connector related files",
}

cmdReadmegen := &cobra.Command{
Use: "readmegen",
Short: "Generate README for connector",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
specifications := cmd.Flag("specifications").Value.String()
readme := cmd.Flag("readme").Value.String()
write, _ := cmd.Flags().GetBool("write")

return readmegen.NewCommand(specifications, readme, write).Execute(cmd.Context())
},
}
cmdReadmegen.Flags().StringP("specifications", "s", "./connector.yaml", "path to the connector.yaml file")
cmdReadmegen.Flags().StringP("readme", "r", "./README.md", "path to the README.md file")
cmdReadmegen.Flags().BoolP("write", "w", false, "Overwrite readme file instead of printing to stdout")

cmdSpecgen := &cobra.Command{
Use: "specgen",
Short: "Generate specification files for connector",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
output := cmd.Flag("output").Value.String()
path := cmd.Flag("path").Value.String()

return specgen.NewCommand(output, path).Execute(cmd.Context())
},
}
cmdSpecgen.Flags().StringP("output", "o", "connector.yaml", "name of the output file")
cmdSpecgen.Flags().StringP("package", "p", ".", "path to the package that contains the Connector variable")

cmdRoot.AddCommand(
cmdReadmegen,
cmdSpecgen,
)
cmdRoot.CompletionOptions.DisableDefaultCmd = true

if err := cmdRoot.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
86 changes: 86 additions & 0 deletions conn-sdk-cli/readmegen/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright © 2025 Meroxa, Inc.
//
// 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 readmegen

import (
"bytes"
"context"
"fmt"
"io"
"os"

"github.com/conduitio/yaml/v3"
)

type Command struct {
specificationsFile string
readmeFile string
write bool
}

func NewCommand(specificationsFile, readmeFile string, write bool) *Command {
return &Command{
specificationsFile: specificationsFile,
readmeFile: readmeFile,
write: write,
}
}

func (cmd *Command) Execute(_ context.Context) error {
specs, err := cmd.readSpecs(cmd.specificationsFile)
if err != nil {
return err
}

buf := new(bytes.Buffer)
var out io.Writer = os.Stdout
if cmd.write {
// Write to buffer and then flush to file
out = buf
}

opts := GenerateOptions{
Data: specs,
ReadmePath: cmd.readmeFile,
Out: out,
}

err = Generate(opts)
if err != nil {
return fmt.Errorf("failed to generate readme: %w", err)
}

if cmd.write {
err = os.WriteFile(cmd.readmeFile, buf.Bytes(), 0o644)
if err != nil {
return fmt.Errorf("failed to write %s: %w", cmd.readmeFile, err)
}
}
return nil
}

func (*Command) readSpecs(path string) (map[string]any, error) {
specsRaw, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read specifications from %v: %w", path, err)
}

var data map[string]any
err = yaml.Unmarshal(specsRaw, &data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal specifications: %w", err)
}
return data, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package readmegen

import (
"embed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package readmegen

import "testing"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package readmegen

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package readmegen

import "testing"

Expand Down
8 changes: 5 additions & 3 deletions specgen/README.md → conn-sdk-cli/specgen/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# specgen
# Specgen

## Overview

Expand Down Expand Up @@ -38,7 +38,9 @@ var Connector = sdk.Connector{

1. Extracts the specifications from the source and destination configuration
struct.
2. Combines the extracted specification with the existing one in `connector.yaml`.
2. Combines the extracted specification with the existing one in
`connector.yaml`.

More detailed information about `specgen` and `connector.yaml` can be found in
the [Conduit documentation](https://conduit.io/docs/developing/connectors/connector-specification).
the
[Conduit documentation](https://conduit.io/docs/developing/connectors/connector-specification).
Loading