Skip to content

Commit

Permalink
make some flags "global" (PersistentFlags)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdevivo committed Oct 15, 2021
1 parent 11828e8 commit cd9e169
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
36 changes: 6 additions & 30 deletions cmd/pgsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ import (
"errors"
"os"
"strings"
"time"

"github.com/askgitdev/askgit/extensions"
"github.com/askgitdev/askgit/extensions/options"
"github.com/askgitdev/askgit/pkg/locator"
"github.com/askgitdev/askgit/pkg/pgsync"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"go.riyazali.net/sqlite"

_ "github.com/askgitdev/askgit/pkg/sqlite"
_ "github.com/mattn/go-sqlite3"
Expand All @@ -25,25 +19,7 @@ var pgsyncCmd = &cobra.Command{
Long: `Use this command to sync the results of an askgit query into a Postgres table`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
l := zerolog.New(os.Stderr).
Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.Stamp}).
Level(zerolog.DebugLevel).
With().
Timestamp().Logger()

// TODO(patrickdevivo) maybe there should be a "RegisterDefault" method that handles this boilerplate.
// Basically, register all the default functionality.
sqlite.Register(
extensions.RegisterFn(
options.WithExtraFunctions(),
options.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
options.WithGitHub(),
options.WithContextValue("githubToken", os.Getenv("GITHUB_TOKEN")),
options.WithContextValue("githubPerPage", os.Getenv("GITHUB_PER_PAGE")),
options.WithContextValue("githubRateLimit", os.Getenv("GITHUB_RATE_LIMIT")),
options.WithLogger(&l),
),
)
registerExt()

var schemaName string
tableName := args[0]
Expand All @@ -60,22 +36,22 @@ var pgsyncCmd = &cobra.Command{
var err error

if postgres, err = sql.Open("postgres", os.Getenv("POSTGRES_CONNECTION")); err != nil {
l.Error().Msgf("could not open postgres connection: %v", err)
logger.Error().Msgf("could not open postgres connection: %v", err)
return
}
defer func() {
if err := postgres.Close(); err != nil {
l.Error().Msgf("could not close postgres connection: %v", err)
logger.Error().Msgf("could not close postgres connection: %v", err)
}
}()

if askgit, err = sql.Open("sqlite3", ":memory:"); err != nil {
l.Error().Msgf("could not initialize askgit: %v", err)
logger.Error().Msgf("could not initialize askgit: %v", err)
return
}
defer func() {
if err := askgit.Close(); err != nil {
l.Error().Msgf("could not close askgit: %v", err)
logger.Error().Msgf("could not close askgit: %v", err)
}
}()

Expand All @@ -86,7 +62,7 @@ var pgsyncCmd = &cobra.Command{
// added mainly so we can run `SET statement_timeout 0` for connections to bit.io
if preamble := os.Getenv("PGSYNC_PREAMBLE"); preamble != "" {
if _, err := postgres.ExecContext(ctx, preamble); err != nil {
l.Error().Msgf("could not execute preamble: %v", err)
logger.Error().Msgf("could not execute preamble: %v", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func init() {
// local (root command only) flags
rootCmd.Flags().StringVarP(&format, "format", "f", "table", "specify the output format. Options are 'csv' 'tsv' 'table' 'single' and 'json'")
rootCmd.Flags().StringVarP(&presetQuery, "preset", "p", "", "used to pick a preset query")
rootCmd.Flags().StringVarP(&repo, "repo", "r", ".", "specify a path to a default repo on disk. This will be used if no repo is supplied as an argument to a git table")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "whether or not to print query execution logs to stderr")
rootCmd.PersistentFlags().StringVarP(&repo, "repo", "r", ".", "specify a path to a default repo on disk. This will be used if no repo is supplied as an argument to a git table")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "whether or not to print query execution logs to stderr")

// register the sqlite extension ahead of any command
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
Expand Down

0 comments on commit cd9e169

Please sign in to comment.