Skip to content

Commit f4b1a08

Browse files
authored
update go, go dependencies and fix viper breaking change (#33)
* update go dependencies * update golangci-version * add linter on branch for test * upgrade golang-ci config * golang-ci lint test * test golangci new version * update arg golang ci lint * update goreleaser go version * restore golangci-lint * fix linter * fix linter * fix linter * fix linter
1 parent 1fa07c3 commit f4b1a08

36 files changed

+379
-246
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ jobs:
1919
- uses: actions/checkout@v4
2020
- uses: actions/setup-go@v5
2121
with:
22-
go-version: '>=1.23'
22+
go-version: "1.24"
2323
- name: golangci-lint
24-
uses: golangci/golangci-lint-action@v6
24+
uses: golangci/golangci-lint-action@v7
2525
with:
26-
version: v1.62
27-
args: --disable-all --enable govet,staticcheck,unused
26+
version: v2.0
27+
args: >
28+
--enable govet
29+
--enable staticcheck
30+
--enable unused

.github/workflows/goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: set up go
2323
uses: actions/setup-go@v5
2424
with:
25-
go-version: "1.23"
25+
go-version: "1.24"
2626

2727
- name: cache go modules
2828
uses: actions/cache@v4

.github/workflows/unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: set up go
1717
uses: actions/setup-go@v5
1818
with:
19-
go-version: "1.23"
19+
go-version: "1.24"
2020

2121
- name: cache go modules
2222
uses: actions/cache@v4

bin/doc-generator/doc-generator.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4-
"github.com/cloudfoundry-community/gautocloud"
5-
_ "github.com/cloudfoundry-community/gautocloud/connectors/all"
6-
. "github.com/cloudfoundry-community/gautocloud/test-utils"
7-
"golang.org/x/text/cases"
8-
"golang.org/x/text/language"
94
"html/template"
105
"os"
116
"path"
127
"reflect"
138
"runtime"
149
"sort"
1510
"strings"
11+
12+
"github.com/cloudfoundry-community/gautocloud"
13+
_ "github.com/cloudfoundry-community/gautocloud/connectors/all"
14+
test_utils "github.com/cloudfoundry-community/gautocloud/test-utils"
15+
"golang.org/x/text/cases"
16+
"golang.org/x/text/language"
1617
)
1718

1819
type DocStruct struct {
@@ -56,68 +57,84 @@ type GlobalDoc struct {
5657
}
5758

5859
func before() {
59-
os.Setenv("MYSQL_URL", CreateEnvValue(ServiceUrl{
60+
err := os.Setenv("MYSQL_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
6061
Type: "mysql",
6162
User: "user",
6263
Password: "password",
6364
Port: 3406,
6465
Target: "mydb",
6566
}))
66-
os.Setenv("POSTGRES_URL", CreateEnvValue(ServiceUrl{
67+
fatalIf(err)
68+
err = os.Setenv("POSTGRES_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
6769
Type: "postgres",
6870
User: "user",
6971
Password: "password",
7072
Port: 5532,
7173
Target: "mydb",
7274
Options: "sslmode=disable",
7375
}))
74-
os.Setenv("MSSQL_URL", CreateEnvValue(ServiceUrl{
76+
fatalIf(err)
77+
err = os.Setenv("MSSQL_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
7578
Type: "sqlserver",
7679
User: "sa",
7780
Password: "password",
7881
Port: 1433,
7982
Target: "test",
8083
}))
81-
os.Setenv("SSO_TOKEN_URI", "http://localhost/tokenUri")
82-
os.Setenv("SSO_AUTH_URI", "http://localhost/authUri")
83-
os.Setenv("SSO_USER_INFO_URI", "http://localhost/userInfo")
84-
os.Setenv("SSO_CLIENT_ID", "myId")
85-
os.Setenv("SSO_CLIENT_SECRET", "mySecret")
86-
os.Setenv("SSO_GRANT_TYPE", "grant1,grant2")
87-
os.Setenv("SSO_SCOPES", "scope1,scope2")
88-
os.Setenv("MONGODB_URL", CreateEnvValue(ServiceUrl{
84+
fatalIf(err)
85+
err = os.Setenv("SSO_TOKEN_URI", "http://localhost/tokenUri")
86+
fatalIf(err)
87+
err = os.Setenv("SSO_AUTH_URI", "http://localhost/authUri")
88+
fatalIf(err)
89+
err = os.Setenv("SSO_USER_INFO_URI", "http://localhost/userInfo")
90+
fatalIf(err)
91+
err = os.Setenv("SSO_CLIENT_ID", "myId")
92+
fatalIf(err)
93+
err = os.Setenv("SSO_CLIENT_SECRET", "mySecret")
94+
fatalIf(err)
95+
err = os.Setenv("SSO_GRANT_TYPE", "grant1,grant2")
96+
fatalIf(err)
97+
err = os.Setenv("SSO_SCOPES", "scope1,scope2")
98+
fatalIf(err)
99+
err = os.Setenv("MONGODB_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
89100
Type: "mongo",
90101
Port: 27017,
91102
Target: "test",
92103
}))
93-
os.Setenv("ORACLE_URL", CreateEnvValue(ServiceUrl{
104+
fatalIf(err)
105+
err = os.Setenv("ORACLE_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
94106
Type: "oci",
95107
Port: 27017,
96108
Target: "test",
97109
}))
98-
os.Setenv("REDIS_URL", CreateEnvValue(ServiceUrl{
110+
fatalIf(err)
111+
err = os.Setenv("REDIS_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
99112
Type: "redis",
100113
User: "redis",
101114
Password: "redis",
102115
Port: 6379,
103116
}))
104-
os.Setenv("AMQP_URL", CreateEnvValue(ServiceUrl{
117+
fatalIf(err)
118+
err = os.Setenv("AMQP_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
105119
Type: "amqp",
106120
User: "user",
107121
Password: "password",
108122
Port: 5672,
109123
}))
110-
os.Setenv("SMTP_URL", CreateEnvValue(ServiceUrl{
124+
fatalIf(err)
125+
err = os.Setenv("SMTP_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
111126
Type: "smtp",
112127
Port: 587,
113128
}))
114-
os.Setenv("S3_URL", CreateEnvValue(ServiceUrl{
129+
fatalIf(err)
130+
err = os.Setenv("S3_URL", test_utils.CreateEnvValue(test_utils.ServiceUrl{
115131
Type: "s3",
116132
User: "accessKey1",
117133
Password: "verySecretKey1",
118134
Port: 8090,
119135
Target: "bucket",
120136
}))
137+
fatalIf(err)
121138
gautocloud.ReloadConnectors()
122139
}
123140
func main() {
@@ -164,7 +181,7 @@ func main() {
164181

165182
}
166183
func toSlug(name string) string {
167-
return strings.ToLower(strings.Replace(name, " ", "-", -1))
184+
return strings.ToLower(strings.ReplaceAll(name, " ", "-"))
168185
}
169186
func getDocMap() map[string]Doc {
170187
docs := make(map[string]Doc)
@@ -276,10 +293,7 @@ func isCloseable(data interface{}) bool {
276293
return false
277294
}
278295
resp = v.MethodByName("Close")
279-
if resp == (reflect.Value{}) {
280-
return false
281-
}
282-
return true
296+
return resp != (reflect.Value{})
283297
}
284298
func generateDocStruct(data interface{}) DocStruct {
285299
givenType := reflect.TypeOf(data)

cloudenv/env_var_cloudenv.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ func (c EnvVarCloudEnv) decodeJson(value string) map[string]interface{} {
107107
}
108108
decoder := json.NewDecoder(bytes.NewReader([]byte(value)))
109109
decoder.UseNumber()
110-
decoder.Decode(&creds)
110+
err := decoder.Decode(&creds)
111+
if err != nil {
112+
return nil
113+
}
111114
return creds
112115
}
113116
func (c EnvVarCloudEnv) EnvVars() []EnvVar {

cloudenv/heroku_cloudenv_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cloudenv_test
33
import (
44
. "github.com/cloudfoundry-community/gautocloud/cloudenv"
55

6+
"os"
7+
68
. "github.com/onsi/ginkgo"
79
. "github.com/onsi/gomega"
8-
"os"
910
)
1011

1112
var _ = Describe("HerokuCloudenv", func() {
@@ -116,13 +117,13 @@ var _ = Describe("HerokuCloudenv", func() {
116117
})
117118
Context("IsInCloudEnv", func() {
118119
It("should return false when have DYNO env var exists", func() {
119-
os.Unsetenv("DYNO")
120+
err := os.Unsetenv("DYNO")
121+
Expect(err).NotTo(HaveOccurred())
120122
Expect(cloudEnv.IsInCloudEnv()).Should(BeFalse())
121123
})
122124
It("should return true when have DYNO env var not exists", func() {
123125
err := os.Setenv("DYNO", "")
124126
Expect(err).NotTo(HaveOccurred())
125-
126127
Expect(cloudEnv.IsInCloudEnv()).Should(BeTrue())
127128
})
128129
})

cloudenv/k8s_cloudenv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c KubernetesCloudEnv) SanitizeEnvVars(envVars []string) []string {
3636
finalEnvVars = append(finalEnvVars, "KUBERNETES_URI="+strings.Join(splitEnvVar[1:], "="))
3737

3838
}
39-
splitEnvVar[0] = strings.Replace(splitEnvVar[0], "_SERVICE", "", -1)
39+
splitEnvVar[0] = strings.ReplaceAll(splitEnvVar[0], "_SERVICE", "")
4040
finalEnvVars = append(finalEnvVars, strings.Join(splitEnvVar, "="))
4141
}
4242
return finalEnvVars

cloudenv/k8s_cloudenv_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cloudenv_test
33
import (
44
. "github.com/cloudfoundry-community/gautocloud/cloudenv"
55

6+
"os"
7+
68
. "github.com/onsi/ginkgo"
79
. "github.com/onsi/gomega"
8-
"os"
910
)
1011

1112
var _ = Describe("KubernetesCloudenv", func() {
@@ -102,7 +103,9 @@ var _ = Describe("KubernetesCloudenv", func() {
102103
})
103104
Context("IsInCloudEnv", func() {
104105
It("should return false when have KUBERNETES_PORT env var exists", func() {
105-
os.Unsetenv("KUBERNETES_PORT")
106+
err := os.Unsetenv("KUBERNETES_PORT")
107+
Expect(err).NotTo(HaveOccurred())
108+
106109
Expect(cloudEnv.IsInCloudEnv()).Should(BeFalse())
107110
})
108111
It("should return true when have KUBERNETES_PORT env var not exists", func() {

cloudenv/local_cloudenv.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"os"
66

77
"encoding/json"
8-
"errors"
9-
"github.com/google/uuid"
10-
"github.com/spf13/viper"
118
"io"
129
"path/filepath"
1310
"reflect"
11+
12+
"github.com/go-viper/encoding/hcl"
13+
"github.com/google/uuid"
14+
"github.com/spf13/viper"
1415
)
1516

1617
const (
@@ -38,12 +39,18 @@ func NewLocalCloudEnv() CloudEnv {
3839
}
3940
func NewLocalCloudEnvFromReader(r io.Reader, configType string) CloudEnv {
4041
cloudEnv := &LocalCloudEnv{}
41-
viper.SetConfigType(configType)
42-
err := viper.ReadConfig(r)
42+
codecRegistry := viper.NewCodecRegistry()
43+
err := codecRegistry.RegisterCodec("hcl", hcl.Codec{})
44+
if err != nil {
45+
panic(fmt.Errorf("fatal error on registering codec: %s", err))
46+
}
47+
v := viper.NewWithOptions(viper.WithCodecRegistry(codecRegistry))
48+
v.SetConfigType(configType)
49+
err = v.ReadConfig(r)
4350
if err != nil {
44-
panic(fmt.Errorf("Fatal error on reading cloud file: %s \n", err))
51+
panic(fmt.Errorf("fatal error on reading cloud file: %s", err))
4552
}
46-
cloudEnv.loadServices(viper.Get(SERVICES_CONFIG_KEY))
53+
cloudEnv.loadServices(v.Get(SERVICES_CONFIG_KEY))
4754
cloudEnv.loadAppName()
4855
return cloudEnv
4956
}
@@ -73,16 +80,22 @@ func (c *LocalCloudEnv) loadConfigFile() error {
7380
})
7481
return nil
7582
}
76-
viper.SetConfigType(filepath.Ext(confPath)[1:])
77-
viper.SetConfigFile(confPath)
78-
err = viper.ReadInConfig()
83+
codecRegistry := viper.NewCodecRegistry()
84+
err = codecRegistry.RegisterCodec("hcl", hcl.Codec{})
7985
if err != nil {
80-
return errors.New(fmt.Sprintf("Fatal error on reading config file: %s \n", err.Error()))
86+
panic(fmt.Errorf("fatal error on registering codec: %s", err))
87+
}
88+
v := viper.NewWithOptions(viper.WithCodecRegistry(codecRegistry))
89+
v.SetConfigType(filepath.Ext(confPath)[1:])
90+
v.SetConfigFile(confPath)
91+
err = v.ReadInConfig()
92+
if err != nil {
93+
return fmt.Errorf("fatal error on reading config file: %s", err.Error())
8194
}
8295
var creds map[interface{}]interface{}
83-
err = viper.Unmarshal(&creds)
96+
err = v.Unmarshal(&creds)
8497
if err != nil {
85-
return errors.New(fmt.Sprintf("Fatal error when unmarshaling config file: %s \n", err.Error()))
98+
return fmt.Errorf("fatal error when unmarshaling config file: %s", err.Error())
8699
}
87100
finalCreds := c.convertMapInterface(creds).(map[string]interface{})
88101
c.servicesLocal = append(c.servicesLocal, ServiceLocal{
@@ -93,15 +106,21 @@ func (c *LocalCloudEnv) loadConfigFile() error {
93106
return nil
94107
}
95108
func (c *LocalCloudEnv) loadCloudFile() error {
96-
viper.SetConfigType(filepath.Ext(os.Getenv(LOCAL_ENV_KEY))[1:])
97-
viper.SetConfigFile(os.Getenv(LOCAL_ENV_KEY))
98-
err := viper.ReadInConfig()
109+
codecRegistry := viper.NewCodecRegistry()
110+
err := codecRegistry.RegisterCodec("hcl", hcl.Codec{})
111+
if err != nil {
112+
panic(fmt.Errorf("fatal error on registering codec: %s", err))
113+
}
114+
v := viper.NewWithOptions(viper.WithCodecRegistry(codecRegistry))
115+
v.SetConfigType(filepath.Ext(os.Getenv(LOCAL_ENV_KEY))[1:])
116+
v.SetConfigFile(os.Getenv(LOCAL_ENV_KEY))
117+
err = v.ReadInConfig()
99118
if err != nil {
100-
return errors.New(fmt.Sprintf("Fatal error on reading cloud file: %s \n", err.Error()))
119+
return fmt.Errorf("fatal error on reading cloud file: %s", err.Error())
101120
}
102-
services := viper.Get(SERVICES_CONFIG_KEY)
121+
services := v.Get(SERVICES_CONFIG_KEY)
103122
if services != nil {
104-
c.loadServices(viper.Get(SERVICES_CONFIG_KEY))
123+
c.loadServices(v.Get(SERVICES_CONFIG_KEY))
105124
} else {
106125
c.servicesLocal = make([]ServiceLocal, 0)
107126
}
@@ -110,7 +129,13 @@ func (c *LocalCloudEnv) loadCloudFile() error {
110129

111130
func (c *LocalCloudEnv) loadAppName() {
112131
c.appName = "<unknown>"
113-
appName := viper.Get("app_name")
132+
codecRegistry := viper.NewCodecRegistry()
133+
err := codecRegistry.RegisterCodec("hcl", hcl.Codec{})
134+
if err != nil {
135+
panic(fmt.Errorf("fatal error on registering codec: %s", err))
136+
}
137+
v := viper.NewWithOptions(viper.WithCodecRegistry(codecRegistry))
138+
appName := v.Get("app_name")
114139
if appName != nil {
115140
c.appName = appName.(string)
116141
}
@@ -207,12 +232,12 @@ func (c *LocalCloudEnv) loadServices(v interface{}) {
207232
}
208233
b, err := json.Marshal(dataFinal)
209234
if err != nil {
210-
panic(fmt.Errorf("Fatal error during loading cloud file: %s \n", err))
235+
panic(fmt.Errorf("fatal error during loading cloud file: %s", err))
211236
}
212237
var services []ServiceLocal
213238
err = json.Unmarshal(b, &services)
214239
if err != nil {
215-
panic(fmt.Errorf("Fatal error during loading cloud file: %s \n", err))
240+
panic(fmt.Errorf("fatal error during loading cloud file: %s", err))
216241
}
217242
c.servicesLocal = services
218243
}

0 commit comments

Comments
 (0)