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
33 changes: 24 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@ import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
)

const (
ConfigDirName = ".codewise"
ConfigFileName = "config.yaml"
)

// DefaultConfig is the initial config content
type Config struct {
Version string `yaml:"version"`
User struct {
Name string `yaml:"name"`
} `yaml:"user"`
Defaults struct {
AppName string `yaml:"app_name"`
Image string `yaml:"image"`
RepoURL string `yaml:"repo_url"`
} `yaml:"defaults"`
}

var DefaultConfig = []byte(`version: v1
user:
name: aryan
defaults:
app_name: myapp
image: codewise:latest
repo_url: https://github.com/example/repo
`)

// InitConfig creates the config directory and file
func InitConfig() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
Expand All @@ -30,36 +43,38 @@ func InitConfig() (string, error) {
configDir := filepath.Join(home, ConfigDirName)
configPath := filepath.Join(configDir, ConfigFileName)

// Create directory if not exists
if err := os.MkdirAll(configDir, 0755); err != nil {
return "", err
}

// If config already exists, do nothing
if _, err := os.Stat(configPath); err == nil {
return configPath, fmt.Errorf("config already exists")
}

// Write default config
if err := os.WriteFile(configPath, DefaultConfig, 0644); err != nil {
return "", err
}

return configPath, nil
}

// ReadConfig reads and returns the config file contents
func ReadConfig() ([]byte, error) {
func ReadConfig() (*Config, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}

configPath := filepath.Join(home, ConfigDirName, ConfigFileName)

if _, err := os.Stat(configPath); err != nil {
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("config file not found")
}

return os.ReadFile(configPath)
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}

return &cfg, nil
}
31 changes: 23 additions & 8 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/aryansharma9917/codewise-cli/pkg/config"
)

const k8sDir = "k8s/app"
Expand All @@ -13,14 +15,27 @@ type Options struct {
Image string
}

func deploymentYAML(opts Options) []byte {
func applyDefaults(opts Options) Options {
cfg, err := config.ReadConfig()
if err == nil {
if opts.AppName == "" {
opts.AppName = cfg.Defaults.AppName
}
if opts.Image == "" {
opts.Image = cfg.Defaults.Image
}
}
if opts.AppName == "" {
opts.AppName = "codewise-app"
}
if opts.Image == "" {
opts.Image = "codewise:latest"
}
return opts
}

func deploymentYAML(opts Options) []byte {
opts = applyDefaults(opts)
return []byte(fmt.Sprintf(`apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -43,11 +58,10 @@ spec:
`, opts.AppName, opts.AppName, opts.AppName, opts.AppName, opts.Image))
}

func serviceYAML(appName string) []byte {
if appName == "" {
appName = "codewise-app"
func serviceYAML(app string) []byte {
if app == "" {
app = "codewise-app"
}

return []byte(fmt.Sprintf(`apiVersion: v1
kind: Service
metadata:
Expand All @@ -59,13 +73,14 @@ spec:
ports:
- port: 80
targetPort: 8080
`, appName, appName))
`, app, app))
}

// InitK8sManifests creates Kubernetes manifests with optional values
func InitK8sManifests(opts Options) error {
opts = applyDefaults(opts)

if _, err := os.Stat(k8sDir); err == nil {
return fmt.Errorf("k8s/app directory already exists")
return fmt.Errorf("%s directory already exists", k8sDir)
}

if err := os.MkdirAll(k8sDir, 0755); err != nil {
Expand Down
Loading