generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typing just "ftl" will enter the interactive CLI No completion or anything else yet, but it works fine. <img width="585" alt="image" src="https://github.com/user-attachments/assets/7143ff27-8575-4f77-9381-c74a23f91e66">
- Loading branch information
1 parent
d3c2947
commit a0e0272
Showing
5 changed files
with
109 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/chzyer/readline" | ||
"github.com/kballard/go-shellquote" | ||
|
||
"github.com/TBD54566975/ftl/internal/projectconfig" | ||
) | ||
|
||
type interactiveCmd struct { | ||
} | ||
|
||
func (i *interactiveCmd) Run(ctx context.Context, k *kong.Kong, projectConfig projectconfig.Config) error { | ||
l, err := readline.NewEx(&readline.Config{ | ||
Prompt: "\033[32m>\033[0m ", | ||
InterruptPrompt: "^C", | ||
EOFPrompt: "exit", | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("init readline: %w", err) | ||
} | ||
l.CaptureExitSignal() | ||
for { | ||
line, err := l.Readline() | ||
if errors.Is(err, readline.ErrInterrupt) { | ||
if len(line) == 0 { | ||
break | ||
} | ||
continue | ||
} else if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
line = strings.TrimSpace(line) | ||
args, err := shellquote.Split(line) | ||
if err != nil { | ||
errorf("%s", err) | ||
continue | ||
} | ||
kctx, err := k.Parse(args) | ||
if err != nil { | ||
errorf("%s", err) | ||
continue | ||
} | ||
subctx := bindContext(ctx, kctx, projectConfig) | ||
|
||
err = kctx.Run(subctx) | ||
if err != nil { | ||
errorf("%s", err) | ||
continue | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func errorf(format string, args ...any) { | ||
fmt.Printf("\033[31m%s\033[0m\n", fmt.Sprintf(format, args...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.