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
28 changes: 28 additions & 0 deletions cmd/deploy_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"

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

var historyEnv string

var deployHistoryCmd = &cobra.Command{
Use: "history",
Short: "Show Helm release history for an environment",
RunE: func(cmd *cobra.Command, args []string) error {

if historyEnv == "" {
return fmt.Errorf("please provide --env")
}

return deploy.History(historyEnv)
},
}

func init() {
deployCmd.AddCommand(deployHistoryCmd)
deployHistoryCmd.Flags().StringVar(&historyEnv, "env", "", "Environment name")
}
30 changes: 30 additions & 0 deletions cmd/deploy_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"fmt"

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

var logsEnv string
var followLogs bool

var deployLogsCmd = &cobra.Command{
Use: "logs",
Short: "Stream logs for deployment pods",
RunE: func(cmd *cobra.Command, args []string) error {

if logsEnv == "" {
return fmt.Errorf("please provide --env")
}

return deploy.Logs(logsEnv, followLogs)
},
}

func init() {
deployCmd.AddCommand(deployLogsCmd)
deployLogsCmd.Flags().StringVar(&logsEnv, "env", "", "Environment name")
deployLogsCmd.Flags().BoolVar(&followLogs, "follow", false, "Stream logs")
}
45 changes: 45 additions & 0 deletions pkg/deploy/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package deploy

import (
"fmt"
"os/exec"
)

func History(envName string) error {

environment, err := LoadEnvironment(envName)
if err != nil {
return err
}

ns := environment.K8s.Namespace
ctx := environment.K8s.Context
release := environment.Helm.Release

fmt.Println("Release History")
fmt.Println("----------------")
fmt.Println("Environment:", envName)
fmt.Println("Namespace:", ns)
fmt.Println("Release:", release)
fmt.Println()

args := []string{
"history",
release,
"-n",
ns,
}

if ctx != "" {
args = append(args, "--kube-context", ctx)
}

cmd := exec.Command("helm", args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to fetch history")
}

fmt.Println(string(out))
return nil
}
70 changes: 70 additions & 0 deletions pkg/deploy/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package deploy

import (
"fmt"
"os/exec"
"strings"
)

func Logs(envName string, follow bool) error {

environment, err := LoadEnvironment(envName)
if err != nil {
return err
}

ns := environment.K8s.Namespace
ctx := environment.K8s.Context

// Get pods
args := []string{
"get",
"pods",
"-n",
ns,
"-o",
"name",
}

if ctx != "" {
args = append(args, "--context", ctx)
}

cmd := exec.Command("kubectl", args...)
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to fetch pods")
}

lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 || lines[0] == "" {
return fmt.Errorf("no pods found")
}

// Take first pod
pod := lines[0]

fmt.Println("Fetching logs for:", pod)
fmt.Println()

logArgs := []string{
"logs",
pod,
"-n",
ns,
}

if follow {
logArgs = append(logArgs, "-f")
}

if ctx != "" {
logArgs = append(logArgs, "--context", ctx)
}

logCmd := exec.Command("kubectl", logArgs...)
logCmd.Stdout = nil
logCmd.Stderr = nil

return logCmd.Run()
}