-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathroot.go
93 lines (75 loc) · 2.21 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cmd
import (
"fmt"
"os"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
"github.com/libsql/libsql-shell-go/pkg/shell"
"github.com/libsql/libsql-shell-go/pkg/shell/enums"
)
type RootArgs struct {
statements string
quiet bool
authToken string
file string
}
func NewRootCmd() *cobra.Command {
var rootArgs RootArgs = RootArgs{}
var rootCmd = &cobra.Command{
SilenceUsage: true,
Use: "libsql-shell <DB>",
Short: "A cli for executing SQL statements on a libSQL or SQLite database",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
shellConfig := shell.ShellConfig{
DbPath: args[0],
InF: cmd.InOrStdin(),
OutF: cmd.OutOrStdout(),
ErrF: cmd.ErrOrStderr(),
HistoryMode: enums.PerDatabaseHistory,
HistoryName: "libsql",
QuietMode: rootArgs.quiet,
AuthToken: rootArgs.authToken,
}
if cmd.Flag("exec").Changed {
if len(rootArgs.statements) == 0 {
return fmt.Errorf("no SQL command to execute")
}
return shell.RunShellLine(shellConfig, rootArgs.statements)
}
if cmd.Flag("from-file").Changed {
if len(rootArgs.file) == 0 {
return fmt.Errorf("file not provided")
}
file, err := os.Open(rootArgs.file)
if err != nil {
return err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return err
}
content := make([]byte, stat.Size())
_, err = file.Read(content)
if err != nil {
return err
}
return shell.RunShellLine(shellConfig, strings.TrimSpace(string(content)))
}
return shell.RunShell(shellConfig)
},
}
rootCmd.Flags().StringVarP(&rootArgs.statements, "exec", "e", "", "SQL statements separated by ;")
rootCmd.Flags().StringVarP(&rootArgs.file, "from-file", "f", "", "Execute commands from a file")
rootCmd.Flags().BoolVarP(&rootArgs.quiet, "quiet", "q", false, "Don't print welcome message")
rootCmd.Flags().StringVar(&rootArgs.authToken, "auth", "", "Add a JWT Token.")
return rootCmd
}
func Execute() {
var rootCmd *cobra.Command = NewRootCmd()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}