Skip to content

Commit

Permalink
add comand to create integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Jan 29, 2025
1 parent 76ca59d commit 8b8788f
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 12 deletions.
126 changes: 126 additions & 0 deletions cmd/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"context"
"errors"

"github.com/ayinke-llc/hermes"
"github.com/ayinke-llc/malak"
"github.com/ayinke-llc/malak/config"
"github.com/ayinke-llc/malak/internal/datastore/postgres"
"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

func addIntegrationCommand(c *cobra.Command, cfg *config.Config) {

cmd := &cobra.Command{
Use: "integration",
Short: "Manage your system wide integrations",
}

cmd.AddCommand(createIntegration(c, cfg))

c.AddCommand(cmd)

Check warning on line 25 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L16-L25

Added lines #L16 - L25 were not covered by tests
}

func createIntegration(_ *cobra.Command, cfg *config.Config) *cobra.Command {

cmd := &cobra.Command{
Use: "create",
Short: `Create a new integration`,
RunE: func(cmd *cobra.Command, args []string) error {

var (
name string
description string
isEnabledIntegration bool

integrationType malak.IntegrationType
)

logger, err := getLogger(hermes.DeRef(cfg))
if err != nil {
return err
}

Check warning on line 46 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L28-L46

Added lines #L28 - L46 were not covered by tests

db, err := postgres.New(cfg, logger)
if err != nil {
logger.Error("could not connect to postgres database",
zap.Error(err))
return err
}

Check warning on line 53 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L48-L53

Added lines #L48 - L53 were not covered by tests

form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("What’s the name of this integration?").
Value(&name).
Validate(func(str string) error {
if hermes.IsStringEmpty(str) {
return errors.New("please provide the name of your integration")
}

Check warning on line 63 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L55-L63

Added lines #L55 - L63 were not covered by tests

if len(str) > 30 {
return errors.New("integration name cannot be more than 30")
}

Check warning on line 67 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L65-L67

Added lines #L65 - L67 were not covered by tests

return nil

Check warning on line 69 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L69

Added line #L69 was not covered by tests
}),

huh.NewInput().
Title("Write a description for this integration?").
CharLimit(300).
Value(&description),

huh.NewSelect[malak.IntegrationType]().
Title("Pick an integration type").
Options(
huh.NewOption("oauth2", malak.IntegrationTypeOauth2),
huh.NewOption("api key", malak.IntegrationTypeApiKey),
huh.NewOption("malak ( internal )", malak.IntegrationTypeMalak),
).
Value(&integrationType),

huh.NewConfirm().
Title("Is enabled?").
Description("should this integration be enabled?").
Value(&isEnabledIntegration),
),
)

defer db.Close()

if err := form.Run(); err != nil {
logger.Error("could not take in data from user",
zap.Error(err))
return err
}

Check warning on line 99 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L93-L99

Added lines #L93 - L99 were not covered by tests

logger.Debug("creating a new integration")

integration := &malak.Integration{
Reference: malak.NewReferenceGenerator().Generate(malak.EntityTypeIntegration),
Description: description,
IsEnabled: isEnabledIntegration,
Metadata: malak.IntegrationMetadata{},
IntegrationType: integrationType,
IntegrationName: name,
}

integrationRepo := postgres.NewIntegrationRepo(db)

if err := integrationRepo.Create(context.Background(), integration); err != nil {
logger.Error("could not create integration", zap.Error(err))
return err
}

Check warning on line 117 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L101-L117

Added lines #L101 - L117 were not covered by tests

logger.Debug("created integration")

return nil

Check warning on line 121 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L119-L121

Added lines #L119 - L121 were not covered by tests
},
}

return cmd

Check warning on line 125 in cmd/integration.go

View check run for this annotation

Codecov / codecov/patch

cmd/integration.go#L125

Added line #L125 was not covered by tests
}
10 changes: 1 addition & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ayinke-llc/malak/internal/pkg/email/smtp"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -75,14 +74,7 @@ func Execute() error {
addHTTPCommand(rootCmd, cfg)
addCronCommand(rootCmd, cfg)
addPlanCommand(rootCmd, cfg)

cmd, _, err := rootCmd.Find(os.Args[1:])
// default cmd if no cmd is given
// default to http
if err == nil && cmd.Use == rootCmd.Use && cmd.Flags().Parse(os.Args[1:]) != pflag.ErrHelp {
args := append([]string{"http"}, os.Args[1:]...)
rootCmd.SetArgs(args)
}
addIntegrationCommand(rootCmd, cfg)

Check warning on line 77 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L77

Added line #L77 was not covered by tests

return rootCmd.Execute()
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func createPlan(_ *cobra.Command, cfg *config.Config) *cobra.Command {
Value(&defaultPlanID),

huh.NewConfirm().
Title("Default plan").
Title("Default plan?").
Description("should this plan be the default one for new users?").
Value(&isDefaultPlan),
),
Expand Down
8 changes: 8 additions & 0 deletions integration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package malak

import (
"context"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -54,4 +55,11 @@ type WorkspaceIntegration struct {
}

type IntegrationRepository interface {
Create(context.Context, *Integration) error

// ListSystem(context.Context) ([]Integration, error)
// DisableSystem(context.Context, *Integration) error
//
// List(context.Context, *Workspace) error
// Disable(context.Context, *Workspace, *Integration) error
}
34 changes: 34 additions & 0 deletions internal/datastore/postgres/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package postgres

import (
"context"
"database/sql"

"github.com/ayinke-llc/malak"
"github.com/uptrace/bun"
)

type integrationRepo struct {
inner *bun.DB
}

func NewIntegrationRepo(db *bun.DB) malak.IntegrationRepository {
return &integrationRepo{
inner: db,
}

Check warning on line 18 in internal/datastore/postgres/integration.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/postgres/integration.go#L15-L18

Added lines #L15 - L18 were not covered by tests
}

func (i *integrationRepo) Create(ctx context.Context,
integration *malak.Integration) error {

ctx, cancelFn := withContext(ctx)
defer cancelFn()

return i.inner.RunInTx(ctx, &sql.TxOptions{},
func(ctx context.Context, tx bun.Tx) error {
_, err := tx.NewInsert().
Model(integration).
Exec(ctx)
return err
})

Check warning on line 33 in internal/datastore/postgres/integration.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/postgres/integration.go#L22-L33

Added lines #L22 - L33 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE integrations (
deleted_at TIMESTAMP WITH TIME ZONE
);

ALTER TABLE integrations ADD CONSTRAINT integrations_reference_check_key CHECK (reference ~ 'integrations_[a-zA-Z0-9._]+');
ALTER TABLE integrations ADD CONSTRAINT integration_reference_check_key CHECK (reference ~ 'integration_[a-zA-Z0-9._]+');

CREATE TABLE workspace_integrations (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
Expand All @@ -31,4 +31,4 @@ CREATE TABLE workspace_integrations (
deleted_at TIMESTAMP WITH TIME ZONE
);

ALTER TABLE workspace_integrations ADD CONSTRAINT workspace_integrations_reference_check_key CHECK (reference ~ 'workspace_integrations_[a-zA-Z0-9._]+');
ALTER TABLE workspace_integrations ADD CONSTRAINT workspace_integration_reference_check_key CHECK (reference ~ 'workspace_integration_[a-zA-Z0-9._]+');

0 comments on commit 8b8788f

Please sign in to comment.