Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 14, 2024
1 parent 23bab64 commit 7672e25
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 203 deletions.
36 changes: 22 additions & 14 deletions cmd/ctrlc/root/agent/run/run.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
package run

import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/ctrlplanedev/cli/pkg/agent"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewAgentRunCmd() *cobra.Command {
var proxyAddr string
var agentName string
var workspace string
var labels []string
var metadata map[string]string
var insecure bool
var targetId string

cmd := &cobra.Command{
Use: "run",
Short: "Run the agent",
Long: `Run the agent to establish connection with the proxy.`,
RunE: func(cmd *cobra.Command, args []string) error {
metadata := make(map[string]string)
for _, label := range labels {
key, value, found := strings.Cut(label, "=")
if !found {
return fmt.Errorf("invalid label format %q, expected key=value", label)
}
metadata[key] = value
if proxyAddr == "" {
proxyAddr = viper.GetString("url")
proxyAddr = strings.TrimPrefix(proxyAddr, "https://")
proxyAddr = strings.TrimPrefix(proxyAddr, "http://")
}

proxyAddr := "wss://" + proxyAddr
if insecure {
proxyAddr = "ws://" + proxyAddr
} else {
proxyAddr = "wss://" + proxyAddr
}

apiKey := os.Getenv("CTRLPLANE_API_KEY")
agent := agent.NewAgent(
proxyAddr,
agentName,
if apiKey == "" {
apiKey = viper.GetString("api-key")
}

opts := []func(*agent.Agent){
agent.WithMetadata(metadata),
agent.WithHeader("X-API-Key", apiKey),
agent.WithHeader("X-Workspace", workspace),
}

agent := agent.NewAgent(
proxyAddr,
agentName,
opts...,
)

backoff := time.Second
Expand All @@ -68,7 +75,8 @@ func NewAgentRunCmd() *cobra.Command {
cmd.Flags().StringVarP(&proxyAddr, "proxy", "p", "app.ctrlplane.dev", "Proxy address to connect through")
cmd.Flags().StringVarP(&agentName, "name", "n", "", "Name for this agent")
cmd.Flags().StringVarP(&workspace, "workspace", "w", "", "Workspace for this agent")
cmd.Flags().StringSliceVarP(&labels, "labels", "l", []string{}, "Labels in the format key=value")
cmd.Flags().StringVarP(&targetId, "target", "t", "", "Target ID to link this agent too")
cmd.Flags().StringToStringVar(&metadata, "metadata", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)")
cmd.Flags().BoolVar(&insecure, "insecure", false, "Allow insecure connections")

cmd.MarkFlagRequired("workspace")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewReleaseChannelCmd() *cobra.Command {

// Add flags
cmd.Flags().StringVar(&name, "name", "", "Name of the release channel (required)")
cmd.Flags().StringVar(&deploymentID, "deployment-id", "", "ID of the deployment (required)")
cmd.Flags().StringVar(&deploymentID, "deployment", "", "ID of the deployment (required)")
cmd.Flags().StringVar(&description, "description", "", "Description of the release channel")
cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("deployment-id")
Expand Down
Loading

0 comments on commit 7672e25

Please sign in to comment.