Skip to content

Commit a1eb08c

Browse files
committed
Setup cli for running dumper
1 parent 5ededee commit a1eb08c

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎cli/cmd/kube.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
8+
v1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/client-go/kubernetes"
11+
"k8s.io/client-go/tools/clientcmd"
12+
)
13+
14+
func homeDir() string {
15+
if h := os.Getenv("HOME"); h != "" {
16+
return h
17+
}
18+
return os.Getenv("USERPROFILE") // windows
19+
}
20+
21+
func getPodInfo(podName string) (*v1.Pod, error) {
22+
config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homeDir(), ".kube", "config"))
23+
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
clientset, err := kubernetes.NewForConfig(config)
29+
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
35+
// TODO: get current namespace
36+
return clientset.CoreV1().Pods("").Get(context.Background(), podName, metav1.GetOptions{})
37+
38+
}

‎cli/cmd/root.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var podName string
12+
var output string
13+
14+
// rootCmd represents the base command when called without any subcommands
15+
var rootCmd = &cobra.Command{
16+
Use: "dotnet-k8s-dumper",
17+
Short: "A brief description of your application",
18+
Long: `A longer description that spans multiple lines and likely contains
19+
examples and usage of using your application. For example:
20+
21+
Cobra is a CLI library for Go that empowers applications.
22+
This application is a tool to generate the needed files
23+
to quickly create a Cobra application.`,
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
podInfo, err := getPodInfo(podName)
26+
27+
if err != nil {
28+
return err
29+
}
30+
31+
log.Println(podInfo)
32+
33+
return nil
34+
},
35+
// Uncomment the following line if your bare application
36+
// has an action associated with it:
37+
}
38+
39+
// Execute adds all child commands to the root command and sets flags appropriately.
40+
// This is called by main.main(). It only needs to happen once to the rootCmd.
41+
func Execute() {
42+
if err := rootCmd.Execute(); err != nil {
43+
fmt.Println(err)
44+
os.Exit(1)
45+
}
46+
}
47+
48+
func init() {
49+
rootCmd.PersistentFlags().StringVar(&podName, "pod-name", "", "Pod name for creating dump")
50+
rootCmd.PersistentFlags().StringVar(&output, "output", "", "Dump output file")
51+
}

‎cli/main.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/dodopizza/dotnet-k8s-dumper/cli/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}

0 commit comments

Comments
 (0)