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
51 changes: 16 additions & 35 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,30 @@ package cmd

import (
"fmt"
"os"
"os/exec"
"runtime"

"github.com/aryansharma9917/codewise-cli/pkg/docker"
"github.com/spf13/cobra"
)

var doctorCmd = &cobra.Command{
Use: "doctor",
Short: "Check Codewise CLI environment",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Codewise CLI Doctor")
fmt.Println("-------------------")

// Go info
fmt.Println("Go version:", runtime.Version())
fmt.Println("OS/Arch:", runtime.GOOS, runtime.GOARCH)

// Codewise version
fmt.Println("Codewise version:", rootCmd.Version)

// Working directory
if wd, err := os.Getwd(); err == nil {
fmt.Println("Working directory:", wd)
}

// Git version
if out, err := exec.Command("git", "--version").Output(); err == nil {
fmt.Println("Git:", string(out))
} else {
fmt.Println("Git: not found")
}
var dockerCmd = &cobra.Command{
Use: "docker",
Short: "Docker related helpers",
}

// Config path (future use)
configPath := os.ExpandEnv("$HOME/.codewise/config.yaml")
if _, err := os.Stat(configPath); err == nil {
fmt.Println("Config file:", configPath)
} else {
fmt.Println("Config file:", configPath, "(not found)")
var dockerInitCmd = &cobra.Command{
Use: "init",
Short: "Generate a Dockerfile",
Run: func(cmd *cobra.Command, args []string) {
err := docker.InitDockerfile()
if err != nil {
fmt.Println("ℹ️", err.Error())
return
}
fmt.Println("✅ Dockerfile created")
},
}

func init() {
rootCmd.AddCommand(doctorCmd)
dockerCmd.AddCommand(dockerInitCmd)
rootCmd.AddCommand(dockerCmd)
}
35 changes: 35 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package docker

import (
"fmt"
"os"
)

const dockerfileName = "Dockerfile"

var defaultDockerfile = []byte(`
FROM golang:1.21-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app

FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/app .
EXPOSE 8080
CMD ["./app"]
`)

// InitDockerfile creates a Dockerfile if it doesn't exist
func InitDockerfile() error {
if _, err := os.Stat(dockerfileName); err == nil {
return fmt.Errorf("Dockerfile already exists")
}

return os.WriteFile(dockerfileName, defaultDockerfile, 0644)
}