This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquality.go
178 lines (139 loc) · 3.87 KB
/
quality.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package datacontract
import (
"fmt"
"io"
"log"
"os"
"os/exec"
)
func PrintQuality(dataContractLocation string, pathToQuality []string, target io.Writer) error {
dataContract, err := GetDataContract(dataContractLocation)
if err != nil {
return fmt.Errorf("failed parsing local data contract: %w", err)
}
qualitySpecification, err := getQualitySpecification(dataContract, pathToQuality)
if err != nil {
return fmt.Errorf("can't get specification: %w", err)
}
Log(target, string(qualitySpecification))
return nil
}
func InsertQuality(
dataContractLocation string,
qualitySpecification []byte,
qualityType string,
pathToQuality []string,
pathToType []string,
) error {
contract, err := GetDataContract(dataContractLocation)
if err != nil {
return err
}
SetValue(contract, pathToQuality, string(qualitySpecification))
SetValue(contract, pathToType, qualityType)
result, err := ToYaml(contract)
if err != nil {
return err
}
err = os.WriteFile(dataContractLocation, result, os.ModePerm)
if err != nil {
return err
}
return nil
}
type QualityCheckOptions struct {
SodaDataSource *string
SodaConfigurationFileName *string
}
func QualityCheck(
dataContractFileName string,
pathToType []string,
pathToSpecification []string,
options QualityCheckOptions,
target io.Writer,
) error {
contract, err := GetDataContract(dataContractFileName)
if err != nil {
return fmt.Errorf("quality checks failed: %w", err)
}
qualityType, err := getQualityType(contract, pathToType)
if err != nil {
return fmt.Errorf("quality type cannot be retrieved: %w", err)
}
if qualityType != "SodaCL" {
log.Printf("The '%s' quality type is not supported yet", qualityType)
return nil
}
qualitySpecification, err := getQualitySpecification(contract, pathToSpecification)
if err != nil {
return fmt.Errorf("quality check specification cannot be retrieved: %w",
err)
}
tempFile, err := os.CreateTemp("", "quality-checks-")
if err != nil {
return fmt.Errorf("can not create temporary file for quality checks: %w", err)
}
tempFile.Write(qualitySpecification)
err = sodaQualityCheck(tempFile.Name(), options, target)
printQualityChecksResult(err, target)
if err != nil {
return fmt.Errorf("quality checks failed: %w", err)
}
tempFile.Close()
return nil
}
func printQualityChecksResult(err error, target io.Writer) {
if err == nil {
Log(target, "🟢 quality checks on data contract passed!")
} else {
Log(target, "🔴 quality checks on data contract failed!")
}
}
func getQualityType(
contract DataContract,
path []string) (qualityType string, err error) {
qualityTypeUntyped, err := GetValue(contract, path)
if err != nil {
return "",
fmt.Errorf("can't get value of quality type: %w for path %v",
err, path)
}
qualityType, ok := qualityTypeUntyped.(string)
if !ok {
return "", fmt.Errorf("quality not of type string")
}
return qualityType, nil
}
func getQualitySpecification(
contract DataContract,
path []string) (specification []byte, err error) {
spec, err := GetValue(contract, path)
if err != nil {
return []byte{},
fmt.Errorf("can't get value of %w quality specification for path %v",
err, path)
}
return TakeStringOrMarshall(spec), nil
}
// soda core checks
func sodaQualityCheck(qualitySpecFileName string, options QualityCheckOptions, target io.Writer) error {
var args = []string{"scan"}
args = append(args, "-d")
if options.SodaDataSource != nil {
args = append(args, *options.SodaDataSource)
} else {
args = append(args, "default")
}
if options.SodaConfigurationFileName != nil {
args = append(args, "-c")
args = append(args, *options.SodaConfigurationFileName)
}
args = append(args, qualitySpecFileName)
cmd := exec.Command("soda", args...)
output, err := cmdCombinedOutput(cmd)
Log(target, string(output))
if err != nil {
return fmt.Errorf("the soda CLI failed: %w", err)
}
return nil
}