Skip to content
Open
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
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ LDFLAGS = -X github.com/ctrlplanedev/cli/cmd/ctrlc/root/version.Version=$(VERSIO
-X github.com/ctrlplanedev/cli/cmd/ctrlc/root/version.GitCommit=$(COMMIT) \
-X github.com/ctrlplanedev/cli/cmd/ctrlc/root/version.BuildDate=$(DATE)

.PHONY: build
build:
go build -ldflags "$(LDFLAGS)" -o bin/ctrlc ./cmd/ctrlc

.PHONY: install
install:
go install -ldflags "$(LDFLAGS)" ./cmd/ctrlc

.PHONY: test
test:
go test -v ./...

.PHONY: clean
fmt:
gofmt -w -s -e .

lint:
golangci-lint run

clean:
rm -rf bin/
rm -rf bin/

.PHONY: build install test clean
62 changes: 52 additions & 10 deletions cmd/ctrlc/root/run/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package exec

import (
"fmt"
"os"
"os/signal"
"runtime"
"syscall"

"github.com/MakeNowJust/heredoc/v2"
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/pkg/jobagent"
Expand All @@ -11,34 +16,71 @@ import (
)

func NewRunExecCmd() *cobra.Command {
return &cobra.Command{
var (
name string
jobAgentType = "exec-bash"
)

if runtime.GOOS == "windows" {
jobAgentType = "exec-powershell"
}

cmd := &cobra.Command{
Use: "exec",
Short: "Execute commands directly when a job is received",
Example: heredoc.Doc(`
$ ctrlc run exec --name "my-script-agent" --workspace 123e4567-e89b-12d3-a456-426614174000
`),
RunE: func(cmd *cobra.Command, args []string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspaceId := viper.GetString("workspace")
if name == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could move input validation above the client creation?

return fmt.Errorf("name is required")
}
if workspaceId == "" {
return fmt.Errorf("workspace is required")
}
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}
runner := NewExecRunner(client)
jobAgentConfig := api.UpsertJobAgentJSONRequestBody{
Name: name,
Type: jobAgentType,
WorkspaceId: workspaceId,
}
ja, err := jobagent.NewJobAgent(
client,
api.UpsertJobAgentJSONRequestBody{
Name: "exec",
Type: "exec",
},
&ExecRunner{},
jobAgentConfig,
runner,
)
if err != nil {
return fmt.Errorf("failed to create job agent: %w", err)
}

// Set up a simple shutdown handler for non-interval mode
// When used with AddIntervalSupport, this would only affect a single iteration
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Info("Shutting down gracefully...")
runner.ExitAll(true)
}()

// Run job check - AddIntervalSupport will handle repeated execution
if err := ja.RunQueuedJobs(); err != nil {
log.Error("failed to run queued jobs", "error", err)
}
if err := ja.UpdateRunningJobs(); err != nil {
log.Error("failed to check for jobs", "error", err)
return fmt.Errorf("failed to run queued jobs: %w", err)
}

return nil
},
}

cmd.Flags().StringVar(&name, "name", "", "Name of the job agent")
cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("workspace")
return cmd
}
Loading