Skip to content

Commit 8791b32

Browse files
authored
fix: cleanup-folder-structure-for-cli-import (#51)
This PR contains a number of QOL fixes: 1. Create `version` package to be used by CLI and controller 2. Move internal code packages into `internal` folder 3. Allow for CLI code to be run from external libraries, aka `kagent` --------- Signed-off-by: Eitan Yarmush <eitan.yarmush@solo.io>
1 parent 7e1f4b4 commit 8791b32

58 files changed

Lines changed: 186 additions & 96 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ COPY pkg/ pkg/
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
24+
ARG LDFLAGS
25+
26+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags "$LDFLAGS" -o manager cmd/main.go
2527

2628
# Use distroless as minimal base image to package the manager binary
2729
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ BUILD_DATE := $(shell date -u '+%Y-%m-%d')
99
GIT_COMMIT := $(shell git rev-parse --short HEAD || echo "unknown")
1010
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null | sed 's/-dirty//' | grep v || echo "v0.0.1+$(GIT_COMMIT)")
1111

12+
13+
# Version information for the build
14+
LDFLAGS := "-X github.com/kagent-dev/kmcp/pkg/internal/version.Version=$(VERSION) \
15+
-X github.com/kagent-dev/kmcp/pkg/internal/version.GitCommit=$(GIT_COMMIT) \
16+
-X github.com/kagent-dev/kmcp/pkg/internal/version.BuildDate=$(BUILD_DATE)"
17+
1218
# Local architecture detection to build for the current platform
1319
LOCALARCH ?= $(shell uname -m | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')
1420

@@ -176,7 +182,7 @@ build: manifests generate fmt vet ## Build manager binary.
176182
.PHONY: build-cli
177183
build-cli: fmt vet ## Build kmcp CLI binary.
178184
mkdir -p $(DIST_FOLDER)
179-
go build -ldflags="-X 'github.com/kagent-dev/kmcp/cmd/kmcp/cmd.Version=$(VERSION)'" -o $(DIST_FOLDER)/kmcp cmd/kmcp/main.go
185+
go build -ldflags=$(LDFLAGS) -o $(DIST_FOLDER)/kmcp cmd/kmcp/main.go
180186

181187
.PHONY: run
182188
run: manifests generate fmt vet ## Run a controller from your host.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/kmcp/cmd/root.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

cmd/kmcp/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/kagent-dev/kmcp/cmd/kmcp/cmd"
7+
"github.com/kagent-dev/kmcp/pkg/cli"
88
)
99

1010
func main() {
11-
if err := cmd.Execute(); err != nil {
11+
if err := cli.Root().Execute(); err != nil {
1212
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
1313
os.Exit(1)
1414
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package cmd
1+
package commands
22

33
import (
44
"fmt"
55
"os"
66
"path/filepath"
77
"strings"
88

9-
"github.com/kagent-dev/kmcp/pkg/templates"
9+
"github.com/kagent-dev/kmcp/pkg/cli/internal/templates"
1010

11-
"github.com/kagent-dev/kmcp/pkg/frameworks"
12-
"github.com/kagent-dev/kmcp/pkg/manifest"
11+
"github.com/kagent-dev/kmcp/pkg/cli/internal/frameworks"
12+
"github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
1313
"github.com/spf13/cobra"
1414
)
1515

@@ -41,7 +41,7 @@ var (
4141
)
4242

4343
func init() {
44-
rootCmd.AddCommand(addToolCmd)
44+
addRootSubCmd(addToolCmd)
4545

4646
addToolCmd.Flags().StringVarP(&addToolDescription, "description", "d", "", "Tool description")
4747
addToolCmd.Flags().BoolVarP(&addToolForce, "force", "f", false, "Overwrite existing tool file")
@@ -87,7 +87,7 @@ func runAddTool(_ *cobra.Command, args []string) error {
8787
toolPath := filepath.Join("src", "tools", toolName+".py")
8888
toolExists := fileExists(toolPath)
8989

90-
if verbose {
90+
if Verbose {
9191
fmt.Printf("Tool file path: %s\n", toolPath)
9292
fmt.Printf("Tool exists: %v\n", toolExists)
9393
}
@@ -174,7 +174,7 @@ func createToolInteractive(toolName, projectRoot, framework string) error {
174174
}
175175

176176
func createTool(toolName, projectRoot, framework string) error {
177-
if verbose {
177+
if Verbose {
178178
fmt.Printf("Creating tool: %s\n", toolName)
179179
}
180180

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package cmd
1+
package commands
22

33
import (
44
"fmt"
55
"os"
66
"os/exec"
77
"strings"
88

9-
"github.com/kagent-dev/kmcp/pkg/manifest"
9+
"github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
1010
"github.com/stoewer/go-strcase"
1111

12-
"github.com/kagent-dev/kmcp/pkg/build"
12+
"github.com/kagent-dev/kmcp/pkg/cli/internal/build"
1313
"github.com/spf13/cobra"
1414
)
1515

@@ -37,7 +37,7 @@ var (
3737
)
3838

3939
func init() {
40-
rootCmd.AddCommand(buildCmd)
40+
addRootSubCmd(buildCmd)
4141

4242
buildCmd.Flags().StringVarP(&buildTag, "tag", "t", "", "Docker image tag (alias for --output)")
4343
buildCmd.Flags().BoolVar(&buildPush, "push", false, "Push Docker image to registry")
@@ -88,7 +88,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
8888
ProjectDir: buildDirectory,
8989
Tag: imageName,
9090
Platform: buildPlatform,
91-
Verbose: verbose,
91+
Verbose: Verbose,
9292
}
9393

9494
if err := builder.Build(opts); err != nil {
@@ -110,7 +110,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
110110
var err error
111111
clusterName, err = getCurrentKindClusterName()
112112
if err != nil {
113-
if verbose {
113+
if Verbose {
114114
fmt.Printf("could not detect kind cluster name: %v, using default\n", err)
115115
}
116116
clusterName = "kind" // default to kind cluster
@@ -129,7 +129,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
129129
}
130130

131131
func runDocker(args ...string) error {
132-
if verbose {
132+
if Verbose {
133133
fmt.Printf("Running: docker %s\n", strings.Join(args, " "))
134134
}
135135
cmd := exec.Command("docker", args...)
@@ -139,7 +139,7 @@ func runDocker(args ...string) error {
139139
}
140140

141141
func runKind(args ...string) error {
142-
if verbose {
142+
if Verbose {
143143
fmt.Printf("Running: kind %s\n", strings.Join(args, " "))
144144
}
145145
cmd := exec.Command("kind", args...)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package commands
22

33
import (
44
"bytes"
@@ -12,8 +12,8 @@ import (
1212
"time"
1313

1414
"github.com/kagent-dev/kmcp/api/v1alpha1"
15-
"github.com/kagent-dev/kmcp/pkg/manifest"
16-
"github.com/kagent-dev/kmcp/pkg/wellknown"
15+
"github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
16+
"github.com/kagent-dev/kmcp/pkg/cli/internal/wellknown"
1717
"github.com/spf13/cobra"
1818
corev1 "k8s.io/api/core/v1"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -79,7 +79,7 @@ var (
7979
)
8080

8181
func init() {
82-
rootCmd.AddCommand(deployCmd)
82+
addRootSubCmd(deployCmd)
8383

8484
// Get current namespace from kubeconfig
8585
currentNamespace, err := getCurrentNamespaceFromKubeconfig()
@@ -155,7 +155,7 @@ func runDeployMCP(_ *cobra.Command, args []string) error {
155155
// Set namespace
156156
mcpServer.Namespace = deployNamespace
157157

158-
if verbose {
158+
if Verbose {
159159
fmt.Printf("Generated MCPServer: %s/%s\n", mcpServer.Namespace, mcpServer.Name)
160160
}
161161

@@ -524,7 +524,7 @@ func waitForDeployment(name, namespace string, timeout time.Duration) error {
524524
}
525525

526526
cmd := exec.CommandContext(ctx, "kubectl", args...)
527-
if verbose {
527+
if Verbose {
528528
fmt.Printf("Running: kubectl %s\n", strings.Join(args, " "))
529529
}
530530
var stderr bytes.Buffer
@@ -544,7 +544,7 @@ func waitForDeployment(name, namespace string, timeout time.Duration) error {
544544
}
545545

546546
func runKubectl(args ...string) error {
547-
if verbose {
547+
if Verbose {
548548
fmt.Printf("Running: kubectl %s\n", strings.Join(args, " "))
549549
}
550550

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package commands
22

33
import (
44
"bufio"
@@ -8,9 +8,9 @@ import (
88
"regexp"
99
"strings"
1010

11-
"github.com/kagent-dev/kmcp/pkg/frameworks"
12-
"github.com/kagent-dev/kmcp/pkg/manifest"
13-
"github.com/kagent-dev/kmcp/pkg/templates"
11+
"github.com/kagent-dev/kmcp/pkg/cli/internal/frameworks"
12+
"github.com/kagent-dev/kmcp/pkg/cli/internal/manifest"
13+
"github.com/kagent-dev/kmcp/pkg/cli/internal/templates"
1414

1515
"github.com/spf13/cobra"
1616
)
@@ -41,7 +41,7 @@ var (
4141
)
4242

4343
func init() {
44-
rootCmd.AddCommand(initCmd)
44+
addRootSubCmd(initCmd)
4545

4646
initCmd.PersistentFlags().BoolVar(&initForce, "force", false, "Overwrite existing directory")
4747
initCmd.PersistentFlags().BoolVar(&initNoGit, "no-git", false, "Skip git initialization")
@@ -101,7 +101,7 @@ func runInitFramework(
101101
Secrets: projectManifest.Secrets,
102102
Directory: projectPath,
103103
NoGit: initNoGit,
104-
Verbose: verbose,
104+
Verbose: Verbose,
105105
}
106106

107107
// Customize project config for the specific framework

0 commit comments

Comments
 (0)