Skip to content

Commit 1bdb0ed

Browse files
authored
Merge pull request #17 from eksrvb/feature/parsing-yaml-and-json
add funktions to parse json and yaml
2 parents fafc2e4 + 634ca41 commit 1bdb0ed

File tree

9 files changed

+212
-0
lines changed

9 files changed

+212
-0
lines changed

docs/commands/parseJSON.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
layout: default
3+
title: parseJSON
4+
parent: Commands
5+
nav_order: 3
6+
---
7+
8+
# Parsing a JSON File
9+
10+
```bash
11+
awesome-ci parseJSON [subcommand-option]
12+
```
13+
14+
| Subcommand option | Description |
15+
| ----------------- | ------------------------------ |
16+
| `-file` | your file location |
17+
| `-value` | your value you want to extract |
18+
19+
## Examlpe
20+
21+
Example demo.json:
22+
23+
```json
24+
{
25+
"value1": "hello",
26+
"value2": "world",
27+
"deepObject": {
28+
"value1": "hello",
29+
"value2": "world"
30+
}
31+
}
32+
```
33+
34+
Example command 1:
35+
36+
```shell
37+
awesome-ci parseJSON -file demo.json -value .value2
38+
world
39+
```
40+
41+
Example command 2:
42+
43+
```shell
44+
awesome-ci parseJSON -file demo.json -value .deepObject
45+
map[value1:hello value2:world]
46+
```
47+
48+
You can check more details of the json construct in one of the following versions.

docs/commands/parseYAML.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: default
3+
title: parseYAML
4+
parent: Commands
5+
nav_order: 4
6+
---
7+
8+
# Parsing a Yaml File
9+
10+
```bash
11+
awesome-ci parseYAML [subcommand-option]
12+
```
13+
14+
| Subcommand option | Description |
15+
| ----------------- | ------------------------------ |
16+
| `-file` | your file location |
17+
| `-value` | your value you want to extract |
18+
19+
## Examlpe
20+
21+
Example demo.yaml:
22+
23+
```yaml
24+
value1: hello
25+
value2: world
26+
deepObject:
27+
value1: hello
28+
value2: world
29+
```
30+
31+
Example command 1:
32+
33+
```shell
34+
awesome-ci parseYAML -file demo.yaml -value .value2
35+
world
36+
```
37+
38+
Example command 2:
39+
40+
```shell
41+
awesome-ci parseYAML -file demo.yaml -value .deepObject
42+
map[value1:hello value2:world]
43+
```
44+
45+
You can check more details of the yaml construct in one of the following versions.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ require (
99
github.com/x-cray/logrus-prefixed-formatter v0.5.2
1010
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
1111
golang.org/x/tools v0.1.0 // indirect
12+
gopkg.in/yaml.v2 v2.4.0
1213
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
4343
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4444
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4545
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
46+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
47+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
48+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"awesome-ci/service"
55
"flag"
66
"fmt"
7+
"log"
78
"os"
89
)
910

1011
var (
1112
cienv string
1213
createRelease CreateReleaseSet
1314
getBuildInfos getBuildInfosSet
15+
parseJson parseJsonYamlSet
16+
parseYaml parseJsonYamlSet
1417
version string
1518
versionFlag bool
1619
//debug bool
@@ -32,6 +35,12 @@ type getBuildInfosSet struct {
3235
format string
3336
}
3437

38+
type parseJsonYamlSet struct {
39+
fs *flag.FlagSet
40+
file string
41+
value string
42+
}
43+
3544
func init() {
3645
flag.StringVar(&cienv, "cienv", "Github", "set your CI Environment for Special Featueres!\nAvalible: Jenkins, Github, Gitlab, Custom\nDefault: Github")
3746
flag.BoolVar(&versionFlag, "version", false, "print version by calling it")
@@ -50,6 +59,16 @@ func init() {
5059
getBuildInfos.fs.StringVar(&getBuildInfos.version, "version", "", "override version to Update")
5160
getBuildInfos.fs.StringVar(&getBuildInfos.patchLevel, "patchLevel", "", "predefine version to Update")
5261
getBuildInfos.fs.StringVar(&getBuildInfos.format, "format", "pr,version", "define output by get")
62+
63+
// parseJSON
64+
parseJson.fs = flag.NewFlagSet("parseJSON", flag.ExitOnError)
65+
parseJson.fs.StringVar(&parseJson.file, "file", "", "file to be parsed")
66+
parseJson.fs.StringVar(&parseJson.value, "value", "", "value for output")
67+
68+
// parseYAML
69+
parseYaml.fs = flag.NewFlagSet("parseYAML", flag.ExitOnError)
70+
parseYaml.fs.StringVar(&parseYaml.file, "file", "", "file to be parsed")
71+
parseYaml.fs.StringVar(&parseYaml.value, "value", "", "value for output")
5372
}
5473

5574
func main() {
@@ -62,6 +81,8 @@ func main() {
6281
createRelease.fs.PrintDefaults()
6382
fmt.Print("\nSubcommand: getBuildInfos\n")
6483
getBuildInfos.fs.PrintDefaults()
84+
fmt.Print("\nSubcommand: parseJSON, parseYAML\n")
85+
parseJson.fs.PrintDefaults()
6586
fmt.Print("\nUsage:\n awesome-ci [subcommand] [options]\n")
6687
fmt.Print("\nUse awesome-ci createRelease -patchLevel bugfix -dry-run\n")
6788
fmt.Print("CI examples at: https://github.com/eksrvb/awesome-ci\n")
@@ -70,6 +91,7 @@ func main() {
7091

7192
if versionFlag {
7293
fmt.Println(version)
94+
os.Exit(0)
7395
}
7496

7597
switch os.Args[1] {
@@ -79,5 +101,13 @@ func main() {
79101
case "getBuildInfos":
80102
getBuildInfos.fs.Parse(os.Args[2:])
81103
service.GetBuildInfos(cienv, &getBuildInfos.version, &getBuildInfos.patchLevel, &getBuildInfos.format)
104+
case "parseJSON":
105+
parseJson.fs.Parse(os.Args[2:])
106+
service.ParseJson(&parseJson.file, &parseJson.value)
107+
case "parseYAML":
108+
parseYaml.fs.Parse(os.Args[2:])
109+
service.ParseYaml(&parseYaml.file, &parseYaml.value)
110+
default:
111+
log.Fatalln("Not a valid Subcommand")
82112
}
83113
}

service/parseJsonYaml.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package service
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"strings"
8+
9+
"gopkg.in/yaml.v2"
10+
)
11+
12+
func ParseJson(file *string, pvalue *string) {
13+
value := *pvalue
14+
15+
dat, err := ioutil.ReadFile(*file)
16+
check(err)
17+
18+
if strings.HasPrefix(*pvalue, "[]") {
19+
var result map[interface{}]interface{}
20+
21+
json.Unmarshal(dat, &result)
22+
23+
fmt.Print(result[value[2:]])
24+
} else if strings.HasPrefix(*pvalue, ".") {
25+
var result map[string]interface{}
26+
27+
json.Unmarshal(dat, &result)
28+
29+
fmt.Print(result[value[1:]])
30+
} else {
31+
var result map[string]interface{}
32+
33+
json.Unmarshal(dat, &result)
34+
35+
fmt.Print(result[value])
36+
37+
}
38+
}
39+
40+
func ParseYaml(file *string, pvalue *string) {
41+
value := *pvalue
42+
43+
dat, err := ioutil.ReadFile(*file)
44+
check(err)
45+
46+
if strings.HasPrefix(*pvalue, "[]") {
47+
var result map[interface{}]interface{}
48+
49+
yaml.Unmarshal(dat, &result)
50+
51+
fmt.Print(result[value[2:]])
52+
} else if strings.HasPrefix(*pvalue, ".") {
53+
var result map[string]interface{}
54+
55+
yaml.Unmarshal(dat, &result)
56+
57+
fmt.Print(result[value[1:]])
58+
} else {
59+
var result map[string]interface{}
60+
61+
yaml.Unmarshal(dat, &result)
62+
63+
fmt.Print(result[value])
64+
65+
}
66+
}

service/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type infosMergeMessage struct {
1212
PatchLevel string
1313
}
1414

15+
func check(e error) {
16+
if e != nil {
17+
panic(e)
18+
}
19+
}
20+
1521
func runcmd(cmd string, shell bool) string {
1622
if shell {
1723
out, err := exec.Command("bash", "-c", cmd).Output()

tests/testdata/values.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"value1": "hello",
3+
"value2": "world",
4+
"deepObject": {
5+
"value1": "hello",
6+
"value2": "world"
7+
}
8+
}

tests/testdata/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
value1: hello
2+
value2: world
3+
deepObject:
4+
value1: hello
5+
value2: world

0 commit comments

Comments
 (0)