Skip to content

Commit

Permalink
chore: add rudder-cli source and rm binaries (#3872)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach committed Sep 15, 2023
1 parent ae989cd commit c7ab148
Show file tree
Hide file tree
Showing 22 changed files with 1,405 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ issues:
- errcheck
- unparam

- path: 'cmd/rudder-cli/status/status.go'
linters:
- bodyclose

linters-settings:
depguard:
rules:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN BUILD_DATE=$(date "+%F,%T") \
make build

RUN go build -o devtool ./cmd/devtool/

RUN go build -o rudder-cli ./cmd/rudder-cli/

FROM alpine:${ALPINE_VERSION}

Expand All @@ -39,10 +39,10 @@ COPY --from=builder rudder-server/rudder-server .
COPY --from=builder rudder-server/build/wait-for-go/wait-for-go .
COPY --from=builder rudder-server/build/regulation-worker .
COPY --from=builder rudder-server/devtool .
COPY --from=builder rudder-server/rudder-cli /usr/bin/rudder-cli

COPY build/docker-entrypoint.sh /
COPY build/wait-for /
COPY ./rudder-cli/rudder-cli.linux.x86_64 /usr/bin/rudder-cli
COPY scripts/generate-event /scripts/generate-event
COPY scripts/batch.json /scripts/batch.json

Expand Down
108 changes: 108 additions & 0 deletions cmd/rudder-cli/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package api

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"

"github.com/olekukonko/tablewriter"

"github.com/rudderlabs/rudder-server/cmd/rudder-cli/config"
)

type SourceT struct {
ID string
Name string
SourceDefinition SourceDefinitionT
Config interface{}
Enabled bool
Destinations []DestinationT
WriteKey string
}

type DestinationT struct {
ID string
Name string
DestinationDefinition DestinationDefinitionT
Config interface{}
Enabled bool
Transformations []TransformationT
}
type TransformationT struct {
ID string
Name string
Description string
VersionID string
}
type SourcesT struct {
Sources []SourceT `json:"sources"`
}

type DestinationDefinitionT struct {
ID string
Name string
DisplayName string
}

type SourceDefinitionT struct {
ID string
Name string
}

func getWorkspaceConfig() (SourcesT, bool) {
baseUrl := config.GetEnv(config.ConfigBackendURLKey)
configBackendToken := config.GetEnv(config.ConfigBackendWorkSpaceToken)

client := &http.Client{}
url := fmt.Sprintf("%s/workspace-config?workspaceToken=%s", baseUrl, configBackendToken)
resp, err := client.Get(url)

var respBody []byte
if resp != nil && resp.Body != nil {
respBody, _ = io.ReadAll(resp.Body)
defer resp.Body.Close()
}

if err != nil {
fmt.Println("Errored when sending request to the server", err)
return SourcesT{}, false
}
var sources SourcesT
err = json.Unmarshal(respBody, &sources)
if err != nil {
fmt.Println("Errored while parsing request", err, string(respBody), resp.StatusCode)
return SourcesT{}, false
}

return sources, true
}

func DisplayConfig() {
sourceList, ok := getWorkspaceConfig()
if !ok {
return
}
sources := sourceList.Sources

table := tablewriter.NewWriter(os.Stdout)

table.SetAutoWrapText(false)
table.SetHeader([]string{"Source", "Destination"})

for i := 0; i < len(sources); i++ {
for j := 0; j < len(sources[i].Destinations); j++ {
sourceName := sources[i].Name
if j > 0 {
sourceName = ""
}
table.Append([]string{
sourceName,
sources[i].Destinations[j].Name,
})
}
}

table.Render()
}
22 changes: 22 additions & 0 deletions cmd/rudder-cli/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
"log"
"net/rpc"
"os"
"path/filepath"
)

// Setup Unix Domain socket client to communicate with
// the rudder server running on the same machine as this cli
func GetUDSClient() *rpc.Client {
tmpdirPath := os.Getenv("RUDDER_TMPDIR")
if tmpdirPath == "" {
tmpdirPath = "/tmp"
}
client, err := rpc.DialHTTP("unix", filepath.Join(tmpdirPath, "rudder-server.sock"))
if err != nil {
log.Fatal("dialing:", err)
}
return client
}
31 changes: 31 additions & 0 deletions cmd/rudder-cli/config/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

// RudderServerPathKey :
const RudderServerPathKey = "RUDDER_SERVER_PATH"

// RudderEnvFilePathKey :
const RudderEnvFilePathKey = "RUDDER_ENV_FILE_PATH"

const (
JobsDBHostKey = "JOBS_DB_HOST"
JobsDBPortKey = "JOBS_DB_PORT"
JobsDBNameKey = "JOBS_DB_DB_NAME"
JobsDBUserKey = "JOBS_DB_USER"
JobsDBPasswordKey = "JOBS_DB_PASSWORD"
)

const (
JobsS3BucketKey = "JOBS_BACKUP_BUCKET"
AWSAccessKey = "AWS_ACCESS_KEY_ID"
AWSSecretKey = "AWS_SECRET_ACCESS_KEY"
)

const DefaultEnvFile = ".env"

const DestTransformURLKey = "DEST_TRANSFORM_URL"

const (
ConfigBackendURLKey = "CONFIG_BACKEND_URL"
ConfigBackendTokenKey = "CONFIG_BACKEND_TOKEN"
ConfigBackendWorkSpaceToken = "WORKSPACE_TOKEN"
)
40 changes: 40 additions & 0 deletions cmd/rudder-cli/config/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"fmt"
"os"

"github.com/joho/godotenv"
)

var initialized = false

func SetEnvFile(path string) {
}

func getServerDirPath() string {
envServerDirPath, ok := os.LookupEnv(RudderServerPathKey)

if !ok {
return "."
}
return envServerDirPath
}

func getEnvFilePath() string {
envFilePath, ok := os.LookupEnv(RudderEnvFilePathKey)
if !ok {
return fmt.Sprintf("%s/%s", getServerDirPath(), DefaultEnvFile)
}
return envFilePath
}

func GetEnv(key string) string {
if !initialized {
// Load the .env file, if it exists
_ = godotenv.Load(getEnvFilePath())
initialized = true
}

return os.Getenv(key)
}
51 changes: 51 additions & 0 deletions cmd/rudder-cli/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package db

import (
"database/sql"
"fmt"
"os"
"strconv"

_ "github.com/lib/pq"
"github.com/olekukonko/tablewriter"

"github.com/rudderlabs/rudder-server/cmd/rudder-cli/util"
)

func getRowCount(db *sql.DB, tableName string) int64 {
totalCount := int64(-1)
sqlStatement := fmt.Sprintf(`SELECT COUNT(*) FROM %s`, tableName)
row := db.QueryRow(sqlStatement)
_ = row.Scan(&totalCount)
return totalCount
}

func DisplayAllTables() {
table := tablewriter.NewWriter(os.Stdout)

table.SetAutoWrapText(false)
table.SetHeader([]string{"Name", "Count"})

db, err := util.GetDbHandle()
if err != nil {
// Write to console
fmt.Println(err)
}

pgTableNames, err := util.GetAllTableNames(db)
if err != nil {
// Write to console
fmt.Println(err)
return
}

for i := 0; i < len(pgTableNames); i++ {
tableName := pgTableNames[i]
table.Append([]string{
tableName,
strconv.FormatInt(getRowCount(db, tableName), 10),
})
}

table.Render()
}
Loading

0 comments on commit c7ab148

Please sign in to comment.