Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from 030/51-never-ignore-errors
Browse files Browse the repository at this point in the history
51 never ignore errors
  • Loading branch information
030 authored Feb 3, 2020
2 parents 3439330 + b6e0874 commit 3afdaa3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ script:
- go build -o $DELIVERABLE
- $SHA512_CMD $DELIVERABLE > ${DELIVERABLE}.sha512.txt
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then sonar-scanner -Dsonar.projectKey=030_go-yq -Dsonar.sources=. -Dsonar.host.url=https://sonarcloud.io -Dsonar.coverage.exclusions=**/*_test.go -Dsonar.go.coverage.reportPaths="coverage.out"; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then bash <(curl -s https://codecov.io/bash); fi
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ -n "${TRAVIS_TAG}" ]; then cp $DELIVERABLE go-yq && a2deb -app go-yq -version ${TRAVIS_TAG} -maintainer "030 <[email protected]>" -description "jq-style golang equivalent of yq"; fi
deploy:
provider: releases
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=030_go-yq&metric=sqale_index)](https://sonarcloud.io/dashboard?id=030_go-yq)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=030_go-yq&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=030_go-yq)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/2811/badge)](https://bestpractices.coreinfrastructure.org/projects/2811)
[![codecov](https://codecov.io/gh/030/go-yq/branch/master/graph/badge.svg)](https://codecov.io/gh/030/go-yq)
[![BCH compliance](https://bettercodehub.com/edge/badge/030/go-yq?branch=master)](https://bettercodehub.com/results/030/go-yq)
[![GolangCI](https://golangci.com/badges/github.com/golangci/golangci-web.svg)](https://golangci.com/r/github.com/030/go-yq)

jq-style golang equivalent of [yq](https://github.com/kislyuk/yq). [Another yq tool that is written in golang](https://github.com/mikefarah/yq) could be used if one requires more features.
jq-style golang equivalent of [yq](https://github.com/kislyuk/yq).

## rationale

Expand All @@ -37,7 +39,7 @@ go-yq was created to prevent that pip has to be installed in order to install yq
## installation

```
curl -L https://github.com/030/go-yq/releases/download/2.1.2/go-yq_2.1.2-0.deb -o go-yq.deb && \
curl -L https://github.com/030/go-yq/releases/download/x.y.z/go-yq_x.y.z-0.deb -o go-yq.deb && \
sudo apt -y install ./go-yq.deb
```

Expand All @@ -63,7 +65,7 @@ or

```
docker run -v /home/ben/dev/ansible-firefox:/ansible-firefox \
-it utrecht/go-yq:2.1.0 .firefox_version \
-it utrecht/go-yq:x.y.z .firefox_version \
/ansible-firefox/defaults/main.yml
```

Expand Down
30 changes: 20 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@ type input struct {
}

func (i input) config() {
i.verifyKey()

viper.SetConfigType("yaml")
viper.SetConfigName(i.filename())
viper.AddConfigPath(i.dir())
}

func (i input) value() string {
func (i input) value() (string, error) {
i.config()
keyWithoutFirstDot := strings.Replace(i.key, ".", "", 1)

err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %s", err))
return "", fmt.Errorf("fatal error config file: %v", err)
}
value := fmt.Sprintf("%v", viper.Get(keyWithoutFirstDot))

if value == "%!s(<nil>)" {
log.Fatal("File: ", i.file, " does not contain key: ", i.key)
if value == "<nil>" {
return "", fmt.Errorf("File: %v does not contain key: %v", i.file, i.key)
}

return value
return value, nil
}

func (i input) dir() string {
Expand All @@ -50,10 +48,11 @@ func (i input) filename() string {
return filepath.Base(filename)
}

func (i input) verifyKey() {
func (i input) verifyKey() error {
if !strings.HasPrefix(i.key, ".") {
log.Fatal("Key should start with a dot, i.e.: ."+i.key+", but was: ", i.key)
return fmt.Errorf("Key should start with a dot, i.e.: .%s, but was: %s", i.key, i.key)
}
return nil
}

func main() {
Expand All @@ -62,5 +61,16 @@ func main() {
}

i := input{key: os.Args[1], file: os.Args[2]}
fmt.Println(i.value())

err := i.verifyKey()
if err != nil {
log.Fatal(err)
}

v, err := i.value()
if err != nil {
log.Fatal(err)
}

fmt.Println(v)
}
57 changes: 54 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"testing"

yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -40,6 +41,22 @@ firefox_version2: "64.0"
firefox_version: 64.0.0
`

// See https://stackoverflow.com/a/34102842/2777965
func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}

func setup() {
createTestYaml()
}

func shutdown() {
removeTestYaml()
}

func createTestYaml() {
m := make(map[interface{}]interface{})

Expand Down Expand Up @@ -89,13 +106,11 @@ func TestYamlValue(t *testing.T) {
i := input{key: key, file: testYaml}

expected := value
actual := i.value()
actual, _ := i.value()
if expected != actual {
t.Errorf("Value was incorrect 'Check whether the key '%s' resides in the test yaml file', got value: %s, want: %s.", key, actual, expected)
}
}

removeTestYaml()
}

func TestDir(t *testing.T) {
Expand Down Expand Up @@ -130,3 +145,39 @@ func TestFile(t *testing.T) {
}
}
}

func TestVerifyKey(t *testing.T) {
unhappy := input{"abc", testYamlFilename}
err := unhappy.verifyKey()
want := "Key should start with a dot, i.e.: .abc, but was: abc"
if err.Error() != want {
t.Errorf("Error expected. Got '%v'. Want '%v'", err, want)
}

happy := input{".abc", testYamlFilename}
err = happy.verifyKey()
if err != nil {
t.Errorf("No error expected. Got '%v'", err)
}
}

func TestValue(t *testing.T) {
i := input{".abc", testYaml}

_, err := i.value()
want := "File: test2.yaml does not contain key: .abc"
if err.Error() != want {
t.Errorf("Error expected. Got '%v'. Want '%v'", err, want)
}
}

func TestReadInConfig(t *testing.T) {
i := input{".abc", "fileDoesNotExist"}

_, err := i.value()
want := "fatal error config file: Config File \"fileDoesNotExist\" Not Found in"
matched, _ := regexp.MatchString(want, err.Error())
if !matched {
t.Errorf("Error expected. Got '%v'. Want '%v'", err, want)
}
}

0 comments on commit 3afdaa3

Please sign in to comment.