Skip to content

Commit

Permalink
Revert "allow lakectl local to be "git data" (#7618)" (#7654)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozkatz committed Apr 14, 2024
1 parent bbd8f56 commit 090edc8
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 243 deletions.
29 changes: 16 additions & 13 deletions cmd/lakectl/cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func localDiff(ctx context.Context, client apigen.ClientWithResponsesInterface,
}

func localHandleSyncInterrupt(ctx context.Context, idx *local.Index, operation string) context.Context {
cmdName := ctx.Value(lakectlLocalCommandNameKey).(string)
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
go func() {
defer stop()
Expand All @@ -88,28 +87,27 @@ func localHandleSyncInterrupt(ctx context.Context, idx *local.Index, operation s
if err != nil {
WriteTo("{{.Error|red}}\n", struct{ Error string }{Error: "Failed to write failed operation to index file."}, os.Stderr)
}
DieFmt(`Operation was canceled, local data may be incomplete.
Use "%s checkout..." to sync with the remote.`, cmdName)
Die(`Operation was canceled, local data may be incomplete.
Use "lakectl local checkout..." to sync with the remote.`, 1)
}()
return ctx
}

func dieOnInterruptedOperation(ctx context.Context, interruptedOperation LocalOperation, force bool) {
cmdName := ctx.Value(lakectlLocalCommandNameKey).(string)
func dieOnInterruptedOperation(interruptedOperation LocalOperation, force bool) {
if !force && interruptedOperation != "" {
switch interruptedOperation {
case commitOperation:
DieFmt(`Latest commit operation was interrupted, data may be incomplete.
Use "%s commit..." to commit your latest changes or "lakectl local pull... --force" to sync with the remote.`, cmdName)
Die(`Latest commit operation was interrupted, data may be incomplete.
Use "lakectl local commit..." to commit your latest changes or "lakectl local pull... --force" to sync with the remote.`, 1)
case checkoutOperation:
DieFmt(`Latest checkout operation was interrupted, local data may be incomplete.
Use "%s checkout..." to sync with the remote.`, cmdName)
Die(`Latest checkout operation was interrupted, local data may be incomplete.
Use "lakectl local checkout..." to sync with the remote.`, 1)
case pullOperation:
DieFmt(`Latest pull operation was interrupted, local data may be incomplete.
Use "%s pull... --force" to sync with the remote.`, cmdName)
Die(`Latest pull operation was interrupted, local data may be incomplete.
Use "lakectl local pull... --force" to sync with the remote.`, 1)
case cloneOperation:
DieFmt(`Latest clone operation was interrupted, local data may be incomplete.
Use "%s checkout..." to sync with the remote or run "lakectl local clone..." with a different directory to sync with the remote.`, cmdName)
Die(`Latest clone operation was interrupted, local data may be incomplete.
Use "lakectl local checkout..." to sync with the remote or run "lakectl local clone..." with a different directory to sync with the remote.`, 1)
default:
panic(fmt.Errorf("found an unknown interrupted operation in the index file: %s- %w", interruptedOperation, ErrUnknownOperation))
}
Expand All @@ -120,3 +118,8 @@ var localCmd = &cobra.Command{
Use: "local",
Short: "Sync local directories with lakeFS paths",
}

//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(localCmd)
}
60 changes: 0 additions & 60 deletions cmd/lakectl/cmd/local_install.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/lakectl/cmd/local_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var localPullCmd = &cobra.Command{
DieErr(err)
}

dieOnInterruptedOperation(cmd.Context(), LocalOperation(idx.ActiveOperation), force)
dieOnInterruptedOperation(LocalOperation(idx.ActiveOperation), force)

currentBase := remote.WithRef(idx.AtHead)
// make sure no local changes
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakectl/cmd/local_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var localStatusCmd = &cobra.Command{
DieErr(err)
}

dieOnInterruptedOperation(cmd.Context(), LocalOperation(idx.ActiveOperation), false)
dieOnInterruptedOperation(LocalOperation(idx.ActiveOperation), false)

remoteBase := remote.WithRef(idx.AtHead)
client := getClient()
Expand Down
153 changes: 66 additions & 87 deletions cmd/lakectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"golang.org/x/exp/slices"
)

type lakectlLocalContextKey string

const (
DefaultMaxIdleConnsPerHost = 100
// version templates
Expand All @@ -51,7 +49,6 @@ lakeFS version: {{.LakeFSVersion}}
Get the latest release {{ .UpgradeURL|blue }}
{{- end }}
`
lakectlLocalCommandNameKey lakectlLocalContextKey = "lakectl-local-command-name"
)

// Configuration is the user-visible configuration structure in Golang form.
Expand Down Expand Up @@ -285,63 +282,63 @@ func getKV(cmd *cobra.Command, name string) (map[string]string, error) { //nolin
return kv, nil
}

func rootPreRun(cmd *cobra.Command, _ []string) {
logging.SetLevel(logLevel)
logging.SetOutputFormat(logFormat)
err := logging.SetOutputs(logOutputs, 0, 0)
if err != nil {
DieFmt("Failed to setup logging: %s", err)
}
if noColorRequested {
DisableColors()
}
if cmd == configCmd {
return
}

switch {
case cfgErr == nil:
logging.ContextUnavailable().
WithField("file", viper.ConfigFileUsed()).
Debug("loaded configuration from file")
case errors.As(cfgErr, &viper.ConfigFileNotFoundError{}):
if cfgFile != "" {
// specific message in case the file isn't found
DieFmt("config file not found, please run \"lakectl config\" to create one\n%s\n", cfgErr)
// rootCmd represents the base command when called without any sub-commands
var rootCmd = &cobra.Command{
Use: "lakectl",
Short: "A cli tool to explore manage and work with lakeFS",
Long: `lakectl is a CLI tool allowing exploration and manipulation of a lakeFS environment`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logging.SetLevel(logLevel)
logging.SetOutputFormat(logFormat)
err := logging.SetOutputs(logOutputs, 0, 0)
if err != nil {
DieFmt("Failed to setup logging: %s", err)
}
if noColorRequested {
DisableColors()
}
if cmd == configCmd {
return
}
case cfgErr != nil:
DieFmt("error reading configuration file: %v", cfgErr)
}

err = viper.UnmarshalExact(&cfg, viper.DecodeHook(
mapstructure.ComposeDecodeHookFunc(
lakefsconfig.DecodeOnlyString,
mapstructure.StringToTimeDurationHookFunc())))
if err != nil {
DieFmt("error unmarshal configuration: %v", err)
}

if cmd.HasParent() {
// Don't send statistics for root command or if one of the excluding
var cmdName string
for curr := cmd; curr.HasParent(); curr = curr.Parent() {
if cmdName != "" {
cmdName = curr.Name() + "_" + cmdName
} else {
cmdName = curr.Name()
if cfgErr == nil {
logging.ContextUnavailable().
WithField("file", viper.ConfigFileUsed()).
Debug("loaded configuration from file")
} else if errors.As(cfgErr, &viper.ConfigFileNotFoundError{}) {
if cfgFile != "" {
// specific message in case the file isn't found
DieFmt("config file not found, please run \"lakectl config\" to create one\n%s\n", cfgErr)
}
// if the config file wasn't provided, try to run using the default values + env vars
} else if cfgErr != nil {
// other errors while reading the config file
DieFmt("error reading configuration file: %v", cfgErr)
}
if !slices.Contains(excludeStatsCmds, cmdName) {
sendStats(cmd.Context(), getClient(), cmdName)

err = viper.UnmarshalExact(&cfg, viper.DecodeHook(
mapstructure.ComposeDecodeHookFunc(
lakefsconfig.DecodeOnlyString,
mapstructure.StringToTimeDurationHookFunc())))
if err != nil {
DieFmt("error unmarshal configuration: %v", err)
}
}
}

// rootCmd represents the base command when called without any sub-commands
var rootCmd = &cobra.Command{
Use: "lakectl",
Short: "A cli tool to explore manage and work with lakeFS",
Long: `lakectl is a CLI tool allowing exploration and manipulation of a lakeFS environment`,
if cmd.HasParent() {
// Don't send statistics for root command or if one of the excluding
var cmdName string
for curr := cmd; curr.HasParent(); curr = curr.Parent() {
if cmdName != "" {
cmdName = curr.Name() + "_" + cmdName
} else {
cmdName = curr.Name()
}
}
if !slices.Contains(excludeStatsCmds, cmdName) {
sendStats(cmd.Context(), getClient(), cmdName)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
if !Must(cmd.Flags().GetBool("version")) {
if err := cmd.Help(); err != nil {
Expand Down Expand Up @@ -462,47 +459,29 @@ func getClient() *apigen.ClientWithResponses {
return client
}

func getBasename() string {
return strings.ToLower(filepath.Base(os.Args[0]))
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
ctx := context.Background()

var cmd *cobra.Command
baseName := getBasename()
switch baseName {
case "git", "git.exe", "git-data", "git-data.exe":
cmd = localCmd
cmd.Use = baseName
cmd.SetContext(context.WithValue(ctx, lakectlLocalCommandNameKey, baseName))
default:
rootCmd.AddCommand(localCmd)
cmd = rootCmd
cmd.SetContext(context.WithValue(ctx, lakectlLocalCommandNameKey, "lakectl local"))
}
// make sure config is properly initialize
setupRootCommand(cmd)
cobra.OnInitialize(initConfig)
cmd.PersistentPreRun = rootPreRun
// run!
err := cmd.Execute()
err := rootCmd.Execute()
if err != nil {
DieErr(err)
}
}

func setupRootCommand(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.lakectl.yaml)")
cmd.PersistentFlags().BoolVar(&noColorRequested, "no-color", getEnvNoColor(), "don't use fancy output colors (default value can be set by NO_COLOR environment variable)")
cmd.PersistentFlags().StringVarP(&baseURI, "base-uri", "", os.Getenv("LAKECTL_BASE_URI"), "base URI used for lakeFS address parse")
cmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", "none", "set logging level")
cmd.PersistentFlags().StringVarP(&logFormat, "log-format", "", "", "set logging output format")
cmd.PersistentFlags().StringSliceVarP(&logOutputs, "log-output", "", []string{}, "set logging output(s)")
cmd.PersistentFlags().BoolVar(&verboseMode, "verbose", false, "run in verbose mode")
cmd.Flags().BoolP("version", "v", false, "version for lakectl")
//nolint:gochecknoinits
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.lakectl.yaml)")
rootCmd.PersistentFlags().BoolVar(&noColorRequested, "no-color", getEnvNoColor(), "don't use fancy output colors (default value can be set by NO_COLOR environment variable)")
rootCmd.PersistentFlags().StringVarP(&baseURI, "base-uri", "", os.Getenv("LAKECTL_BASE_URI"), "base URI used for lakeFS address parse")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", "none", "set logging level")
rootCmd.PersistentFlags().StringVarP(&logFormat, "log-format", "", "", "set logging output format")
rootCmd.PersistentFlags().StringSliceVarP(&logOutputs, "log-output", "", []string{}, "set logging output(s)")
rootCmd.PersistentFlags().BoolVar(&verboseMode, "verbose", false, "run in verbose mode")
rootCmd.Flags().BoolP("version", "v", false, "version for lakectl")
}

func getEnvNoColor() bool {
Expand Down
24 changes: 0 additions & 24 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2507,30 +2507,6 @@ lakectl ingest --from <object store URI> --to <lakeFS path URI> [--dry-run] [fla



### lakectl install-git-plugin

set up `git data` (directory must exist and be in $PATH)

#### Synopsis
{:.no_toc}

Add a symlink to lakectl named `git-data`.
This allows calling `git data` and having it act as the `lakectl local` command
(as long as the symlink is within the executing users' $PATH environment variable

```
lakectl install-git-plugin <directory> [flags]
```

#### Options
{:.no_toc}

```
-h, --help help for install-git-plugin
```



### lakectl local

Sync local directories with lakeFS paths
Expand Down

0 comments on commit 090edc8

Please sign in to comment.