diff --git a/main.go b/main.go new file mode 100644 index 0000000..fd47bb4 --- /dev/null +++ b/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "log" + "os" + "regexp" + "strings" + + "github.com/sirupsen/logrus" +) + +func scanFile(file string, key string) string { + f, err := os.Open(file) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + + var val string + + for scanner.Scan() { + logrus.Debug("Check whether file: ", file, " and line: ", scanner.Text(), " contains key: ", key) + if strings.Contains(scanner.Text(), key+":") { + val = value(scanner.Text()) + } + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } + + return val +} + +func value(keyValue string) string { + re := regexp.MustCompile("^[a-z-_]+: (.*)$") + match := re.FindStringSubmatch(keyValue) + + if len(match) == 0 { + log.Fatal("Cannot extract value for key, but was: '", keyValue, + "' please check whether the regex matches the key") + } + + logrus.Debug("MATCH: ", match) + + return match[1] +} + +func main() { + key := flag.String("key", "key", "Specify the key") + yamlFile := flag.String("yamlFile", "file.yaml", "Path to a yaml file") + debug := flag.Bool("debug", false, "Whether debugging should be enabled") + + flag.Parse() + + if *debug { + logrus.SetLevel(logrus.DebugLevel) + } + + logrus.Debug("key: ", *key) + logrus.Debug("yamlFile: ", *yamlFile) + + value := scanFile(*yamlFile, *key) + if value == "" { + log.Fatal("File: ", *yamlFile, " does not contain key: ", *key) + } + fmt.Println(value) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..cd3627b --- /dev/null +++ b/main_test.go @@ -0,0 +1,20 @@ +package main + +import "testing" + +func TestScanFile(t *testing.T) { + keyValue := map[string]string{ + "a": "abc", + "b": "def", + "c": "ghi", + "under_scores": "ensure that keys may contain an underscore", + } + + for key, value := range keyValue { + expected := value + actual := scanFile("test.yaml", key) + if expected != actual { + t.Errorf("Value was incorrect, got: %s, want: %s.", actual, expected) + } + } +} diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..0abba26 --- /dev/null +++ b/test.yaml @@ -0,0 +1,4 @@ +a: abc +b: def +c: ghi +under_scores: ensure that keys may contain an underscore \ No newline at end of file