-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.go
106 lines (86 loc) · 2.17 KB
/
common.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
package opslevel
import (
"fmt"
"strings"
"github.com/relvacode/iso8601"
"github.com/shurcooL/graphql"
)
type IdentifierInput struct {
Id graphql.ID `graphql:"id,omitempty" json:"id,omitempty"`
Alias graphql.String `graphql:"alias,omitempty" json:"alias,omitempty"`
}
type PageInfo struct {
HasNextPage graphql.Boolean `graphql:"hasNextPage"`
HasPreviousPage graphql.Boolean `graphql:"hasPreviousPage"`
Start graphql.String `graphql:"startCursor"`
End graphql.String `graphql:"endCursor"`
}
type PayloadVariables map[string]interface{}
type DeleteInput struct {
Id graphql.ID `json:"id"`
}
type OpsLevelErrors struct {
Message string
Path []string
}
type IdResponsePayload struct {
Id graphql.ID `graphql:"deletedCheckId"`
Errors []OpsLevelErrors
}
func (p *IdResponsePayload) Mutate(client *Client, m interface{}, v PayloadVariables) error {
if err := client.Mutate(m, v); err != nil {
return err
}
return FormatErrors(p.Errors)
}
func NewId(id string) *IdentifierInput {
return &IdentifierInput{
Id: graphql.ID(id),
}
}
func NewIdFromAlias(alias string) *IdentifierInput {
return &IdentifierInput{
Alias: graphql.String(alias),
}
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool {
p := new(bool)
*p = v
return p
}
func FormatErrors(errs []OpsLevelErrors) error {
if len(errs) == 0 {
return nil
}
var errstrings []string
errstrings = append(errstrings, "OpsLevel API Errors:")
for _, err := range errs {
errstrings = append(errstrings, fmt.Sprintf("\t* %s", string(err.Message)))
}
return fmt.Errorf(strings.Join(errstrings, "\n"))
}
func NewID(id string) *graphql.ID {
output := graphql.ID(id)
return &output
}
func NewInt(i int) *int {
output := i
return &output
}
func NewISO8601Date(datetime string) iso8601.Time {
date, _ := iso8601.ParseString(datetime)
return iso8601.Time{Time: date}
}
func removeDuplicates(data []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range data {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}