Skip to content

Commit

Permalink
run: disable tarantool flag parsing
Browse files Browse the repository at this point in the history
    Disabled tarantool flag parsing for 'run' command.
    Done that so there will be no more differences in behaviour
    between 'tt run' and 'tarantool'.
    e.g positional flag parsing, missing/more flags than tarantool has.

    Closes #590
  • Loading branch information
better0fdead authored and psergee committed Dec 27, 2023
1 parent afb76ff commit 744a4c7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 80 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Module ``tt replicaset``, to manage replicasets:
- ``tt replicaset status`` to show a cluster status information.

### Changed

- Disable ``tt run`` tarantool flag parsing.

## [2.1.0] - 2023-12-15

### Changed
Expand All @@ -20,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `tt connect` auto-completion shows directories and files when there are no
running apps.
- `tt rocks --server` now accepts several URL's.
- Disable ``tt run`` tarantool flag parsing.

### Added

Expand Down
61 changes: 27 additions & 34 deletions cli/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,13 @@ import (
)

var (
// runEval contains "-e" flag content.
runEval string
// runLib contains "-l" flag content.
runLib string
// runInteractive contains "-I" flag content.
runInteractive bool
// runStdin contains "-" flag content.
runStdin string
// runVersion contains "-v" flag content.
runVersion bool
// runArgs contains command args.
runArgs []string
)

func newRunOpts(cmdCtx cmdcontext.CmdCtx) *running.RunOpts {
return &running.RunOpts{
func newRunInfo(cmdCtx cmdcontext.CmdCtx) *running.RunInfo {
return &running.RunInfo{
CmdCtx: cmdCtx,
RunFlags: running.RunFlags{
RunEval: runEval,
RunLib: runLib,
RunInteractive: runInteractive,
RunStdin: runStdin,
RunVersion: runVersion,
RunArgs: runArgs,
},
}
}

Expand All @@ -49,8 +31,15 @@ func NewRunCmd() *cobra.Command {
All command line arguments are passed to the interpreted SCRIPT. Options to process in the SCRIPT
are passed after '--'.
`,
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
cmdCtx.CommandName = cmd.Name()
for _, opt := range args {
if opt == "-h" || opt == "--help" {
cmd.Help()
return
}
}
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalRunModule, args)
handleCmdErr(cmd, err)
},
Expand Down Expand Up @@ -86,12 +75,6 @@ is after '--', so passed to script.lua as is.
DisableFlagsInUseLine: true,
}

runCmd.Flags().StringVarP(&runEval, "evaluate", "e", "", "execute string 'EXPR'")
runCmd.Flags().StringVarP(&runLib, "library", "l", "", "require library 'NAME'")
runCmd.Flags().BoolVarP(&runInteractive, "interactive", "i", false,
"enter interactive mode after executing 'SCRIPT'")
runCmd.Flags().BoolVarP(&runVersion, "version", "v", false, "print used tarantool version")

return runCmd
}

Expand All @@ -101,20 +84,30 @@ func internalRunModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
return errNoConfig
}

runOpts := newRunOpts(*cmdCtx)
runInfo := newRunInfo(*cmdCtx)
scriptPath := ""
startIndex := 0
if len(args) > 0 {
if strings.HasSuffix(args[0], ".lua") {
scriptPath = args[0]
if _, err := os.Stat(scriptPath); err != nil {
return fmt.Errorf("there was some problem locating script: %s", err)
for i, option := range args {
if strings.HasSuffix(option, ".lua") {
scriptPath = option
if _, err := os.Stat(scriptPath); err != nil {
return fmt.Errorf("there was some problem locating script: %s", err)
}
startIndex = i
break
}
startIndex = 1
}
}
runOpts.RunFlags.RunArgs = args[startIndex:]
if err := running.Run(runOpts, scriptPath); err != nil {

if len(args) != 0 && scriptPath != "" {
runInfo.RunOpts.RunArgs = args[startIndex+1:]
runInfo.RunOpts.RunFlags = args[:startIndex]
} else if scriptPath == "" {
runInfo.RunOpts.RunFlags = args
}

if err := running.Run(runInfo, scriptPath); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions cli/running/cluster_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ func (inst *clusterInstance) Start() error {
}

// Run runs tarantool instance.
func (inst *clusterInstance) Run(flags RunFlags) error {
func (inst *clusterInstance) Run(opts RunOpts) error {
newInstanceEnv := os.Environ()
args := []string{inst.tarantoolPath}
args = append(args, convertFlagsToTarantoolOpts(flags)...)
args = append(args, flags.RunArgs...)

f, err := integrity.FileRepository.Read(inst.tarantoolPath)
if err != nil {
return err
}
f.Close()

args = append(args, opts.RunFlags...)
args = append(args, opts.RunArgs...)
log.Debugf("Running Tarantool with args: %s", strings.Join(args[1:], " "))
execErr := syscall.Exec(inst.tarantoolPath, args, newInstanceEnv)
if execErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/running/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Instance interface {
Start() error

// Run runs tarantool interpreter.
Run(flags RunFlags) error
Run(opts RunOpts) error

// Wait waits for the process to complete.
Wait() error
Expand Down
28 changes: 10 additions & 18 deletions cli/running/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,19 @@ type InstanceCtx struct {
// AppHashes
}

// RunFlags contains flags for tt run.
type RunFlags struct {
// RunEval contains "-e" flag content.
RunEval string
// RunLib contains "-l" flag content.
RunLib string
// RunInteractive contains "-i" flag content.
RunInteractive bool
// RunStdin contains "-" flag content.
RunStdin string
// RunVersion contains "-v" flag content.
RunVersion bool
// RunOpts contains flags and args for tt run.
type RunOpts struct {
// RunArgs contains command args.
RunArgs []string
// RunFlags contains command flags.
RunFlags []string
}

// RunOpts contains information for tt run.
type RunOpts struct {
// RunInfo contains information for tt run.
type RunInfo struct {
CmdCtx cmdcontext.CmdCtx
RunningCtx RunningCtx
RunFlags RunFlags
RunOpts RunOpts
}

// providerImpl is an implementation of Provider interface.
Expand Down Expand Up @@ -683,10 +675,10 @@ func Stop(run *InstanceCtx) error {
}

// Run runs an Instance.
func Run(runOpts *RunOpts, scriptPath string) error {
inst := scriptInstance{tarantoolPath: runOpts.CmdCtx.Cli.TarantoolCli.Executable,
func Run(runInfo *RunInfo, scriptPath string) error {
inst := scriptInstance{tarantoolPath: runInfo.CmdCtx.Cli.TarantoolCli.Executable,
appPath: scriptPath}
err := inst.Run(runOpts.RunFlags)
err := inst.Run(runInfo.RunOpts)
return err
}

Expand Down
27 changes: 3 additions & 24 deletions cli/running/script_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,19 @@ func (inst *scriptInstance) Start() error {
}

// Run runs tarantool instance.
func (inst *scriptInstance) Run(flags RunFlags) error {
func (inst *scriptInstance) Run(opts RunOpts) error {
f, err := integrity.FileRepository.Read(inst.tarantoolPath)
if err != nil {
return err
}
f.Close()

newInstanceEnv := os.Environ()
newInstanceEnv = append(newInstanceEnv,
"TT_CLI_INSTANCE="+inst.appPath,
"TT_CLI=true",
)
args := []string{inst.tarantoolPath}
args = append(args, convertFlagsToTarantoolOpts(flags)...)
args = append(args, opts.RunFlags...)
if inst.appPath != "" {
log.Debugf("Script to run: %s", inst.appPath)

Expand All @@ -243,7 +242,7 @@ func (inst *scriptInstance) Run(flags RunFlags) error {
// Enable reading from input for Tarantool.
args = append(args, "-")
}
args = append(args, flags.RunArgs...)
args = append(args, opts.RunArgs...)
log.Debugf("Running Tarantool with args: %s", strings.Join(args[1:], " "))
execErr := syscall.Exec(inst.tarantoolPath, args, newInstanceEnv)
if execErr != nil {
Expand Down Expand Up @@ -286,23 +285,3 @@ func (inst *scriptInstance) Stop(waitTimeout time.Duration) error {
}
return inst.processController.Stop(waitTimeout)
}

// convertFlagsToTarantoolOpts turns flags into tarantool command.
func convertFlagsToTarantoolOpts(flags RunFlags) []string {
var command []string
if flags.RunEval != "" {
command = append(command, "-e")
command = append(command, flags.RunEval)
}
if flags.RunInteractive {
command = append(command, "-i")
}
if flags.RunLib != "" {
command = append(command, "-l")
command = append(command, flags.RunLib)
}
if flags.RunVersion {
command = append(command, "-v")
}
return command
}

0 comments on commit 744a4c7

Please sign in to comment.