You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flux2/cmd/tk/utils.go

101 lines
1.8 KiB
Go

package main
import (
"bufio"
"bytes"
"context"
"io"
"os"
"os/exec"
"text/template"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type Utils struct {
}
type ExecMode string
const (
ModeOS ExecMode = "os.stderr|stdout"
ModeStderrOS ExecMode = "os.stderr"
ModeCapture ExecMode = "capture.stderr|stdout"
)
func (*Utils) execCommand(ctx context.Context, mode ExecMode, command string) (string, error) {
var stdoutBuf, stderrBuf bytes.Buffer
c := exec.CommandContext(ctx, "/bin/sh", "-c", command)
if mode == ModeStderrOS {
c.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
}
if mode == ModeOS {
c.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
c.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
}
if mode == ModeStderrOS || mode == ModeOS {
if err := c.Run(); err != nil {
return "", err
} else {
return "", nil
}
}
if mode == ModeCapture {
if output, err := c.CombinedOutput(); err != nil {
return "", err
} else {
return string(output), nil
}
}
return "", nil
}
func (*Utils) execTemplate(obj interface{}, tmpl, filename string) error {
t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return err
}
var data bytes.Buffer
writer := bufio.NewWriter(&data)
if err := t.Execute(writer, obj); err != nil {
return err
}
if err := writer.Flush(); err != nil {
return err
}
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, data.String())
if err != nil {
return err
}
return file.Sync()
}
func (*Utils) kubeClient(config string) (*kubernetes.Clientset, error) {
cfg, err := clientcmd.BuildConfigFromFlags("", config)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
return client, nil
}