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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

BINARY_NAME=sitectl
DOCS_PORT ?= 3000
INSTALL_DIR ?= /usr/local/bin
INSTALL_DIR ?= $(or $(dir $(shell which $(BINARY_NAME) 2>/dev/null)),/usr/local/bin/)

deps:
go get .
Expand All @@ -12,7 +12,7 @@ build: deps
go build -o $(BINARY_NAME) .

install: build
sudo cp $(BINARY_NAME) $(INSTALL_DIR)/$(BINARY_NAME)
sudo cp $(BINARY_NAME) $(INSTALL_DIR)$(BINARY_NAME)
@if [ -d ../sitectl-isle ]; then $(MAKE) -C ../sitectl-isle install; fi
@if [ -d ../sitectl-drupal ]; then $(MAKE) -C ../sitectl-drupal install; fi

Expand Down
40 changes: 23 additions & 17 deletions cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ var (
componentSetDisposition string
componentSetTLSMode string
componentSetYolo bool
invokePluginCommand = func(pluginName string, args []string) error {
invokePluginCommand = func(pluginName, contextName string, args []string) error {
installed, ok := plugin.FindInstalled(pluginName)
if !ok {
return fmt.Errorf("plugin %q is not installed", pluginName)
}
_, err := pluginSDK.InvokePluginCommand(installed.Name, args, plugin.CommandExecOptions{
Stdin: RootCmd.InOrStdin(),
Stdout: RootCmd.OutOrStdout(),
Stderr: RootCmd.ErrOrStderr(),
invocation := make([]string, 0, len(args)+2)
if strings.TrimSpace(contextName) != "" {
invocation = append(invocation, "--context", contextName)
}
invocation = append(invocation, args...)
_, err := pluginSDK.InvokePluginCommand(installed.Name, invocation, plugin.CommandExecOptions{
Context: RootCmd.Context(),
Stdin: RootCmd.InOrStdin(),
Stdout: RootCmd.OutOrStdout(),
Stderr: RootCmd.ErrOrStderr(),
})
return err
}
Expand All @@ -51,7 +57,7 @@ var componentDescribeCmd = &cobra.Command{
Aliases: []string{"status"},
Short: "Describe the current component state",
RunE: func(cmd *cobra.Command, args []string) error {
owner, name, err := resolveComponentOwner(cmd, componentDescribeName)
contextName, owner, name, err := resolveComponentOwner(cmd, componentDescribeName)
if err != nil {
return err
}
Expand All @@ -73,7 +79,7 @@ var componentDescribeCmd = &cobra.Command{
invocation = append(invocation, "--format", componentDescribeFormat)
}

return invokePluginCommand(owner, invocation)
return invokePluginCommand(owner, contextName, invocation)
},
}

Expand All @@ -82,7 +88,7 @@ var componentReconcileCmd = &cobra.Command{
Aliases: []string{"review", "align"},
Short: "Review and reconcile component state",
RunE: func(cmd *cobra.Command, args []string) error {
owner, name, err := resolveComponentOwner(cmd, componentReconcileName)
contextName, owner, name, err := resolveComponentOwner(cmd, componentReconcileName)
if err != nil {
return err
}
Expand All @@ -107,7 +113,7 @@ var componentReconcileCmd = &cobra.Command{
invocation = append(invocation, "--format", componentReconcileFormat)
}

return invokePluginCommand(owner, invocation)
return invokePluginCommand(owner, contextName, invocation)
},
}

Expand All @@ -116,7 +122,7 @@ var componentSetCmd = &cobra.Command{
Short: "Set a component disposition",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
owner, name, err := resolveComponentOwner(cmd, args[0])
contextName, owner, name, err := resolveComponentOwner(cmd, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +150,7 @@ var componentSetCmd = &cobra.Command{
invocation = append(invocation, "--yolo")
}

return invokePluginCommand(owner, invocation)
return invokePluginCommand(owner, contextName, invocation)
},
}

Expand Down Expand Up @@ -179,15 +185,15 @@ func init() {

var pluginSDK *plugin.SDK

func resolveComponentOwner(cmd *cobra.Command, raw string) (string, string, error) {
func resolveComponentOwner(cmd *cobra.Command, raw string) (string, string, string, error) {
contextName, err := config.ResolveCurrentContextName(cmd.Flags())
if err != nil {
return "", "", err
return "", "", "", err
}

ctx, err := config.GetContext(contextName)
if err != nil {
return "", "", err
return "", "", "", err
}

owner := ctx.Plugin
Expand All @@ -197,12 +203,12 @@ func resolveComponentOwner(cmd *cobra.Command, raw string) (string, string, erro
name = componentName
}
if strings.TrimSpace(owner) == "" {
return "", "", fmt.Errorf("context %q does not define a plugin owner", ctx.Name)
return "", "", "", fmt.Errorf("context %q does not define a plugin owner", ctx.Name)
}
if owner == "core" {
return "", "", fmt.Errorf("context %q uses plugin %q; component commands require a stack plugin such as isle", ctx.Name, owner)
return "", "", "", fmt.Errorf("context %q uses plugin %q; component commands require a stack plugin such as isle", ctx.Name, owner)
}
return owner, name, nil
return contextName, owner, name, nil
}

func splitNamespacedComponent(raw string) (string, string, bool) {
Expand Down
10 changes: 8 additions & 2 deletions cmd/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ func TestResolveComponentOwnerUsesNamespace(t *testing.T) {
t.Fatalf("Set(context) error = %v", err)
}

owner, name, err := resolveComponentOwner(cmd, "drupal/modules")
contextName, owner, name, err := resolveComponentOwner(cmd, "drupal/modules")
if err != nil {
t.Fatalf("resolveComponentOwner() error = %v", err)
}
if contextName != "museum" {
t.Fatalf("unexpected context name: %q", contextName)
}
if owner != "drupal" || name != "modules" {
t.Fatalf("unexpected owner/name: %q %q", owner, name)
}
Expand All @@ -68,10 +71,13 @@ func TestResolveComponentOwnerFallsBackToContextPlugin(t *testing.T) {
t.Fatalf("Set(context) error = %v", err)
}

owner, name, err := resolveComponentOwner(cmd, "fcrepo")
contextName, owner, name, err := resolveComponentOwner(cmd, "fcrepo")
if err != nil {
t.Fatalf("resolveComponentOwner() error = %v", err)
}
if contextName != "museum" {
t.Fatalf("unexpected context name: %q", contextName)
}
if owner != "isle" || name != "fcrepo" {
t.Fatalf("unexpected owner/name: %q %q", owner, name)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Examples:
cmdArgs = append(cmdArgs, filteredArgs...)
c := exec.Command("docker", cmdArgs...)
c.Dir = context.ProjectDir
_, err = context.RunCommand(c)
_, err = context.RunCommandContext(cmd.Context(), c)
if err != nil {
return err
}
Expand Down
47 changes: 0 additions & 47 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,6 @@ func promptRemoteEnvironmentContext(localCtx, previousRemote *config.Context) (*
localCtx.ComposeNetwork,
localCtx.EffectiveComposeNetwork(),
)
runSudo := remoteContextBool(previousRemote, func(ctx *config.Context) bool { return ctx.RunSudo }, localCtx.RunSudo)

for {
hostname, err = promptRequiredValueWithDefault("Remote hostname/domain (e.g. stage.example.com)", hostname)
if err != nil {
Expand Down Expand Up @@ -808,7 +806,6 @@ func promptRemoteEnvironmentContext(localCtx, previousRemote *config.Context) (*
SSHUser: sshUser,
SSHPort: sshPort,
SSHKeyPath: sshKey,
RunSudo: localCtx.RunSudo,
DockerSocket: dockerSocket,
ComposeFile: append([]string{}, localCtx.ComposeFile...),
EnvFile: append([]string{}, localCtx.EnvFile...),
Expand All @@ -820,7 +817,6 @@ func promptRemoteEnvironmentContext(localCtx, previousRemote *config.Context) (*
remoteCtx.ProjectName = projectName
remoteCtx.ComposeProjectName = composeProjectName
remoteCtx.ComposeNetwork = composeNetwork
remoteCtx.RunSudo = runSudo
if detected := config.DetectContextComposeNetwork(remoteCtx); detected != "" {
remoteCtx.ComposeNetwork = detected
}
Expand Down Expand Up @@ -911,37 +907,6 @@ func promptUintWithDefault(label string, defaultValue uint) (uint, error) {
return uint(parsed), nil
}

func promptBooleanChoice(label string, defaultValue bool) (bool, error) {
defaultChoice := "no"
if defaultValue {
defaultChoice = "yes"
}
value, err := createConfigPromptChoice(
strings.ToLower(strings.ReplaceAll(label, " ", "-")),
[]corecomponent.Choice{
{
Value: "yes",
Label: "yes",
Help: label,
Aliases: []string{"y", "1"},
},
{
Value: "no",
Label: "no",
Help: "Do not use sudo.",
Aliases: []string{"n", "2"},
},
},
defaultChoice,
createConfigInput,
strings.Split(corecomponent.RenderSection(label, label+"?"), "\n")...,
)
if err != nil {
return false, err
}
return strings.TrimSpace(value) == "yes", nil
}

func validateRemoteDockerAccess(ctx *config.Context) error {
if ctx == nil || ctx.DockerHostType != config.ContextRemote {
return nil
Expand Down Expand Up @@ -988,16 +953,11 @@ func validateRemoteDockerAccess(ctx *config.Context) error {
if promptErr != nil {
return promptErr
}
runSudo, promptErr := promptBooleanChoice("Run Docker commands with sudo", ctx.RunSudo)
if promptErr != nil {
return promptErr
}
ctx.ProjectDir = projectDir
ctx.ProjectName = projectName
ctx.ComposeProjectName = firstNonEmptyString(ctx.ComposeProjectName, projectName)
ctx.ComposeNetwork = firstNonEmptyString(config.DetectContextComposeNetwork(ctx), ctx.ComposeNetwork, ctx.EffectiveComposeNetwork())
ctx.DockerSocket = dockerSocket
ctx.RunSudo = runSudo
continue
}
return nil
Expand Down Expand Up @@ -1034,13 +994,6 @@ func remoteContextUint(ctx *config.Context, getter func(*config.Context) uint, f
return fallback
}

func remoteContextBool(ctx *config.Context, getter func(*config.Context) bool, fallback bool) bool {
if ctx == nil || getter == nil {
return fallback
}
return getter(ctx)
}

func suggestedEnvironmentContextName(localCtx *config.Context, environment string) string {
base := strings.TrimSpace(environment)
if localCtx == nil {
Expand Down
8 changes: 0 additions & 8 deletions cmd/config_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,6 @@ func TestRunCreateConfigRepromptsDockerSettingsAfterComposePSFailure(t *testing.
if ctx.DockerSocket != "/run/user/1000/docker.sock" {
t.Fatalf("expected updated docker socket, got %q", ctx.DockerSocket)
}
if !ctx.RunSudo {
t.Fatal("expected updated run sudo true")
}
if ctx.ProjectName != "museum-prod" {
t.Fatalf("expected updated project name museum-prod, got %q", ctx.ProjectName)
}
Expand All @@ -762,8 +759,6 @@ func TestRunCreateConfigRepromptsDockerSettingsAfterComposePSFailure(t *testing.
return "no", nil
case "update-environment-context":
return "update", nil
case "run-docker-commands-with-sudo":
return "yes", nil
default:
t.Fatalf("unexpected choice prompt: %s", name)
return "", nil
Expand Down Expand Up @@ -811,9 +806,6 @@ func TestRunCreateConfigRepromptsDockerSettingsAfterComposePSFailure(t *testing.
if remoteCtx.Plugin != "core" {
t.Fatalf("expected saved plugin core, got %q", remoteCtx.Plugin)
}
if !remoteCtx.RunSudo {
t.Fatal("expected saved run sudo true")
}
}

func TestInheritNewContextDefaultsFromActive(t *testing.T) {
Expand Down
Loading
Loading