Skip to content

feat: add support for executing files #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"strings"

_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
Expand All @@ -15,6 +16,7 @@ type RootArgs struct {
statements string
quiet bool
authToken string
file string
}

func NewRootCmd() *cobra.Command {
Expand Down Expand Up @@ -44,11 +46,38 @@ func NewRootCmd() *cobra.Command {
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.")

Expand Down