Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/root.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"fmt"
9
"os"
10
"path/filepath"
11
12
homedir "github.com/mitchellh/go-homedir"
13
"github.com/spf13/cobra"
14
"k8s.io/client-go/rest"
15
16
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
17
"github.com/gitpod-io/gitpod/common-go/log"
18
"github.com/gitpod-io/gitpod/gpctl/pkg/prettyprint"
19
"github.com/gitpod-io/gitpod/gpctl/pkg/util"
20
)
21
22
// rootCmd represents the base command when called without any subcommands
23
var rootCmd = &cobra.Command{
24
Use: "gpctl",
25
Short: "Gpctl controls a Gitpod installation",
26
Args: cobra.MinimumNArgs(1),
27
}
28
29
// Execute adds all child commands to the root command and sets flags appropriately.
30
// This is called by main.main(). It only needs to happen once to the rootCmd.
31
func Execute() {
32
common_grpc.SetupLogging()
33
34
if err := rootCmd.Execute(); err != nil {
35
fmt.Println(err)
36
os.Exit(1)
37
}
38
}
39
40
func init() {
41
rootCmd.PersistentFlags().StringP("kubeconfig", "c", "$HOME/.kube/config", "Path to the kubeconfig file used for connecting to the cluster")
42
43
rootCmd.PersistentFlags().StringP("output-format", "o", "template", "Output format. One of: string|json|jsonpath|template")
44
rootCmd.PersistentFlags().String("output-template", "", "Output format Go template or jsonpath. Use with -o template or -o jsonpath")
45
}
46
47
func getKubeconfig() (*rest.Config, string, error) {
48
kubeconfig, err := rootCmd.PersistentFlags().GetString("kubeconfig")
49
if err != nil {
50
return nil, "", err
51
}
52
53
if kubeconfig == "$HOME/.kube/config" {
54
home, err := homedir.Dir()
55
if err != nil {
56
return nil, "", err
57
}
58
kubeconfig = filepath.Join(home, ".kube", "config")
59
}
60
61
return util.GetKubeconfig(kubeconfig)
62
}
63
64
func getOutputFormat(defaultTemplate string, defaultJsonpath string) *prettyprint.Printer {
65
format := prettyprint.TemplateFormat
66
template := defaultTemplate
67
jsonpath := defaultJsonpath
68
if rootCmd.PersistentFlags().Lookup("output-format").Changed {
69
fm, _ := rootCmd.PersistentFlags().GetString("output-format")
70
switch fm {
71
case "json":
72
format = prettyprint.JSONFormat
73
case "string":
74
format = prettyprint.StringFormat
75
case "template":
76
format = prettyprint.TemplateFormat
77
case "jsonpath":
78
format = prettyprint.JSONPathFormat
79
template = jsonpath
80
default:
81
log.WithField("format", fm).Warn("Unknown format, falling back to template")
82
}
83
}
84
if rootCmd.PersistentFlags().Lookup("output-template").Changed {
85
template, _ = rootCmd.PersistentFlags().GetString("output-template")
86
}
87
88
return &prettyprint.Printer{
89
Template: template,
90
Format: format,
91
Writer: os.Stdout,
92
}
93
}
94
95