-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
794 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: ['trunk'] | ||
|
||
pull_request: | ||
branches: ['trunk'] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.20 | ||
|
||
- name: Vet | ||
run: go vet -v ./... | ||
|
||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# cli | ||
# Spice.xyz CLI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main | ||
|
||
func main() { | ||
Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spiceai/gospice" | ||
"github.com/spicehq/cli/pkg/arrow" | ||
) | ||
|
||
var ( | ||
apiKeyFlag string | ||
outputFormatFlag string | ||
showDetailsFlag bool | ||
) | ||
|
||
var queryCmd = &cobra.Command{ | ||
Use: "query", | ||
Short: "Spice.xyz CLI query", | ||
Example: ` | ||
spice query | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if apiKeyFlag == "" { | ||
cmd.PrintErrf("error: --api_key is required\n") | ||
os.Exit(1) | ||
} | ||
|
||
var query = "Select number, hash FROM eth.recent_blocks ORDER BY number DESC LIMIT 3" | ||
if len(args) > 0 { | ||
query = args[0] | ||
} | ||
|
||
spice := gospice.NewSpiceClient() | ||
defer spice.Close() | ||
|
||
if err := spice.Init(apiKeyFlag); err != nil { | ||
cmd.PrintErrf("error intializing the Spice.xyz SDK: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
startTime := time.Now() | ||
reader, err := spice.Query(cmd.Context(), query) | ||
if err != nil { | ||
cmd.PrintErrf("error querying Spice.xyz: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
defer reader.Release() | ||
queryExecutionTime := time.Since(startTime) | ||
|
||
for reader.Next() { | ||
record := reader.Record() | ||
defer record.Release() | ||
|
||
switch outputFormatFlag { | ||
case "csv": | ||
if err = arrow.WriteCsv(os.Stdout, record); err != nil { | ||
cmd.PrintErrf("error writing CSV: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
case "json": | ||
data, err := record.MarshalJSON() | ||
if err != nil { | ||
cmd.PrintErrf("error writing JSON: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
os.Stdout.Write(data) | ||
os.Stdout.WriteString("\n") | ||
default: | ||
cmd.PrintErrf("error: invalid output format: %s\n", outputFormatFlag) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
if showDetailsFlag { | ||
os.Stdout.WriteString("\n") | ||
cmd.Printf("Fetched in: %s\n", queryExecutionTime) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
queryCmd.Flags().StringVar(&apiKeyFlag, "api_key", "", "Spice.xyz API Key") | ||
queryCmd.Flags().StringVar(&outputFormatFlag, "output_format", "csv", "Output format (csv or json)") | ||
queryCmd.Flags().BoolVar(&showDetailsFlag, "show_details", false, "Show details of query") | ||
|
||
RootCmd.AddCommand(queryCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
algorithmFlag string | ||
numberEpisodesFlag int64 | ||
loggers []string | ||
) | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "spice", | ||
Short: "Spice.xyz CLI", | ||
} | ||
|
||
// Execute adds all child commands to the root command. | ||
func Execute() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
if err := RootCmd.Execute(); err != nil { | ||
RootCmd.Println(err) | ||
os.Exit(-1) | ||
} | ||
} | ||
|
||
func initConfig() { | ||
viper.SetEnvPrefix("spice") | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
viper.AutomaticEnv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Spice.xyz CLI version", | ||
Example: ` | ||
spice version | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Printf("Version: 0.1\n") | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(versionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module github.com/spicehq/cli | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/apache/arrow/go/v10 v10.0.1 | ||
github.com/spf13/cobra v1.7.0 | ||
github.com/spf13/viper v1.15.0 | ||
github.com/spiceai/gospice v0.1.0 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.0.4 // indirect | ||
github.com/apache/thrift v0.16.0 // indirect | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/goccy/go-json v0.9.11 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/google/flatbuffers v2.0.8+incompatible // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/klauspost/asmfmt v1.3.2 // indirect | ||
github.com/klauspost/compress v1.15.9 // indirect | ||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect | ||
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect | ||
github.com/pierrec/lz4/v4 v4.1.15 // indirect | ||
github.com/spf13/afero v1.9.3 // indirect | ||
github.com/spf13/cast v1.5.0 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/subosito/gotenv v1.4.2 // indirect | ||
github.com/zeebo/xxh3 v1.0.2 // indirect | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect | ||
golang.org/x/net v0.5.0 // indirect | ||
golang.org/x/sys v0.4.0 // indirect | ||
golang.org/x/text v0.6.0 // indirect | ||
golang.org/x/tools v0.1.12 // indirect | ||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | ||
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect | ||
google.golang.org/grpc v1.52.0 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.