config

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package config /*

Index

Constants

This section is empty.

Variables

View Source
var ConfigCmd = &cobra.Command{
	Use:   "config",
	Short: "Show the current Pennsieve configuration file.",
	Long: `Show the current Pennsieve configuration file.

The Pennsieve configuration file contains information for the Pennsieve agent to authenticate with the Pennsieve Platform.

It is usually located in '~/.pennsieve/config.ini' and contains profiles, api-keys and secrets associated with a 
particular user. Do not share your config.ini file as API-Keys and secrets are private to individual users.
`,
	Run: func(cmd *cobra.Command, args []string) {

		home, err := os.UserHomeDir()
		pennsieveFolder := filepath.Join(home, ".pennsieve")
		configFile := filepath.Join(pennsieveFolder, "config.ini")
		data, err := ioutil.ReadFile(configFile)
		if err != nil {
			fmt.Println("Unable to render Pennsieve configuration file.")
			os.Exit(1)
		}
		fmt.Println(string(data))

	},
}
View Source
var InitCmd = &cobra.Command{
	Use:   "init <config file path>",
	Short: "Create a new Pennsieve Profile at a specified location.",
	Long: `Create a new Pennsieve configuration file and add profile with API Key/secret.

NOTE: This method ignores the globally defined "config" parameter. You set the file path for 
the new configuration file as the first argument for the method.

NOTE: When invoking the Pennsieve CLI, when you want to use a different config.ini file than the default file, you
need to specify the config.ini file location with every command by passing in the "config" parameter. `,
	Args: cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {

		configFile := args[0]
		forceCreate, _ := cmd.Flags().GetBool("force")
		profileName, _ := cmd.Flags().GetString("profile")
		apiToken, _ := cmd.Flags().GetString("api_token")
		apiSecret, _ := cmd.Flags().GetString("api_secret")

		if strings.HasPrefix(configFile, "~/") {
			dirname, _ := os.UserHomeDir()
			configFile = filepath.Join(dirname, configFile[2:])
		}

		if strings.ToLower(profileName) == "pennsieve" {
			fmt.Println("Cannot use 'pennsieve' as a profile name as this is a reserved variable.")
			return
		}

		_, err := os.Stat(configFile)
		if err == nil {
			if !forceCreate {
				fmt.Println("Existing configuration file found at:", configFile)
				fmt.Printf("\nWould you like to overwrite your existing configuration? (y/n): ")

				response := ""
				fmt.Scanln(&response)

				if response != "y" {
					fmt.Println("Cancelling action.")
					return
				}

				os.Remove(configFile)
			} else {
				os.Remove(configFile)
			}
		} else {
			fmt.Println(err)
		}

		viper.SetConfigFile(configFile)
		viper.AddConfigPath(configFile)

		configPath := filepath.Dir(configFile)

		os.MkdirAll(configPath, os.ModePerm)

		viper.SetConfigType("ini")
		viper.Set("agent.port", "9000")
		viper.Set("agent.upload_workers", "10")
		viper.Set("agent.upload_chunk_size", "32")
		viper.Set("global.default_profile", profileName)
		viper.Set(fmt.Sprintf("%s.api_token", profileName), apiToken)
		viper.Set(fmt.Sprintf("%s.api_secret", profileName), apiSecret)

		err = viper.WriteConfig()
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println("New configuration file created: ", viper.ConfigFileUsed())

	},
}
View Source
var WizardCmd = &cobra.Command{
	Use:   "wizard",
	Short: "Create a new config file using the configuration wizard.",
	Long: `Create a new config file using the configuration wizard.

Use the Pennsieve configuration wizard to create a new Pennsieve Configuration file and add an initial set of 
API credentials. 

NOTE: This method will remove any existing configuration file if it exists and previously defined API-Keys and secrets 
will not be recoverable. Use the 'pennsieve profile create' function to add profiles to an existing configuration file.

`,
	Run: func(cmd *cobra.Command, args []string) {

		home, err := os.UserHomeDir()
		pennsieveFolder := filepath.Join(home, ".pennsieve")
		configFile := filepath.Join(pennsieveFolder, "config.ini")

		_, err = os.Stat(configFile)
		if err == nil {
			fmt.Println("Existing configuration file found at:", configFile)
			fmt.Printf("\nWould you like to overwrite your existing configuration? (y/n): ")

			response := ""
			fmt.Scanln(&response)

			if response != "y" {
				return
			}

			os.Remove(configFile)
		}

		fmt.Println("\nCreating new configuration file at", configFile)

		if _, err := os.Stat(pennsieveFolder); errors.Is(err, os.ErrNotExist) {
			if err := os.Mkdir(pennsieveFolder, os.ModePerm); err != nil {
				log.Fatal(err)
			}
		}

		var profileName string
		fmt.Println("\nCreate new profile:")
		for {
			profileName = ""
			fmt.Printf("   Profile name [user]: ")
			scanner := bufio.NewScanner(os.Stdin)
			if scanner.Scan() {
				profileName = strings.TrimSpace(scanner.Text())
			}

			if strings.ToLower(profileName) == "pennsieve" {
				fmt.Println("Cannot use 'pennsieve' as a profile name as this is a reserved variable name.")
				continue
			}
			break
		}

		if len(profileName) == 0 {
			profileName = "user"
		}

		var apiToken string
		fmt.Printf("   API token: ")
		fmt.Scanln(&apiToken)

		var apiSecret string
		fmt.Printf("   API secret: ")
		fmt.Scanln(&apiSecret)

		fmt.Printf("Creating new profile: '%s'\n", profileName)

		fmt.Printf("Continue and write changes? (y/n) ")
		response := ""
		fmt.Scanln(&response)

		if response == "y" {
			viper.SetConfigType("ini")
			viper.AddConfigPath(filepath.Join(home, ".pennsieve"))
			viper.SetConfigFile(configFile)

			viper.SetDefault("agent.port", "9000")
			viper.SetDefault("agent.upload_workers", "10")
			viper.SetDefault("agent.upload_chunk_size", "32")

			viper.Set("agent.port", "9000")
			viper.Set("agent.upload_workers", "10")
			viper.Set("agent.upload_chunk_size", "32")
			viper.Set(fmt.Sprintf("%s.api_token", profileName), apiToken)
			viper.Set(fmt.Sprintf("%s.api_secret", profileName), apiSecret)
			viper.Set("global.default_profile", profileName)

			err = viper.SafeWriteConfig()
			if err != nil {
				fmt.Println(err)
			}

			fmt.Println("New profile created in config file: ", profileName)
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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