Skip to content

Commit

Permalink
REFACTORING-V2 - add rawargs-v2. refactored version, more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hashmap-kz committed Jan 5, 2025
1 parent 552a79a commit 554b854
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 247 deletions.
165 changes: 74 additions & 91 deletions pkg/cmd/rawargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ type CmdArgsRawRecognized struct {
HasStdin bool
}

func allEmpty(where []string) bool {
if len(where) == 0 {
return true
}
for _, s := range where {
if strings.TrimSpace(s) != "" {
func allEmpty(values []string) bool {
for _, v := range values {
if strings.TrimSpace(v) != "" {
return false
}
}
Expand All @@ -41,138 +38,124 @@ func ParseArgs() (CmdArgsRawRecognized, error) {
arg := args[i]
switch {

// --filename=pod.yaml
case strings.HasPrefix(arg, "--filename="):
filenameGiven := strings.TrimPrefix(arg, "--filename=")
if filenameGiven == "" {
return result, fmt.Errorf("missing value for flag %s", arg)
}
err := handleStdin(filenameGiven, &result)
if err != nil {
// Handle --filename= or -f=
case strings.HasPrefix(arg, "--filename="), strings.HasPrefix(arg, "-f="):
if err := handleFilename(strings.SplitN(arg, "=", 2)[1], &result); err != nil {
return result, err
}
if filenameGiven != "-" {
result.Filenames = append(result.Filenames, filenameGiven)
}

// -f=pod.yaml
case strings.HasPrefix(arg, "-f="):
filenameGiven := strings.TrimPrefix(arg, "-f=")
if filenameGiven == "" {
// Handle --filename or -f with a separate value
case arg == "--filename" || arg == "-f":
if i+1 >= len(args) || args[i+1] == "" {
return result, fmt.Errorf("missing value for flag %s", arg)
}
err := handleStdin(filenameGiven, &result)
if err != nil {
if err := handleFilename(args[i+1], &result); err != nil {
return result, err
}
if filenameGiven != "-" {
result.Filenames = append(result.Filenames, filenameGiven)
}
// --filename pod.yaml -f pod.yaml
case arg == "--filename" || arg == "-f":
if i+1 < len(args) {
filenameGiven := args[i+1]
if filenameGiven == "" {
return result, fmt.Errorf("missing value for flag %s", arg)
}
err := handleStdin(filenameGiven, &result)
if err != nil {
return result, err
}
if filenameGiven != "-" {
result.Filenames = append(result.Filenames, filenameGiven)
}
i++ // Skip the next argument since it's the value
} else {
return result, fmt.Errorf("missing value for flag %s", arg)
}
i++ // Skip the next argument

// --envsubst-allowed-vars=HOME,USER
// Handle --envsubst-allowed-vars=
case strings.HasPrefix(arg, "--envsubst-allowed-vars="):
split := strings.Split(strings.TrimPrefix(arg, "--envsubst-allowed-vars="), ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for flag %s", arg)
list, err := appendList(strings.TrimPrefix(arg, "--envsubst-allowed-vars="))
if err != nil {
return result, err
}
result.EnvsubstAllowedVars = append(result.EnvsubstAllowedVars, split...)
result.EnvsubstAllowedVars = append(result.EnvsubstAllowedVars, list...)

// --envsubst-allowed-vars HOME,USER
// Handle --envsubst-allowed-vars with a separate value
case arg == "--envsubst-allowed-vars":
if i+1 < len(args) {
split := strings.Split(args[i+1], ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for flag %s", arg)
}
result.EnvsubstAllowedVars = append(result.EnvsubstAllowedVars, split...)
i++ // Skip the next argument since it's the value
} else {
if i+1 >= len(args) || args[i+1] == "" {
return result, fmt.Errorf("missing value for flag %s", arg)
}
list, err := appendList(args[i+1])
if err != nil {
return result, err
}
result.EnvsubstAllowedVars = append(result.EnvsubstAllowedVars, list...)
i++ // Skip the next argument

// --envsubst-allowed-prefixes=CI_,APP_
// Handle --envsubst-allowed-prefixes=
case strings.HasPrefix(arg, "--envsubst-allowed-prefixes="):
split := strings.Split(strings.TrimPrefix(arg, "--envsubst-allowed-prefixes="), ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for flag %s", arg)
list, err := appendList(strings.TrimPrefix(arg, "--envsubst-allowed-prefixes="))
if err != nil {
return result, err
}
result.EnvsubstAllowedPrefix = append(result.EnvsubstAllowedPrefix, split...)
result.EnvsubstAllowedPrefix = append(result.EnvsubstAllowedPrefix, list...)

// --envsubst-allowed-prefixes CI_,APP_
// Handle --envsubst-allowed-prefixes with a separate value
case arg == "--envsubst-allowed-prefixes":
if i+1 < len(args) {
split := strings.Split(args[i+1], ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for flag %s", arg)
}
result.EnvsubstAllowedPrefix = append(result.EnvsubstAllowedPrefix, split...)
i++ // Skip the next argument since it's the value
} else {
if i+1 >= len(args) || args[i+1] == "" {
return result, fmt.Errorf("missing value for flag %s", arg)
}
list, err := appendList(args[i+1])
if err != nil {
return result, err
}
result.EnvsubstAllowedPrefix = append(result.EnvsubstAllowedPrefix, list...)
i++ // Skip the next argument

// --recursive, -R
// Handle recursive and help flags
case arg == "--recursive" || arg == "-R":
result.Recursive = true

// --help, -h
case arg == "--help" || arg == "-h":
result.Help = true

// Handle unrecognized arguments
default:
result.Others = append(result.Others, arg)
}
}

// trying to get allowed-vars config from envs
// Load allowed vars and prefixes from environment variables
if len(result.EnvsubstAllowedVars) == 0 {
if value, exists := os.LookupEnv(envsubstAllowedVarsEnv); exists {
split := strings.Split(value, ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for env: %s", envsubstAllowedVarsEnv)
}
result.EnvsubstAllowedVars = split
if err := loadEnvVars(envsubstAllowedVarsEnv, &result.EnvsubstAllowedVars); err != nil {
return result, err
}
}

// trying to get allowed-prefixes from envs
if len(result.EnvsubstAllowedPrefix) == 0 {
if value, exists := os.LookupEnv(envsubstAllowedPrefixesEnv); exists {
split := strings.Split(value, ",")
if allEmpty(split) {
return result, fmt.Errorf("missing value for env: %s", envsubstAllowedPrefixesEnv)
}
result.EnvsubstAllowedPrefix = split
if err := loadEnvVars(envsubstAllowedPrefixesEnv, &result.EnvsubstAllowedPrefix); err != nil {
return result, err
}
}

return result, nil
}

func handleStdin(filenameGiven string, result *CmdArgsRawRecognized) error {
if filenameGiven == "-" {
func handleFilename(filename string, result *CmdArgsRawRecognized) error {
if filename == "" {
return fmt.Errorf("missing filename value")
}
if filename == "-" {
if result.HasStdin {
return fmt.Errorf("multiple redirection to stdin detected")
}
result.HasStdin = true
} else {
result.Filenames = append(result.Filenames, filename)
}
return nil
}

func appendList(value string) ([]string, error) {
split := strings.Split(value, ",")
if value == "" || allEmpty(split) {
return nil, fmt.Errorf("empty list value")
}
return split, nil
}

func loadEnvVars(envKey string, target *[]string) error {
value, exists := os.LookupEnv(envKey)
if !exists {
return nil
}

split := strings.Split(value, ",")
if allEmpty(split) {
return fmt.Errorf("missing value for env: %s", envKey)
}

*target = split
return nil
}
Loading

0 comments on commit 554b854

Please sign in to comment.