|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "os"
|
6 | 5 |
|
7 |
| - homedir "github.com/mitchellh/go-homedir" |
8 |
| - "github.com/spf13/cobra" |
9 |
| - "github.com/spf13/viper" |
10 |
| - apiv1 "k8s.io/api/core/v1" |
11 |
| - //"k8s.io/cli-runtime/pkg/genericclioptions" |
| 6 | + "github.com/fntlnz/kubectl-trace/pkg/cmd" |
| 7 | + "github.com/spf13/pflag" |
| 8 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
12 | 9 | )
|
13 | 10 |
|
14 |
| -var cfgFile string |
| 11 | +func main() { |
| 12 | + flags := pflag.NewFlagSet("kubectl-trace", pflag.ExitOnError) |
| 13 | + pflag.CommandLine = flags |
15 | 14 |
|
16 |
| -var rootCmd = &cobra.Command{ |
17 |
| - Use: "trace", |
18 |
| - Short: "Execute and manage bpftrace programs on your kubernetes cluster", |
19 |
| -} |
20 |
| - |
21 |
| -func Execute() { |
22 |
| - if err := rootCmd.Execute(); err != nil { |
23 |
| - fmt.Println(err) |
24 |
| - os.Exit(1) |
| 15 | + streams := genericclioptions.IOStreams{ |
| 16 | + In: os.Stdin, |
| 17 | + Out: os.Stdout, |
| 18 | + ErrOut: os.Stderr, |
25 | 19 | }
|
26 |
| -} |
27 |
| - |
28 |
| -func init() { |
29 |
| - cobra.OnInitialize(initConfig) |
30 |
| - |
31 |
| - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kubectl-trace.yaml)") |
32 |
| - |
33 |
| - rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.") |
34 |
| - viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig")) |
35 |
| - viper.BindEnv("kubeconfig", "KUBECONFIG") |
36 |
| - |
37 |
| - rootCmd.PersistentFlags().StringP("namespace", "n", apiv1.NamespaceDefault, "If present, the namespace scope for this CLI request") |
38 |
| - viper.BindPFlag("namespace", rootCmd.PersistentFlags().Lookup("namespace")) |
39 |
| - |
40 |
| - rootCmd.AddCommand(runCmd) |
41 |
| - rootCmd.AddCommand(deleteCmd) |
42 |
| - rootCmd.AddCommand(getCmd) |
43 |
| -} |
44 |
| - |
45 |
| -// initConfig reads in config file and ENV variables if set. |
46 |
| -func initConfig() { |
47 |
| - if cfgFile != "" { |
48 |
| - // Use config file from the flag. |
49 |
| - viper.SetConfigFile(cfgFile) |
50 |
| - } else { |
51 |
| - // Find home directory. |
52 |
| - home, err := homedir.Dir() |
53 |
| - if err != nil { |
54 |
| - fmt.Println(err) |
55 |
| - os.Exit(1) |
56 |
| - } |
57 |
| - |
58 |
| - // Search config in home directory with name ".kubectl-trace" (without extension). |
59 |
| - viper.AddConfigPath(home) |
60 |
| - viper.SetConfigName(".kubectl-trace") |
61 |
| - } |
62 |
| - |
63 |
| - // If a config file is found, read it in. |
64 |
| - if err := viper.ReadInConfig(); err == nil { |
65 |
| - fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 20 | + root := cmd.NewTraceCommand(streams) |
| 21 | + if err := root.Execute(); err != nil { |
| 22 | + os.Exit(1) |
66 | 23 | }
|
67 | 24 | }
|
0 commit comments