settings

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExportSettingsCommand = cli.Command{
	Name:        "export-suffix",
	Usage:       "suffix to be added when exporting credentials using granteds --export flag.",
	Subcommands: []*cli.Command{&SetExportSettingsCommand},
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}
		fmt.Println(cfg.ExportCredentialSuffix)
		return nil
	},
}
View Source
var PrintCommand = cli.Command{
	Name:  "print",
	Usage: "List Granted Settings",
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}
		data := [][]string{
			{"update-checker-api-url", c.String("update-checker-api-url")},
		}

		for k, v := range structs.Map(cfg) {
			data = append(data, []string{k, fmt.Sprint(v)})
		}

		table := tablewriter.NewWriter(os.Stderr)
		table.SetHeader([]string{"SETTING", "VALUE"})
		table.SetAutoWrapText(false)
		table.SetAutoFormatHeaders(true)
		table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
		table.SetAlignment(tablewriter.ALIGN_LEFT)
		table.SetCenterSeparator("")
		table.SetColumnSeparator("")
		table.SetRowSeparator("")
		table.SetRowLine(true)
		table.SetHeaderLine(false)
		table.SetBorder(false)
		table.SetTablePadding("\t")
		table.SetNoWhiteSpace(true)
		table.AppendBulk(data)
		table.Render()
		return nil
	},
}
View Source
var ProfileOrderingCommand = cli.Command{
	Name:        "profile-order",
	Usage:       "Update profile ordering when assuming",
	Subcommands: []*cli.Command{&SetProfileOrderingCommand},
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}
		fmt.Println(cfg.Ordering)
		return nil
	},
}
View Source
var SetConfigCommand = cli.Command{
	Name:  "set",
	Usage: "Set a value in settings",
	Flags: []cli.Flag{
		&cli.StringFlag{Name: "setting", Aliases: []string{"s"}, Usage: "The name of a configuration setting, currently only string, int and bool types are supported. e.g 'DisableUsageTips'. For other configuration, set the value using builtin commands or by directly modifying the config file for advanced use cases."},
		&cli.StringFlag{Name: "value", Aliases: []string{"v"}, Usage: "The value to set the configuration setting to"},
	},
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		configType := reflect.TypeOf(*cfg)
		configValue := reflect.ValueOf(cfg).Elem()
		type field struct {
			ftype  reflect.StructField
			fvalue reflect.Value
		}
		var fields []string
		var fieldMap = make(map[string]field)

		for i := 0; i < configType.NumField(); i++ {
			fieldType := configType.Field(i)
			kind := fieldType.Type.Kind()
			if kind == reflect.Bool || kind == reflect.String || kind == reflect.Int {
				fieldValue := configValue.Field(i)
				fields = append(fields, fieldType.Name)
				fieldMap[fieldType.Name] = field{
					fvalue: fieldValue,
					ftype:  fieldType,
				}
			}
		}

		var selectedFieldName = c.String("setting")
		if selectedFieldName == "" {
			p := &survey.Select{
				Message: "Select the configuration to change",
				Options: fields,
			}
			err = survey.AskOne(p, &selectedFieldName)
			if err != nil {
				return err
			}
		}

		var selectedField field
		var ok bool
		selectedField, ok = fieldMap[selectedFieldName]
		if !ok {
			return fmt.Errorf("the selected field %s is not a valid config parameter", selectedFieldName)
		}
		// Prompt the user to update the field
		var value interface{}
		var prompt survey.Prompt
		switch selectedField.ftype.Type.Kind() {
		case reflect.Bool:
			if !c.IsSet("value") {
				prompt = &survey.Confirm{
					Message: fmt.Sprintf("Enter new value for %s:", selectedFieldName),
					Default: selectedField.fvalue.Bool(),
				}
				err = survey.AskOne(prompt, &value)
				if err != nil {
					return err
				}
			} else {
				valueStr := c.String("value")
				value, err = strconv.ParseBool(valueStr)
				if err != nil {
					return err
				}
			}

		case reflect.String:
			if !c.IsSet("value") {
				var str string
				prompt = &survey.Input{
					Message: fmt.Sprintf("Enter new value for %s:", selectedFieldName),
					Default: fmt.Sprintf("%v", selectedField.fvalue.Interface()),
				}
				err = survey.AskOne(prompt, &str)
				if err != nil {
					return err
				}
				value = str
			} else {
				value = c.String("value")
			}
		case reflect.Int:
			if !c.IsSet("value") {
				prompt = &survey.Input{
					Message: fmt.Sprintf("Enter new value for %s:", selectedFieldName),
					Default: fmt.Sprintf("%v", selectedField.fvalue.Interface()),
				}
				err = survey.AskOne(prompt, &value)
				if err != nil {
					return err
				}
			} else {
				valueInt := c.String("value")
				value, err = strconv.Atoi(valueInt)
				if err != nil {
					return err
				}
			}
		}

		newValue := reflect.ValueOf(value)
		if newValue.Type().ConvertibleTo(selectedField.ftype.Type) {
			selectedField.fvalue.Set(newValue.Convert(selectedField.ftype.Type))
		} else {
			return fmt.Errorf("invalid type for %s", selectedField.ftype.Name)
		}

		clio.Infof("Updating the value of %s to %v", selectedFieldName, value)
		err = cfg.Save()
		if err != nil {
			return err
		}
		clio.Success("Config updated successfully")
		return nil
	},
}
View Source
var SetExportSettingsCommand = cli.Command{
	Name:  "set",
	Usage: "sets a suffix to be added when exporting credentials using granteds --export flag.",
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}
		withStdio := survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
		in := survey.Input{
			Message: "Exported credential suffix:",
		}
		var selection string
		clio.NewLine()
		err = testable.AskOne(&in, &selection, withStdio)
		if err != nil {
			return err
		}

		cfg.ExportCredentialSuffix = selection
		err = cfg.Save()
		if err != nil {
			return err
		}

		clio.Successf("Set export credential suffix to: %s", selection)
		return nil

	},
}
View Source
var SetProfileOrderingCommand = cli.Command{
	Name:  "set",
	Usage: "Sets the method of ordering IAM profiles in the assume method",
	Action: func(c *cli.Context) error {
		cfg, err := config.Load()
		if err != nil {
			return err
		}
		withStdio := survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
		in := survey.Select{
			Message: "Select filter type",
			Options: []string{"Frecency", "Alphabetical"},
		}
		var selection string
		clio.NewLine()
		err = testable.AskOne(&in, &selection, withStdio)
		if err != nil {
			return err
		}

		cfg.Ordering = selection
		err = cfg.Save()
		if err != nil {
			return err
		}

		clio.Success("Set profile ordering to: ", selection)
		return nil

	},
}
View Source
var SettingsCommand = cli.Command{
	Name:        "settings",
	Usage:       "Manage Granted settings",
	Subcommands: []*cli.Command{&PrintCommand, &ProfileOrderingCommand, &ExportSettingsCommand, &requesturl.Commands, &SetConfigCommand},
	Action:      PrintCommand.Action,
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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