Skip to content

Commit

Permalink
refactor: just tidying up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
eljamo committed Dec 13, 2023
1 parent d33a638 commit 4c439f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Usage:
mempass [flags]
Flags:
--case_transform string case transformation, allowed values: ALTERNATE, ALTERNATE_LETTERCASE, CAPITALISE, CAPITALISE_INVERT, INVERT, LOWER, LOWER_VOWEL_UPPER_CONSONANT, NONE, RANDOM, UPPER (default "RANDOM")
--case_transform string case transformation, allowed values: ALTERNATE, ALTERNATE_LETTERCASE, CAPITALISE, CAPITALISE_INVERT, INVERT, LOWER, LOWER_VOWEL_UPPER_CONSONANT, NONE, RANDOM, SENTENCE, UPPER (default "RANDOM")
--custom_config_path string custom config file path, you can use this to load a custom config. Such as ones generated by xkpasswd.net
-h, --help help for mempass
--num_passwords int number of passwords to generate, valid values: 1+ (default 3)
Expand All @@ -31,7 +31,7 @@ Flags:
--padding_characters_before int number of characters to pad before the password, valid values: 0+ (default 2)
--padding_digits_after int number of digits to pad before the password, valid values: 0+ (default 2)
--padding_digits_before int number of digits to pad before the password, valid values: 0+ (default 2)
--padding_type string padding type, allowed values: ADAPTIVE, FIXED, NONE
--padding_type string padding type, allowed values: ADAPTIVE, FIXED, NONE (default "FIXED")
--preset string use a built-in preset. Valid values: DEFAULT, APPLEID, NTLM, SECURITYQ, WEB16, WEB16_XKPASSWD, WEB32, WIFI, XKCD, XKCD_XKPASSWD (default "DEFAULT")
--separator_alphabet strings comma-separated list of characters to separate password parts, example values: !, @, $, %, ^, &, *, -, +, =, :, |, ~, ?, /, ., ;
--separator_character string character to separate password parts, example values: RANDOM, !, @, $, %, ^, &, *, -, +, =, :, |, ~, ?, /, ., ; (default "RANDOM")
Expand Down
60 changes: 29 additions & 31 deletions cmd/cli/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"github.com/spf13/pflag"
)

const CustomConfigKeyPath = "custom_config_path"
const CustomConfigPathKey string = "custom_config_path"

func generateConfig(cmd *cobra.Command) (*config.Settings, error) {
baseCfg, customCfg, err := loadJSONFiles(cmd)
basePreset, customCfg, err := loadJSONFiles(cmd)
if err != nil {
return nil, fmt.Errorf("loadJSONFiles error: %w", err)
return nil, err
}

flagCfg, err := getCmdFlagsAsJSON(cmd)
flagCfg, err := getCmdFlags(cmd)
if err != nil {
return nil, fmt.Errorf("getCmdFlags error: %w", err)
return nil, err
}

return config.Generate(baseCfg, customCfg, flagCfg)
return config.Generate(basePreset, customCfg, flagCfg)
}

// loadJSONFiles loads the base config and the custom config from the JSON files
// Loads the base preset and the custom config from the JSON files
func loadJSONFiles(cmd *cobra.Command) (map[string]any, map[string]any, error) {
customCfg, err := getCustomConfigJSON(cmd)
if err != nil {
Expand All @@ -38,29 +38,31 @@ func loadJSONFiles(cmd *cobra.Command) (map[string]any, map[string]any, error) {
return nil, nil, err
}

baseCfg, err := asset.GetJSONPreset(presetValue)
basePreset, err := asset.GetJSONPreset(presetValue)
if err != nil {
return nil, nil, err
}

return baseCfg, customCfg, nil
return basePreset, customCfg, nil
}

// Loads the custom config JSON file
func getCustomConfigJSON(cmd *cobra.Command) (map[string]any, error) {
path, err := getCustomConfigPath(cmd)
path, err := cmd.Flags().GetString(CustomConfigPathKey)
if err != nil {
return nil, err
}

customCfgJSON, err := loadCustomConfig(path)
customCfgJSON, err := loadCustomConfigJSON(path)
if err != nil {
return nil, err
}

return customCfgJSON, nil
}

func getCmdFlagsAsJSON(cmd *cobra.Command) (map[string]any, error) {
// Returns a map of the cmd flags and their values
func getCmdFlags(cmd *cobra.Command) (map[string]any, error) {
flags := make(map[string]any)

var err error
Expand All @@ -69,42 +71,35 @@ func getCmdFlagsAsJSON(cmd *cobra.Command) (map[string]any, error) {
return
}

var flagErr error
switch flag.Value.Type() {
case "string":
flags[flag.Name], flagErr = cmd.Flags().GetString(flag.Name)
flags[flag.Name], err = cmd.Flags().GetString(flag.Name)
case "int":
flags[flag.Name], flagErr = cmd.Flags().GetInt(flag.Name)
flags[flag.Name], err = cmd.Flags().GetInt(flag.Name)
case "bool":
flags[flag.Name], flagErr = cmd.Flags().GetBool(flag.Name)
flags[flag.Name], err = cmd.Flags().GetBool(flag.Name)
case "stringSlice":
flags[flag.Name], flagErr = cmd.Flags().GetStringSlice(flag.Name)
}

if flagErr != nil {
err = flagErr
flags[flag.Name], err = cmd.Flags().GetStringSlice(flag.Name)
}
})

if err != nil {
return nil, fmt.Errorf("getCmdFlagsAsJSON error: %w", err)
return nil, fmt.Errorf("error parsing cmd flags (%w)", err)
}

return flags, nil
}

func getCustomConfigPath(cmd *cobra.Command) (string, error) {
return cmd.Flags().GetString(CustomConfigKeyPath)
}

func loadCustomConfig(path string) (map[string]any, error) {
// Loads the custom config JSON file
func loadCustomConfigJSON(path string) (map[string]any, error) {
if path == "" {
return nil, nil
}

return asset.LoadJSONFile(path)
}

// Returns the preset value from the custom config if it exists
func getPresetFromCustomConfig(customCfgJSON map[string]any) string {
if customCfgJSON == nil {
return ""
Expand All @@ -117,6 +112,7 @@ func getPresetFromCustomConfig(customCfgJSON map[string]any) string {
return ""
}

// Returns the preset value
func getPresetValue(cmd *cobra.Command, customJSONCfg map[string]any) (string, error) {
var presetValue string
presetFlag, presetArgPresent, err := checkPresetFlag(cmd)
Expand All @@ -135,16 +131,18 @@ func getPresetValue(cmd *cobra.Command, customJSONCfg map[string]any) (string, e
return presetValue, nil
}

// Returns the preset flag value and if preset flag was explicitly set
func checkPresetFlag(cmd *cobra.Command) (string, bool, error) {
presetArgPresent := isFlagSet(cmd, option.PresetKey)
presetFlag, err := cmd.Flags().GetString(option.PresetKey)
if presetArgPresent && (err != nil || presetFlag == "") {
return "", false, fmt.Errorf("invalid %s flag: %w", option.PresetKey, err)
presetFlagValue, err := cmd.Flags().GetString(option.PresetKey)
if presetArgPresent && (err != nil || presetFlagValue == "") {
return "", false, fmt.Errorf("invalid %s flag (%w)", option.PresetKey, err)
}

return presetFlag, presetArgPresent, nil
return presetFlagValue, presetArgPresent, nil
}

// Checks if a flag has been explicitly set
func isFlagSet(cmd *cobra.Command, flagKey string) bool {
var flagSet bool
cmd.Flags().Visit(func(flag *pflag.Flag) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)

var version = "1.7.0"
var version = "1.8.0"

var rootCmd = &cobra.Command{
Use: "mempass",
Expand Down Expand Up @@ -59,7 +59,7 @@ func init() {

// Preset and Custom Config Flags
rootCmd.Flags().String(
"custom_config_path", "",
CustomConfigPathKey, "",
"custom config file path, you can use this to load a custom config. Such as ones generated by xkpasswd.net",
)
rootCmd.Flags().String(
Expand Down Expand Up @@ -97,7 +97,7 @@ func init() {
"maximum word length, valid values: 1+",
)

// Seperator Flags
// Separator Flags
rootCmd.Flags().StringSlice(
"separator_alphabet",
[]string{},
Expand Down

0 comments on commit 4c439f2

Please sign in to comment.