diff --git a/cmd/doctor.go b/cmd/doctor.go index bb48726..1649eb8 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -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) } diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go new file mode 100644 index 0000000..5eb9b72 --- /dev/null +++ b/pkg/docker/docker.go @@ -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) +}