2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-11-01 19:03:23 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-02-07 19:13:19 +00:00
|
|
|
"strings"
|
2022-11-01 19:03:23 +00:00
|
|
|
|
|
|
|
"github.com/itchyny/gojq"
|
|
|
|
"github.com/teris-io/shortid"
|
2022-04-25 14:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RandName(name string) string {
|
2022-11-01 19:03:23 +00:00
|
|
|
shortID, err := shortid.New(1, shortid.DefaultABC, 6666)
|
2023-01-11 21:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
id, err := shortID.Generate()
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-02-07 19:13:19 +00:00
|
|
|
id = strings.ToLower(id)
|
2022-11-01 19:03:23 +00:00
|
|
|
return name + "-" + id
|
|
|
|
}
|
|
|
|
|
|
|
|
// JQFilter uses the provided "jq" filter to parse json.
|
|
|
|
// Matching results are returned as a slice of strings.
|
|
|
|
func JQFilter(config, filter string) ([]string, error) {
|
|
|
|
result := []string{}
|
|
|
|
query, err := gojq.Parse(filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var m interface{}
|
|
|
|
err = json.Unmarshal([]byte(config), &m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := query.Run(m)
|
|
|
|
for {
|
|
|
|
v, ok := iter.Next()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err, ok := v.(error); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf("%v", v)
|
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IntToPointer(i int) *int {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
|
|
|
func BoolToPointer(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
func StringToPointer(s string) *string {
|
|
|
|
return &s
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|