Skip to content

Commit

Permalink
Fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
auxten committed Dec 23, 2023
1 parent ce0ea84 commit 6104b40
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/chdb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: chDB-go

on:
push:
branches: [ "main" ]
paths-ignore:
- '**/.md'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Fetch library
run: |
make install
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.20
- name: Build
run: |
go mod tidy
make build
- name: Test
run: ./chdb-go "SELECT 12345"

41 changes: 41 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: chDB-go Release

on:
release:
types: [created]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Fetch library
run: |
make install
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.20
- name: Build
run: |
go mod tidy
make build
- name: Test
run: ./chdb-go "SELECT 12345"
- name: Get Version
run: |
echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
- name: Upload release
if: github.event_name != 'pull_request'
uses: boxpositron/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
release_config: |
chdb-go-linux
tag_name: ${{ env.VERSION }}
release_name: chdbgo_${{ env.VERSION }}
draft: false
prerelease: false
overwrite: true
File renamed without changes.
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/chdb-io/chdb-go

go 1.21.5

require github.com/c-bata/go-prompt v0.2.6

require (
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect
)
126 changes: 126 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"context"
"flag"
"fmt"
"os"

"github.com/c-bata/go-prompt"

"github.com/chdb-io/chdb-go/cli"
"github.com/chdb-io/chdb-go/cli/completer"
"github.com/chdb-io/chdb-go/cli/history"

"github.com/chdb-io/chdb-go/chdb"
)

func main() {
// Define command line flags
pathFlag := flag.String("path", "",
`Specify a custom path for the session, default is a temporary directory and
data will lost after exit. If you want to keep the data, specify a path to a directory.`)

helpFlag := flag.Bool("help", false,
`Show this help message and exit.
Usage: chdb-go [options] [sql [output format]]
Example:
./chdb-go 'SELECT 123' # default output CSV
./chdb-go 'SELECT 123' JSON
./chdb-go # enter interactive mode, data will lost after exit
./chdb-go --path sess_path # enter persistent interactive mode
`)

flag.Parse()

if *helpFlag {
flag.Usage()
return
}

// If path is specified or no additional arguments, enter interactive mode
if len(flag.Args()) == 0 {
var err error
var session *chdb.Session
if *pathFlag != "" {
session, err = chdb.NewSession(*pathFlag)
} else {
session, err = chdb.NewSession()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err)
os.Exit(1)
}
defer session.Close()

interactiveMode(session)
} else {
// Execute a single query from command line arguments
args := flag.Args()
sql := args[0]
format := "CSV" // Default format
if len(args) > 1 {
format = args[1]
}

result := chdb.Query(sql, format)
if result == nil {
fmt.Println("No result or an error occurred.")
return
}

// Print the result
fmt.Println(result)
}
}

func interactiveMode(session *chdb.Session) {
fmt.Println("Enter your SQL commands; type 'exit' to quit.")

h, uh, err := initHistory("")
if err != nil {
fmt.Errorf("Failed to init history: %s", err)
return
}

c := cli.New(session, h, true)
complete := completer.New()

p := prompt.New(
c.Executor,
complete.Complete,
prompt.OptionTitle("chDB golang cli."),
prompt.OptionHistory(h.RowsToStrArr(uh)),
prompt.OptionPrefix(c.GetCurrentDB(context.Background())+" :) "),
prompt.OptionLivePrefix(c.GetLivePrefixState),
prompt.OptionPrefixTextColor(prompt.White),
prompt.OptionAddKeyBind(prompt.KeyBind{
Key: prompt.F3,
Fn: c.MultilineControl,
}),
)

p.Run()
}

func initHistory(path string) (*history.History, []*history.Row, error) {
var historyPath string
if path != "" {
historyPath = path
} else {
home, _ := os.UserHomeDir()
historyPath = home + "/.chdb-go-cli-history"
}

h, err := history.New(historyPath)
if err != nil {
return nil, nil, err
}

uh, err := h.Read()
if err != nil {
return nil, nil, err
}

return h, uh, nil
}

0 comments on commit 6104b40

Please sign in to comment.