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/main.go

84 lines
1.7 KiB
Go

package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
)
var VERSION = "0.0.1"
var rootCmd = &cobra.Command{
Use: "tk",
Short: "Kubernetes CD assembler",
Version: VERSION,
SilenceUsage: true,
SilenceErrors: true,
}
var (
kubeconfig string
namespace string
timeout time.Duration
)
func init() {
if home := homeDir(); home != "" {
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"),
"path to the kubeconfig file")
} else {
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "",
"absolute path to the kubeconfig file")
}
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "", "gitops-system",
"the namespace scope for this operation")
rootCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "", 5*time.Minute,
"timeout for this operation")
}
func main() {
log.SetFlags(0)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func kubernetesClient() (*kubernetes.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
}
func execCommand(command string) (string, error) {
c := exec.Command("/bin/sh", "-c", command)
output, err := c.CombinedOutput()
if err != nil {
return "", err
}
return string(output), nil
}