diff --git a/index.yaml b/index.yaml index 8d2d5ee9..4f2c9c7a 100644 --- a/index.yaml +++ b/index.yaml @@ -65,6 +65,8 @@ tools: reference: ./search/tavily pagerduty: reference: ./pagerduty + postgres: + reference: ./postgres knowledgeDataSources: notion-data-source: diff --git a/postgres/credential/tool.gpt b/postgres/credential/tool.gpt new file mode 100644 index 00000000..49717855 --- /dev/null +++ b/postgres/credential/tool.gpt @@ -0,0 +1,50 @@ +Name: Postgres Credential +Share Credential: postgres-cred as postgres +Type: credential + +--- +Name: postgres-cred +Tools: github.com/obot-platform/tools/generic-credential + +#!sys.call github.com/obot-platform/tools/generic-credential + +{ + "promptInfo": { + "fields": [ + { + "name": "Postgres Host", + "description": "The host of your Postgres database", + "env": "POSTGRES_HOST" + }, + { + "name": "Postgres Port", + "description": "The port number of your Postgres database", + "env": "POSTGRES_PORT" + }, + { + "name": "Postgres Database", + "description": "The name of your Postgres database", + "env": "POSTGRES_DATABASE" + }, + { + "name": "Postgres Username", + "description": "The username for your Postgres database", + "env": "POSTGRES_USERNAME" + }, + { + "name": "Postgres Password", + "description": "The password for your Postgres database", + "env": "POSTGRES_PASSWORD", + "sensitive": true + }, + { + "name": "Postgres sslmode (optional)", + "description": "The setting for the sslmode parameter when connecting to the database. `require` is the default.", + "env": "POSTGRES_SSLMODE" + } + ], + "message": "Enter your Postgres database credentials." + }, + "validationTool": "github.com/obot-platform/tools/postgres/validate-cred.gpt" +} + diff --git a/postgres/go.mod b/postgres/go.mod new file mode 100644 index 00000000..ca89eb07 --- /dev/null +++ b/postgres/go.mod @@ -0,0 +1,20 @@ +module postgres_query_executor + +go 1.24.0 + +require ( + gorm.io/driver/postgres v1.5.11 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + golang.org/x/crypto v0.35.0 // indirect + golang.org/x/sync v0.11.0 // indirect + golang.org/x/text v0.22.0 // indirect +) diff --git a/postgres/go.sum b/postgres/go.sum new file mode 100644 index 00000000..5e0f68a8 --- /dev/null +++ b/postgres/go.sum @@ -0,0 +1,36 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs= +golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ= +golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= +golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= +golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= +gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/postgres/main.go b/postgres/main.go new file mode 100644 index 00000000..f8e8c328 --- /dev/null +++ b/postgres/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "strings" + + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +func main() { + query := os.Getenv("QUERY") + host := os.Getenv("POSTGRES_HOST") + port := os.Getenv("POSTGRES_PORT") + database := os.Getenv("POSTGRES_DATABASE") + user := os.Getenv("POSTGRES_USERNAME") + password := os.Getenv("POSTGRES_PASSWORD") + sslmode := os.Getenv("POSTGRES_SSLMODE") + + if sslmode == "" { + sslmode = "require" + } + + dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s", + user, password, host, port, database, sslmode) + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + fmt.Printf("Error opening database: %v\n", err) + os.Exit(1) + } + + // Check for validate mode + if len(os.Args) > 1 && os.Args[1] == "validate" { + // Test the connection with a simple query + var result int + err = db.Raw("SELECT 1").Scan(&result).Error + if err != nil { + fmt.Printf("Error validating connection: %v\n", err) + os.Exit(1) + } + + fmt.Println("Database connection validated successfully") + os.Exit(0) + } + + // Check if the query returns data + queryUpper := strings.TrimSpace(strings.ToUpper(query)) + returnsData := strings.Contains(queryUpper, "SELECT") || + strings.Contains(queryUpper, "RETURNING") || + strings.HasPrefix(queryUpper, "VALUES") || + strings.HasPrefix(queryUpper, "SHOW") + + if returnsData { + var results []map[string]any + err := db.Raw(query).Scan(&results).Error + if err != nil { + fmt.Printf("Error executing query: %v\n", err) + os.Exit(1) + } + + jsonData, err := json.Marshal(results) + if err != nil { + fmt.Printf("Error marshaling JSON: %v\n", err) + os.Exit(1) + } + + fmt.Println(string(jsonData)) + } else { + result := db.Exec(query) + if result.Error != nil { + fmt.Printf("Error executing query: %v\n", result.Error) + os.Exit(1) + } + + fmt.Printf("Query executed successfully! Rows affected: %d\n", result.RowsAffected) + } +} diff --git a/postgres/tool.gpt b/postgres/tool.gpt new file mode 100644 index 00000000..2d2a8dbb --- /dev/null +++ b/postgres/tool.gpt @@ -0,0 +1,10 @@ +Name: Postgres Query Executor +Description: Execute SQL queries against a Postgres database +Credential: ./credential +Param: query: The SQL query to execute + +#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool + +--- +!metadata:*:icon +https://cdn.jsdelivr.net/npm/simple-icons@v13/icons/postgresql.svg diff --git a/postgres/validate-cred.gpt b/postgres/validate-cred.gpt new file mode 100644 index 00000000..d84cc458 --- /dev/null +++ b/postgres/validate-cred.gpt @@ -0,0 +1,5 @@ +Name: postgres-cred-validate +Description: Validate PostgreSQL credentials +Metadata: index: false + +#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool validate