Skip to content

Commit 6104b40

Browse files
committed
Fix readme
1 parent ce0ea84 commit 6104b40

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

.github/workflows/chdb.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: chDB-go
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
paths-ignore:
7+
- '**/.md'
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Fetch library
15+
run: |
16+
make install
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: 1.20
21+
- name: Build
22+
run: |
23+
go mod tidy
24+
make build
25+
- name: Test
26+
run: ./chdb-go "SELECT 12345"
27+

.github/workflows/release.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: chDB-go Release
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Fetch library
14+
run: |
15+
make install
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: 1.20
20+
- name: Build
21+
run: |
22+
go mod tidy
23+
make build
24+
- name: Test
25+
run: ./chdb-go "SELECT 12345"
26+
- name: Get Version
27+
run: |
28+
echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
29+
- name: Upload release
30+
if: github.event_name != 'pull_request'
31+
uses: boxpositron/upload-multiple-releases@1.0.7
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
with:
35+
release_config: |
36+
chdb-go-linux
37+
tag_name: ${{ env.VERSION }}
38+
release_name: chdbgo_${{ env.VERSION }}
39+
draft: false
40+
prerelease: false
41+
overwrite: true

REAEME.md renamed to README.md

File renamed without changes.

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/chdb-io/chdb-go
2+
3+
go 1.21.5
4+
5+
require github.com/c-bata/go-prompt v0.2.6
6+
7+
require (
8+
github.com/mattn/go-colorable v0.1.7 // indirect
9+
github.com/mattn/go-isatty v0.0.12 // indirect
10+
github.com/mattn/go-runewidth v0.0.9 // indirect
11+
github.com/mattn/go-tty v0.0.3 // indirect
12+
github.com/pkg/term v1.2.0-beta.2 // indirect
13+
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect
14+
)

main.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"os"
8+
9+
"github.com/c-bata/go-prompt"
10+
11+
"github.com/chdb-io/chdb-go/cli"
12+
"github.com/chdb-io/chdb-go/cli/completer"
13+
"github.com/chdb-io/chdb-go/cli/history"
14+
15+
"github.com/chdb-io/chdb-go/chdb"
16+
)
17+
18+
func main() {
19+
// Define command line flags
20+
pathFlag := flag.String("path", "",
21+
`Specify a custom path for the session, default is a temporary directory and
22+
data will lost after exit. If you want to keep the data, specify a path to a directory.`)
23+
24+
helpFlag := flag.Bool("help", false,
25+
`Show this help message and exit.
26+
Usage: chdb-go [options] [sql [output format]]
27+
Example:
28+
./chdb-go 'SELECT 123' # default output CSV
29+
./chdb-go 'SELECT 123' JSON
30+
./chdb-go # enter interactive mode, data will lost after exit
31+
./chdb-go --path sess_path # enter persistent interactive mode
32+
`)
33+
34+
flag.Parse()
35+
36+
if *helpFlag {
37+
flag.Usage()
38+
return
39+
}
40+
41+
// If path is specified or no additional arguments, enter interactive mode
42+
if len(flag.Args()) == 0 {
43+
var err error
44+
var session *chdb.Session
45+
if *pathFlag != "" {
46+
session, err = chdb.NewSession(*pathFlag)
47+
} else {
48+
session, err = chdb.NewSession()
49+
}
50+
if err != nil {
51+
fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err)
52+
os.Exit(1)
53+
}
54+
defer session.Close()
55+
56+
interactiveMode(session)
57+
} else {
58+
// Execute a single query from command line arguments
59+
args := flag.Args()
60+
sql := args[0]
61+
format := "CSV" // Default format
62+
if len(args) > 1 {
63+
format = args[1]
64+
}
65+
66+
result := chdb.Query(sql, format)
67+
if result == nil {
68+
fmt.Println("No result or an error occurred.")
69+
return
70+
}
71+
72+
// Print the result
73+
fmt.Println(result)
74+
}
75+
}
76+
77+
func interactiveMode(session *chdb.Session) {
78+
fmt.Println("Enter your SQL commands; type 'exit' to quit.")
79+
80+
h, uh, err := initHistory("")
81+
if err != nil {
82+
fmt.Errorf("Failed to init history: %s", err)
83+
return
84+
}
85+
86+
c := cli.New(session, h, true)
87+
complete := completer.New()
88+
89+
p := prompt.New(
90+
c.Executor,
91+
complete.Complete,
92+
prompt.OptionTitle("chDB golang cli."),
93+
prompt.OptionHistory(h.RowsToStrArr(uh)),
94+
prompt.OptionPrefix(c.GetCurrentDB(context.Background())+" :) "),
95+
prompt.OptionLivePrefix(c.GetLivePrefixState),
96+
prompt.OptionPrefixTextColor(prompt.White),
97+
prompt.OptionAddKeyBind(prompt.KeyBind{
98+
Key: prompt.F3,
99+
Fn: c.MultilineControl,
100+
}),
101+
)
102+
103+
p.Run()
104+
}
105+
106+
func initHistory(path string) (*history.History, []*history.Row, error) {
107+
var historyPath string
108+
if path != "" {
109+
historyPath = path
110+
} else {
111+
home, _ := os.UserHomeDir()
112+
historyPath = home + "/.chdb-go-cli-history"
113+
}
114+
115+
h, err := history.New(historyPath)
116+
if err != nil {
117+
return nil, nil, err
118+
}
119+
120+
uh, err := h.Read()
121+
if err != nil {
122+
return nil, nil, err
123+
}
124+
125+
return h, uh, nil
126+
}

0 commit comments

Comments
 (0)