Skip to content
Merged
Show file tree
Hide file tree
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
676 changes: 0 additions & 676 deletions cmd/config.go

Large diffs are not rendered by default.

853 changes: 0 additions & 853 deletions cmd/config_create_test.go

This file was deleted.

85 changes: 84 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,29 @@ func runCreate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("no create definitions found; install a sitectl-* plugin that registers one")
}

forwarded := []string{}
if !createArgsContainFlag(args, "type") {
targetType, err := promptForCreateTarget()
if err != nil {
return err
}
forwarded = append(forwarded, "--type", string(targetType))
}

owner, spec, remaining, err := resolveCreateInvocation(plugins, args)
if err != nil {
return err
}
if !createArgsContainFlag(remaining, "checkout-source") && !createArgsContainFlag(args, "checkout-source") {
checkoutSource, sourceErr := promptForCheckoutSource(createForwardedTargetType(args, forwarded))
if sourceErr != nil {
return sourceErr
}
forwarded = append(forwarded, "--checkout-source", string(checkoutSource))
}

_, err = pluginSDK.InvokePluginCommand(owner, append([]string{"__create", spec.Name}, remaining...), plugin.CommandExecOptions{
invokeArgs := append([]string{"__create", spec.Name}, append(forwarded, remaining...)...)
_, err = pluginSDK.InvokePluginCommand(owner, invokeArgs, plugin.CommandExecOptions{
Context: RootCmd.Context(),
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Expand Down Expand Up @@ -124,6 +141,44 @@ func resolveCreateInvocation(plugins []plugin.InstalledPlugin, args []string) (s
return owner, spec, remaining, err
}

func promptForCreateTarget() (config.ContextType, error) {
selected, err := createPromptChoice(
"create target",
[]corecomponent.Choice{
{Value: string(config.ContextLocal), Label: "local", Help: "Run this stack on your local machine."},
{Value: string(config.ContextRemote), Label: "remote", Help: "Run this stack on a remote machine over SSH."},
},
string(config.ContextLocal),
createPromptInput,
strings.Split(corecomponent.RenderSection("Target machine", "Choose where this stack will run."), "\n")...,
)
if err != nil {
return "", err
}
return config.ContextType(strings.TrimSpace(selected)), nil
}

func promptForCheckoutSource(targetType config.ContextType) (plugin.CheckoutSource, error) {
defaultChoice := string(plugin.CheckoutSourceTemplate)
if targetType == config.ContextRemote {
defaultChoice = string(plugin.CheckoutSourceExisting)
}
selected, err := createPromptChoice(
"checkout source",
[]corecomponent.Choice{
{Value: string(plugin.CheckoutSourceTemplate), Label: "template", Help: "Clone the template repository as a fresh install."},
{Value: string(plugin.CheckoutSourceExisting), Label: "existing", Help: "Use a repo or checkout that already exists."},
},
defaultChoice,
createPromptInput,
strings.Split(corecomponent.RenderSection("Project source", "Choose whether to create from the template repository or use an existing checkout."), "\n")...,
)
if err != nil {
return "", err
}
return plugin.CheckoutSource(strings.TrimSpace(selected)), nil
}

func promptForCreatePlugin(plugins []plugin.InstalledPlugin) (plugin.InstalledPlugin, error) {
if len(plugins) == 1 {
return plugins[0], nil
Expand Down Expand Up @@ -258,3 +313,31 @@ func helpersFirstCreateRepo(installed plugin.InstalledPlugin, spec plugin.Create
}
return "-"
}

func createArgsContainFlag(args []string, name string) bool {
longFlag := "--" + name
for i, arg := range args {
if arg == longFlag {
return true
}
if strings.HasPrefix(arg, longFlag+"=") {
return true
}
if i > 0 && args[i-1] == longFlag {
return true
}
}
return false
}

func createForwardedTargetType(args, forwarded []string) config.ContextType {
for i, arg := range append([]string{}, append(forwarded, args...)...) {
if arg == "--type" && i+1 < len(append([]string{}, append(forwarded, args...)...)) {
return config.ContextType(strings.TrimSpace(append([]string{}, append(forwarded, args...)...)[i+1]))
}
if strings.HasPrefix(arg, "--type=") {
return config.ContextType(strings.TrimSpace(strings.TrimPrefix(arg, "--type=")))
}
}
return config.ContextLocal
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"strings"
"syscall"
"time"

"charm.land/fang/v2"
"github.com/libops/sitectl/pkg/config"
Expand Down Expand Up @@ -86,7 +87,10 @@ func init() {
}

func discoverAndRegisterPlugins() {
for _, discovered := range plugin.DiscoverInstalled() {
started := time.Now()
discoveredPlugins := plugin.DiscoverInstalledLightweight()
slog.Debug("registering discovered plugin commands", "count", len(discoveredPlugins), "duration", time.Since(started))
for _, discovered := range discoveredPlugins {
pluginName := discovered.Name
pluginPath := discovered.Path
binaryName := discovered.BinaryName
Expand Down
Loading