Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the minio-loadgen as per golang best practices #107

Open
ksatchit opened this issue Dec 5, 2019 · 1 comment
Open

Refactor the minio-loadgen as per golang best practices #107

ksatchit opened this issue Dec 5, 2019 · 1 comment
Assignees

Comments

@ksatchit
Copy link
Member

ksatchit commented Dec 5, 2019

Is this a BUG REPORT or FEATURE REQUEST?

Choose one: BUG REPORT or FEATURE REQUEST

  • The initivars() function can be refactored according to golang best practices.
@ksatchit
Copy link
Member Author

ksatchit commented Dec 5, 2019

Example approach below. Thank you @mittachaitu @akhilerm @shubham14bajpai !!:

package main

import (
	"fmt"
	"os"
)

// function as a type 
type getEnvValidate func() (string, error)

// vars mapped to each ENV variable for future use
var mode, service, namespace string

// global err var
var err error

// function to validate presence and obtain ENV key
func getEnv(key string) (string, error) {
	value := os.Getenv(key)
	if len(value) == 0 {
		// just to print error before terminating
		fmt.Println("Missing ENV variable:", key)

		// exits at first missing ENV
		return "", fmt.Errorf("ENV %s not found", key)
		/*
		// dumb check to see if we iterate for all described ENVs if error not returned 
		return "", nil 
		*/
	}
	return value, nil
}

// functions sharing same signature as type getEnvValidate
func modeEnv() (string, error) {
  mode, err = getEnv("MODE")
  if err != nil {
    return "", err
  }
  return mode, nil
}

func serviceEnv() (string, error) {
  service, err = getEnv("SERVICE")
  if err != nil {
    return "", err
  }
  return service, nil
}

func namespaceEnv() (string, error) {
  namespace, err = getEnv("NAMESPACE")
  if err != nil {
    return "", err
  }
  return namespace, nil
}

/* add new functions for new ENVs here */

// function taking variadic input & iterating over each fn to execute them
func initVar(f ...getEnvValidate) error{
	for _, fs := range f{
	  _, err := fs()
	  if err != nil{
	    return err
	  }
	}
     return nil
}

func main() {

  // construct a slice of fns needed as part of validation
  // add new fns in the list here
  getAndValidate := []getEnvValidate{modeEnv,serviceEnv,namespaceEnv}

  // call function to execute the individual fns 
  initVar(getAndValidate...)

 /*
 // Print out in positive case. We have defined this as global vars

 fmt.Println(mode)
 fmt.Println(service)
 fmt.Println(namespace)
 */
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants