Skip to content

Commit 245c931

Browse files
committed
🎨 Create structure for the CLI
This commit sets up the boilerplate for the CLI package
1 parent 9300b77 commit 245c931

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

cmd/query.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var queryCmd = &cobra.Command{
10+
Use: "query [sql query]",
11+
Short: "Run a SQL query",
12+
Long: `Run a SQL query on the data sources installed on the system.
13+
The query can be specified as an argument or read from stdin.
14+
If no query is provided, the command will launch an interactive input.`,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
fmt.Println("query called")
17+
},
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(queryCmd)
22+
queryCmd.Flags().String("format", "pretty", "Output format (pretty, json, csv, plain)")
23+
queryCmd.Flags().Bool("json", false, "Output format as JSON")
24+
queryCmd.Flags().Bool("csv", false, "Output format as CSV")
25+
queryCmd.Flags().Bool("plain", false, "Output format as plain text")
26+
}

cmd/root.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// rootCmd represents the base command when called without any subcommands
10+
var rootCmd = &cobra.Command{
11+
Use: "anyq",
12+
Short: "A tool to query any data source",
13+
Long: `Anyquery is a tool that allows you to query any data source
14+
by writing SQL queries. It is designed to be extended by anyone.`,
15+
}
16+
17+
func Execute() {
18+
err := rootCmd.Execute()
19+
if err != nil {
20+
os.Stderr.WriteString(err.Error() + "\n")
21+
os.Exit(1)
22+
}
23+
}
24+
25+
func init() {
26+
rootCmd.Flags().Bool("no-input", false, "Do not launch an interactive input")
27+
rootCmd.Flags().BoolP("version", "v", false, "Print the version of the program")
28+
}

cmd/server.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var serverCmd = &cobra.Command{
10+
Use: "server",
11+
Short: "Lets you connect to anyquery remotely",
12+
Long: `Listens for incoming connections and allows you to run queries
13+
using any postgres client.`,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
fmt.Println("server called")
16+
},
17+
}
18+
19+
func init() {
20+
rootCmd.AddCommand(serverCmd)
21+
serverCmd.Flags().StringP("host", "h", "localhost", "Host to listen on")
22+
serverCmd.Flags().IntP("port", "p", 5432, "Port to listen on")
23+
serverCmd.Flags().StringP("database", "d", "anyquery", "Database to connect to (a path or :memory:)")
24+
serverCmd.Flags().Bool("readonly", false, "Start the server in read-only mode")
25+
}

cmd/store.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var storeCmd = &cobra.Command{
10+
Use: "store",
11+
Short: "Install and manage plugins",
12+
Long: `The store command allows you to install and manage plugins
13+
from the anyquery store. Plugins are used to connect to data sources
14+
and run queries on them.`,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
fmt.Println("store called")
17+
},
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(storeCmd)
22+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ require (
1717
github.com/golang/protobuf v1.5.4 // indirect
1818
github.com/hashicorp/go-hclog v1.6.3 // indirect
1919
github.com/hashicorp/yamux v0.1.1 // indirect
20+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2021
github.com/mattn/go-colorable v0.1.13 // indirect
2122
github.com/mattn/go-isatty v0.0.20 // indirect
2223
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
2324
github.com/oklog/run v1.1.0 // indirect
2425
github.com/pmezard/go-difflib v1.0.0 // indirect
26+
github.com/spf13/cobra v1.8.0 // indirect
27+
github.com/spf13/pflag v1.0.5 // indirect
2528
golang.org/x/net v0.24.0 // indirect
2629
golang.org/x/sys v0.19.0 // indirect
2730
golang.org/x/text v0.14.0 // indirect

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
22
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
34
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
56
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -18,6 +19,8 @@ github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDm
1819
github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
1920
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
2021
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
22+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
23+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2124
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
2225
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
2326
github.com/julien040/go-sqlite3-anyquery v1.17.0 h1:agkknNjaUc9gbTONaMrPvCoJXBoJ84CoRSXaMh+IUv4=
@@ -37,6 +40,11 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
3740
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
3841
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3942
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
43+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
44+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
45+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
46+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
47+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4048
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4149
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
4250
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"github.com/julien040/anyquery/cmd"
5+
)
46

57
func main() {
68

7-
fmt.Println("Welcome to AnyQuery!")
9+
cmd.Execute()
810
}

0 commit comments

Comments
 (0)