Skip to content

Commit

Permalink
config: dump full config on terminal if no args are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 8, 2019
1 parent 20f1ae5 commit c2e55b9
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bufio"
"fmt"
"github.com/spf13/viper"
"os"
Expand Down Expand Up @@ -47,13 +48,11 @@ func fullConfigPath(filePath string, fileName string, fileType string) string {
}

func createConfigFileIfNonExistent(ffp string) {
if _, err := os.Stat(ffp); os.IsExist(err) {
return
}

_, err := os.Create(ffp)
if err != nil {
panic(fmt.Sprintf("config file does not exists and wasn't able to create it: %s\n", err))
if _, err := os.Stat(ffp); os.IsNotExist(err) {
_, err := os.Create(ffp)
if err != nil {
panic(fmt.Sprintf("config file does not exists and wasn't able to create it: %s\n", err))
}
}
}

Expand Down Expand Up @@ -100,10 +99,19 @@ func (c *config) Set(key string, value string) {
}

func (c *config) PrintAll() {
err := viper.ReadInConfig()
file, err := os.Open(c.fileFullPath)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
panic(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}

err = scanner.Err()
if err != nil {
panic(err)
}
wholeConfig := viper.AllSettings()
fmt.Printf("%v\n", wholeConfig)
}

0 comments on commit c2e55b9

Please sign in to comment.