infctl-cli/app/k8s.go
jon brookes 924954d0ff changed app to use json config for pipeline steps
readme command line usage - to specify pipeline file name
readme updated to include reasoning behind the project

use native golang sqlite

RunScriptCommand named in functionMap
removed unused functions
removed unused functions
run script and pipeline example
renamed functions to drop the word script and add pipeline verb
2025-07-14 16:34:15 +01:00

85 lines
2.5 KiB
Go

package app
import (
"bytes"
"fmt"
"log/slog"
"os/exec"
"strings"
)
func k8sNamespaceExists(project string) error {
commandString := "kubectl get ns " + project
cmd := exec.Command("sh", "-c", commandString)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if strings.Contains(stderr.String(), "not found") {
err := k8sCreateNamespace(project)
if err != nil {
slog.Error(fmt.Sprintf("Failed to create namespace: %s", project))
return fmt.Errorf("failed to create namespace: %w", err)
}
return nil
} else {
slog.Error(fmt.Sprintf("❌ Command failed with error: %v\n", err))
slog.Debug(fmt.Sprintf("🐞 Stdout: %s\n", stdout.String()))
slog.Debug(fmt.Sprintf("🐞 Stderr: %s\n", stderr.String()))
return fmt.Errorf("failed to run kubectl command: %w", err)
}
}
output := stdout.String()
if !strings.Contains(output, project) {
return fmt.Errorf("namespace %s does not exist", project)
}
slog.Info(fmt.Sprintf("k8sNamespaceExists nothing to do - project: %s eists ...", project))
return nil
}
func k8sCreateNamespace(project string) error {
slog.Info(fmt.Sprintf("in k8sCreateNamespace with project: %s", project))
commandString := "kubectl create ns " + project
cmd := exec.Command("sh", "-c", commandString)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
slog.Error(fmt.Sprintf("❌ Command failed with error: %v\n", err))
slog.Debug(fmt.Sprintf("🐞 Stdout: %s\n", stdout.String()))
slog.Debug(fmt.Sprintf("🐞 Stderr: %s\n", stderr.String()))
return fmt.Errorf("failed to run kubectl command: %w", err)
}
output := stdout.String()
if !strings.Contains(output, project) {
return fmt.Errorf("failed to create namespace %s", project)
}
return nil
}
func RunCommand(command string) error {
slog.Debug(fmt.Sprintf("🐞 Running script command: %s", command))
cmd := exec.Command("sh", "-c", command)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
slog.Error(fmt.Sprintf("❌ Command failed with error: %v\n", err))
slog.Debug(fmt.Sprintf("🐞 Stdout: %s\n", stdout.String()))
slog.Debug(fmt.Sprintf("🐞 Stderr: %s\n", stderr.String()))
return fmt.Errorf("failed to run script command: %w", err)
}
output := stdout.String()
slog.Debug(fmt.Sprintf("RunCommand command executed successfully: %s", command))
slog.Debug(fmt.Sprintf("RunCommand command output: %s", output))
return nil
}