-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
121 lines (102 loc) · 3.37 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package commands
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"github.com/adrg/xdg"
"github.com/kform-dev/pkg-server/cmd/pkgctl/commands/pkgcmd"
"github.com/kform-dev/pkg-server/cmd/pkgctl/commands/repocmd"
"github.com/kform-dev/pkg-server/cmd/pkgctl/commands/secretcmd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
)
const (
defaultConfigFileSubDir = "pkgctl"
defaultConfigFileName = "pkgctl"
defaultConfigFileNameExt = "yaml"
defaultConfigEnvPrefix = "PKGCTL"
//defaultDBPath = "package_db"
)
var (
configFile string
)
func GetMain(ctx context.Context) *cobra.Command {
//showVersion := false
cmd := &cobra.Command{
Use: "pkgctl",
Short: "pkgctl is a pkg management tool",
Long: "pkgctl is a pkg management tool",
SilenceUsage: true,
// We handle all errors in main after return from cobra so we can
// adjust the error message coming from libraries
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// initialize viper
// ensure the viper config directory exists
//cobra.CheckErr(fsys.EnsureDir(ctx, xdg.ConfigHome, defaultConfigFileSubDir))
// initialize viper settings
initConfig()
// create package store if it does not exist
//_, err := store.New(cmd.Context(),
// filepath.Join(xdg.ConfigHome, defaultConfigFileSubDir, defaultDBPath))
//return err
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
h, err := cmd.Flags().GetBool("help")
if err != nil {
return err
}
if h {
return cmd.Help()
}
return cmd.Usage()
},
}
pf := cmd.PersistentFlags()
kubeflags := genericclioptions.NewConfigFlags(true)
kubeflags.AddFlags(pf)
var k8s bool
cmd.Flags().BoolVarP(&k8s, "k8s", "k", false, "when set to true execute the command on the k8s cluster, otherwise it is execute locally")
kubeflags.WrapConfigFn = func(rc *rest.Config) *rest.Config {
rc.UserAgent = fmt.Sprintf("pkg/%s", version)
return rc
}
// ensure the viper config directory exists
cobra.CheckErr(os.MkdirAll(path.Join(xdg.ConfigHome, defaultConfigFileSubDir), 0700))
// initialize viper settings
initConfig()
cmd.AddCommand(pkgcmd.GetCommand(ctx, version, kubeflags, k8s))
cmd.AddCommand(secretcmd.GetCommand(ctx, version, kubeflags, k8s))
cmd.AddCommand(repocmd.GetCommand(ctx, version, kubeflags, k8s))
//cmd.PersistentFlags().StringVar(&configFile, "config", "c", fmt.Sprintf("Default config file (%s/%s/%s.%s)", xdg.ConfigHome, defaultConfigFileSubDir, defaultConfigFileName, defaultConfigFileNameExt))
return cmd
}
type Runner struct {
Command *cobra.Command
//Ctx context.Context
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if configFile != "" {
// Use config file from the flag.
viper.SetConfigFile(configFile)
} else {
viper.AddConfigPath(filepath.Join(xdg.ConfigHome, defaultConfigFileSubDir))
viper.SetConfigType(defaultConfigFileNameExt)
viper.SetConfigName(defaultConfigFileName)
_ = viper.SafeWriteConfig()
}
//viper.Set("kubecontext", kubecontext)
//viper.Set("kubeconfig", kubeconfig)
viper.SetEnvPrefix(defaultConfigEnvPrefix)
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
_ = 1
}
}