-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathutils.go
More file actions
146 lines (127 loc) · 4.19 KB
/
utils.go
File metadata and controls
146 lines (127 loc) · 4.19 KB
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
package utils
import (
"encoding/base64"
"fmt"
"net/url"
"strings"
"time"
"github.com/google/uuid"
"github.com/inhies/go-bytesize"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
)
// Ptr Returns the pointer to any type T
func Ptr[T any](v T) *T {
return &v
}
// PtrString creates a string representation of a passed object pointer or returns
// an empty string, if the passed object is _nil_.
func PtrString[T any](t *T) string {
if t != nil {
return fmt.Sprintf("%v", *t)
}
return ""
}
// PtrValue returns the dereferenced value if the pointer is not nil. Otherwise
// the types zero element is returned
func PtrValue[T any](t *T) (r T) {
if t != nil {
return *t
}
return r
}
// Int64Ptr returns a pointer to an int64
// Needed because the Ptr function only returns pointer to int
func Int64Ptr(i int64) *int64 {
return &i
}
// Float64Ptr returns a pointer to a float64
// Needed because the Ptr function only returns pointer to float
func Float64Ptr(f float64) *float64 {
return &f
}
// CmdHelp is used to explicitly set the Run function for non-leaf commands to the command help function, so that we can catch invalid commands
// This is a workaround needed due to the open issue on the Cobra repo: https://github.com/spf13/cobra/issues/706
func CmdHelp(cmd *cobra.Command, _ []string) {
cmd.Help() //nolint:errcheck //the function doesnt return anything to satisfy the required interface of the Run function
}
// ValidateUUID validates if the provided string is a valid UUID
func ValidateUUID(value string) error {
_, err := uuid.Parse(value)
if err != nil {
return fmt.Errorf("parse %s as UUID: %w", value, err)
}
return nil
}
// ConvertInt64PToFloat64P converts an int64 pointer to a float64 pointer
// This function will return nil if the input is nil
func ConvertInt64PToFloat64P(i *int64) *float64 {
if i == nil {
return nil
}
f := float64(*i)
return &f
}
func ValidateURLDomain(value string) error {
urlStruct, err := url.Parse(value)
if err != nil {
return fmt.Errorf("parse url: %w", err)
}
urlHost := urlStruct.Hostname()
if urlHost == "" {
return fmt.Errorf("bad url")
}
allowedUrlDomain := viper.GetString(config.AllowedUrlDomainKey)
if !strings.HasSuffix(urlHost, allowedUrlDomain) {
return fmt.Errorf(`only urls belonging to domain %s are allowed`, allowedUrlDomain)
}
return nil
}
// ConvertTimePToDateTimeString converts a time.Time pointer to a string represented as "2006-01-02 15:04:05"
// This function will return an empty string if the input is nil
func ConvertTimePToDateTimeString(t *time.Time) string {
if t == nil {
return ""
}
return t.Format(time.DateTime)
}
// PtrStringDefault return the value of a pointer [v] as string. If the pointer is nil, it returns the [defaultValue].
func PtrStringDefault[T any](v *T, defaultValue string) string {
if v == nil {
return defaultValue
}
return fmt.Sprintf("%v", *v)
}
// PtrByteSizeDefault return the value of an in64 pointer to a string representation of bytesize. If the pointer is nil,
// it returns the [defaultValue].
func PtrByteSizeDefault(size *int64, defaultValue string) string {
if size == nil {
return defaultValue
}
return bytesize.New(float64(*size)).String()
}
// Base64Encode encodes a []byte to a base64 representation as string
func Base64Encode(message []byte) string {
b := make([]byte, base64.StdEncoding.EncodedLen(len(message)))
base64.StdEncoding.Encode(b, message)
return string(b)
}
func UserAgentConfigOption(cliVersion string) sdkConfig.ConfigurationOption {
return sdkConfig.WithUserAgent(fmt.Sprintf("stackit-cli/%s", cliVersion))
}
// ConvertStringMapToInterfaceMap converts a map[string]string to a pointer to map[string]interface{}.
// Returns nil if the input map is empty.
//
//nolint:gocritic // Linter wants to have a non-pointer type for the map, but this would mean a nil check has to be done before every usage of this func.
func ConvertStringMapToInterfaceMap(m *map[string]string) *map[string]interface{} {
if m == nil || len(*m) == 0 {
return nil
}
result := make(map[string]interface{}, len(*m))
for k, v := range *m {
result[k] = v
}
return &result
}