cmd

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigCmd = &cobra.Command{
	Use:   "config",
	Short: "Manage configuration of the CLI utility",
	Long:  "Manage configuration of the CLI utility",
}
View Source
var ConfigInitCmd = &cobra.Command{
	Use:   "init",
	Short: "import config",
	Long:  "import config",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		filename := args[0]
		log.GetLogger().Infof("Start [%s %s]", cmd.CommandPath(), filename)
		c := &config.Config{}

		file, err := ioutil.ReadFile(filename)
		if err != nil {
			log.GetLogger().Debugf("Failed to open file %s, [ERROR: %s]", filename, err)
			exit.SetExitWithError(err, fmt.Sprintf("Failed to open file %s", filename))
			return
		}

		err = json.Unmarshal([]byte(file), c)
		if err != nil {
			log.GetLogger().Debugf("Failed to parse file %s, [ERROR: %s]", filename, err)
			exit.SetExitWithError(err, fmt.Sprintf("Failed to parse file as json file %s", filename))
			return
		}

		c.Log(fmt.Sprintf("Init with file %s: ", filename))

		if len(c.Profile) == 0 {
			c.Profile = "prod"
		}

		err = c.MiniCheck()
		if err != nil {
			log.GetLogger().Debugf("Config file has missing fields %s, [ERROR: %s]", filename, err)
			exit.SetExitWithError(err, fmt.Sprintf("Config file has missing fields %s", filename))
			return
		}

		log.GetLogger().Infof("Minicheck ok")

		configFile := context.GetContext().ConfigFilename()
		err = c.Save(configFile)
		if err != nil {
			log.GetLogger().Debugf("Error in saving config file, [ERROR: %s]", err)
			exit.SetExitWithError(err, fmt.Sprintf("Error saving confing file"))
			return
		}

		log.GetLogger().Infof("Save config to file %s", configFile)
		log.GetLogger().Infof("End [%s %s]", cmd.CommandPath(), filename)
	},
}
View Source
var ConfigSetCmd = &cobra.Command{
	Use:   "set",
	Short: "Set pctl config",
	Long:  "Set various parameters of pctl config",
}
View Source
var ConfigSetProjectCmd = &cobra.Command{
	Use:   "project <project name>",
	Short: "set the project to be used",
	Long:  "set the project under which all applicable pctl command will be executed",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) <= 0 {
			err := fmt.Errorf("invalid command")
			exit.SetExitWithError(err, fmt.Sprintf("Failed to get the command args"))
			return
		}
		project := args[0]
		log.GetLogger().Debugf("Start [%s %s]", cmd.CommandPath(), project)
		_, err := testProjectAccess(project)
		if err != nil {
			exit.SetExitWithError(err, fmt.Sprintf("Failed to set project %s", project))
			return
		}
		conf := config.GetConfig()
		conf.Project = project
		configFile := context.GetContext().ConfigFilename()
		err = conf.Save(configFile)
		if err != nil {
			log.GetLogger().Debugf("Error in saving config file, [ERROR: %s]", err)
			exit.SetExitWithError(err, fmt.Sprintf("Error saving confing file"))
			return
		}

		log.GetLogger().Infof("Save config to file %s", configFile)
		log.GetLogger().Debugf("End [%s]", cmd.CommandPath())
	},
}
View Source
var ConfigShowCmd = &cobra.Command{
	Use:   "show",
	Short: "display current config",
	Long:  "display current config",
	Args:  cobra.ExactArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		log.GetLogger().Infof("Start [%s]", cmd.CommandPath())
		showSource, err := cmd.Flags().GetBool("source")
		if err != nil {
			showSource = false
		}
		if showSource {
			output.PrintOutputer(cmd, config.GetConfigTracker())
		} else {
			output.PrintOutputer(cmd, config.GetConfig())
		}
		log.GetLogger().Infof("End [%s]", cmd.CommandPath())
	},
}
View Source
var KubeconfigCmd = &cobra.Command{
	Use:     "kubeconfig",
	Short:   "Generate kubeconfig",
	Long:    "Allows the user to generate a kubeconfig",
	Aliases: []string{"kc"},
}

KubeconfigCmd is command definition for kubeconfig root command

View Source
var KubeconfigDownloadCmd = &cobra.Command{
	Use:   "download",
	Short: "Download the generated kubeconfig",
	Long:  "Download the generated kubeconfig",
	Args:  cobra.ExactArgs(0),
	Run: func(cmd *cobra.Command, args []string) {
		log.GetLogger().Infof("Start [%s]", cmd.CommandPath())

		auth := config.GetConfig().GetAppAuthProfile()

		defaultNamespace, _ := cmd.Flags().GetString("namespace")
		cluster, _ := cmd.Flags().GetString("cluster")
		params := url.Values{}
		if defaultNamespace != "" {
			params.Add("namespace", defaultNamespace)
		}
		if cluster != "" {
			params.Add("opts.selector", fmt.Sprintf("paralus.dev/clusterName=%s", cluster))
		}
		params.Add("opts.ID", config.GetConfig().APIKey)
		params.Add("opts.organization", config.GetConfig().Organization)

		uri := fmt.Sprintf("/v2/sentry/kubeconfig/user?%s", params.Encode())
		resp, err := auth.AuthAndRequestFullResponse(uri, "GET", nil)
		if err != nil {
			exit.SetExitWithError(err, "failed to get kubeconfig")
			return
		}

		jsonData := &struct {
			Data string `json:"data"`
		}{}

		err = resp.JSON(jsonData)
		if err != nil {
			exit.SetExitWithError(err, "failed to get kubeconfig")
			return
		}

		decoded, err := base64.StdEncoding.DecodeString(jsonData.Data)
		if err != nil {
			exit.SetExitWithError(err, "failed to get kubeconfig")
			return
		}
		yaml := string(decoded)

		toFile, _ := cmd.Flags().GetString("to-file")
		if len(toFile) != 0 {
			err := ioutil.WriteFile(toFile, []byte(yaml), 0644)
			if err != nil {
				exit.SetExitWithError(err, "failed to store the downloaded kubeconfig file ")
				return
			}
			fmt.Println(fmt.Sprintf("kubeconfig downloaded to file - %s", toFile))
		} else {
			fmt.Println(yaml)
		}

		log.GetLogger().Infof("End [%s]", cmd.CommandPath())
	},
}

KubeconfigDownloadCmd is command definition for kubeconfig download cmd

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd. if any of the subcommands run into an error, it shows up here

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL