Skip to content

Commit

Permalink
[Amir/Jenson] Displaying message to create config file based on forma…
Browse files Browse the repository at this point in the history
…t if the config file is missing, test for same
  • Loading branch information
jenson committed Nov 6, 2018
1 parent ce64e6f commit beca432
Show file tree
Hide file tree
Showing 10 changed files with 485 additions and 154 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ build-deps:
update-deps:
glide update

compile:
generate:
go-bindata -pkg config -o config/data.go data/config_template.yaml

compile: generate
mkdir -p out/
go build -race $(GLIDE_NOVENDOR)

ci.test: build-deps
ci.test: build-deps generate
ENVIRONMENT=test go test $(shell glide novendor | grep -v proctord) -v

test:
test: generate
ENVIRONMENT=test go test $(shell glide novendor | grep -v proctord)

build: build-deps compile fmt vet lint
Expand Down
21 changes: 15 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package config

import (
"fmt"
"github.com/fatih/color"
"github.com/gojektech/proctor/io"
"os"

"github.com/spf13/viper"
)

const (
Environment = "ENVIRONMENT"
Host = "PROCTOR_HOST"
Email = "EMAIL_ID"
Token = "ACCESS_TOKEN"
ProctorHost = "PROCTOR_HOST"
EmailId = "EMAIL_ID"
AccessToken = "ACCESS_TOKEN"
)

type ProctorConfig struct {
Expand All @@ -30,12 +32,19 @@ func LoadConfig() (ProctorConfig, error) {
err := viper.ReadInConfig()

if err != nil {
configFileUsed := viper.ConfigFileUsed()
if _, err := os.Stat(configFileUsed); os.IsNotExist(err) {
bytes, _ := dataConfig_templateYamlBytes()
template := string(bytes)
io.GetPrinter().Println(fmt.Sprintf("Config file not found in %s/proctor.yaml", ConfigFileDir()), color.FgRed)
io.GetPrinter().Println(fmt.Sprintf("Create a config file with template:\n\n%s\n\n", template), color.FgGreen)
}
return ProctorConfig{}, err
}

proctorHost := viper.GetString(Host)
emailId := viper.GetString(Email)
accessToken := viper.GetString(Token)
proctorHost := viper.GetString(ProctorHost)
emailId := viper.GetString(EmailId)
accessToken := viper.GetString(AccessToken)
return ProctorConfig{Host: proctorHost, Email: emailId, AccessToken: accessToken}, nil
}

Expand Down
32 changes: 26 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"github.com/fatih/color"
"github.com/gojektech/proctor/io"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -32,9 +34,9 @@ func TestLoadConfigsFromEnvironmentVariables(t *testing.T) {
proctorHost := "test.example.com"
email := "user@example.com"
accessToken := "test-token"
os.Setenv(Host, proctorHost)
os.Setenv(Email, email)
os.Setenv(Token, accessToken)
os.Setenv(ProctorHost, proctorHost)
os.Setenv(EmailId, email)
os.Setenv(AccessToken, accessToken)
configFilePath := createProctorConfigFile(t, "")
defer os.Remove(configFilePath)

Expand All @@ -46,9 +48,9 @@ func TestLoadConfigsFromEnvironmentVariables(t *testing.T) {

func TestLoadConfigFromFile(t *testing.T) {
setUp()
os.Unsetenv(Host)
os.Unsetenv(Email)
os.Unsetenv(Token)
os.Unsetenv(ProctorHost)
os.Unsetenv(EmailId)
os.Unsetenv(AccessToken)

configFilePath := createProctorConfigFile(t, "PROCTOR_HOST: file.example.com\nEMAIL_ID: file@example.com\nACCESS_TOKEN: file-token")
defer os.Remove(configFilePath)
Expand All @@ -59,6 +61,24 @@ func TestLoadConfigFromFile(t *testing.T) {
assert.Equal(t, ProctorConfig{Host: "file.example.com", Email: "file@example.com", AccessToken: "file-token"}, proctorConfig)
}

func TestShouldPrintInstructionsForConfigFileIfFileNotFound(t *testing.T) {
setUp()
configFilePath := fmt.Sprintf("%s/proctor.yaml", ConfigFileDir())
os.Remove(configFilePath)

mockPrinter := &io.MockPrinter{}
io.SetupMockPrinter(mockPrinter)
defer io.ResetPrinter()
mockPrinter.On("Println", fmt.Sprintf("Config file not found in %s", configFilePath), color.FgRed).Once()
helpMessage := "Create a config file with template:\n\nPROCTOR_HOST: <host>\nEMAIL_ID: <email>\nACCESS_TOKEN: <access-token>\n\n"
mockPrinter.On("Println", helpMessage, color.FgGreen).Once()

_, err := LoadConfig()

assert.Error(t, err)
mockPrinter.AssertExpectations(t)
}

func createProctorConfigFile(t *testing.T, content string) string {
proctorHost := []byte(fmt.Sprintf(content))
configFilePath := fmt.Sprintf("%s/proctor.yaml", ConfigFileDir())
Expand Down
237 changes: 237 additions & 0 deletions config/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions data/config_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROCTOR_HOST: <host>
EMAIL_ID: <email>
ACCESS_TOKEN: <access-token>
Loading

0 comments on commit beca432

Please sign in to comment.