Skip to content

Commit 2faeb5b

Browse files
ci: ensure "go fmt" formatting, use "any" instead of "interface{}" (#150)
Adds a check in CI that `go fmt ./...` doesn't change anything, and that `any` is used instead of `interface{}`. (Lifted entirely [from Pebble](https://github.com/canonical/pebble/blob/7e8e6bf44d386e1fd6eb478e09c2722a8bd1b22a/.github/workflows/tests.yml#L46-L62)). To satisfy the new check, also contains a `go fmt ./...` run, which cleans up some whitespace in client_test.go. --------- Co-authored-by: Ben Hoyt <ben.hoyt@canonical.com>
1 parent 33dbf57 commit 2faeb5b

File tree

10 files changed

+64
-43
lines changed

10 files changed

+64
-43
lines changed

.github/workflows/push.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
jobs:
18+
format:
19+
name: Format check
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v6
24+
25+
- name: Set up Go
26+
uses: actions/setup-go@v6
27+
with:
28+
go-version-file: 'go.mod'
29+
30+
- name: Ensure no formatting changes
31+
run: |
32+
go fmt ./...
33+
git diff --exit-code
34+
35+
- name: Ensure no use of empty interface (should be any)
36+
run: |
37+
! egrep -R --exclude-dir .git 'interface\{\}'
38+
1839
binaries:
1940
name: Build concierge
2041
runs-on: ubuntu-latest

internal/config/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package config
22

33
// MergeMaps takes two maps and returns a combined map, where KV pairs in the second map arg
44
// take precedence over the first.
5-
func MergeMaps[K string, V interface{}](m1 map[K]V, m2 map[K]V) map[K]V {
5+
func MergeMaps[K string, V any](m1 map[K]V, m2 map[K]V) map[K]V {
66
combinedMap := map[K]V{}
77
for k := range m1 {
88
combinedMap[k] = m1[k]

internal/juju/juju.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (j *JujuHandler) install() error {
126126
// writeCredentials iterates over any provided cloud credentials and authors Juju's
127127
// credentials.yaml
128128
func (j *JujuHandler) writeCredentials() error {
129-
credentials := map[string]interface{}{"credentials": map[string]interface{}{}}
129+
credentials := map[string]any{"credentials": map[string]any{}}
130130
addedCredentials := false
131131

132132
// Iterate over the providers
@@ -137,8 +137,8 @@ func (j *JujuHandler) writeCredentials() error {
137137
}
138138

139139
// Set the credentials for the provider, under the credential name "concierge".
140-
credentials["credentials"] = map[string]interface{}{
141-
p.CloudName(): map[string]interface{}{
140+
credentials["credentials"] = map[string]any{
141+
p.CloudName(): map[string]any{
142142
"concierge": p.Credentials(),
143143
},
144144
}

internal/providers/google.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewGoogle(system system.Worker, config *config.Config) *Google {
2020
system: system,
2121
bootstrap: config.Providers.Google.Bootstrap,
2222
credentialsFile: credentialsFile,
23-
credentials: map[string]interface{}{},
23+
credentials: map[string]any{},
2424
modelDefaults: config.Providers.Google.ModelDefaults,
2525
bootstrapConstraints: config.Providers.Google.BootstrapConstraints,
2626
}
@@ -31,7 +31,7 @@ type Google struct {
3131
bootstrap bool
3232
system system.Worker
3333
credentialsFile string
34-
credentials map[string]interface{}
34+
credentials map[string]any
3535
modelDefaults map[string]string
3636
bootstrapConstraints map[string]string
3737
}
@@ -45,7 +45,7 @@ func (l *Google) Prepare() error {
4545
return fmt.Errorf("failed to read credentials file: %w", err)
4646
}
4747

48-
credentials := make(map[string]interface{})
48+
credentials := make(map[string]any)
4949

5050
err = yaml.Unmarshal(contents, &credentials)
5151
if err != nil {
@@ -71,7 +71,7 @@ func (l *Google) CloudName() string { return "google" }
7171
func (l *Google) GroupName() string { return "" }
7272

7373
// Credentials reports the section of Juju's credentials.yaml for the provider.
74-
func (l *Google) Credentials() map[string]interface{} { return l.credentials }
74+
func (l *Google) Credentials() map[string]any { return l.credentials }
7575

7676
// ModelDefaults reports the Juju model-defaults specific to the provider.
7777
func (l *Google) ModelDefaults() map[string]string { return l.modelDefaults }

internal/providers/google_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ func TestNewGoogle(t *testing.T) {
3030
config: noOverrides,
3131
expected: &Google{
3232
system: system,
33-
credentials: map[string]interface{}{},
33+
credentials: map[string]any{},
3434
},
3535
},
3636
{
3737
config: credsInConfig,
3838
expected: &Google{
3939
system: system,
4040
credentialsFile: "/home/ubuntu/credentials.yaml",
41-
credentials: map[string]interface{}{},
41+
credentials: map[string]any{},
4242
},
4343
},
4444
{
4545
config: overrides,
4646
expected: &Google{
4747
system: system,
4848
credentialsFile: "/home/ubuntu/alternate-credentials.yaml",
49-
credentials: map[string]interface{}{},
49+
credentials: map[string]any{},
5050
},
5151
},
5252
}
@@ -92,7 +92,7 @@ private-key: |
9292
project-id: concierge
9393
`)
9494

95-
fakeCredsMarshalled := make(map[string]interface{})
95+
fakeCredsMarshalled := make(map[string]any)
9696
err := yaml.Unmarshal(creds, &fakeCredsMarshalled)
9797
if err != nil {
9898
t.Fatal(err)

internal/providers/k8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (k *K8s) CloudName() string { return "k8s" }
102102
func (k *K8s) GroupName() string { return "" }
103103

104104
// Credentials reports the section of Juju's credentials.yaml for the provider
105-
func (m K8s) Credentials() map[string]interface{} { return nil }
105+
func (m K8s) Credentials() map[string]any { return nil }
106106

107107
// ModelDefaults reports the Juju model-defaults specific to the provider.
108108
func (m *K8s) ModelDefaults() map[string]string { return m.modelDefaults }

internal/providers/lxd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (l *LXD) CloudName() string { return "localhost" }
8181
func (l *LXD) GroupName() string { return "lxd" }
8282

8383
// Credentials reports the section of Juju's credentials.yaml for the provider
84-
func (l *LXD) Credentials() map[string]interface{} { return nil }
84+
func (l *LXD) Credentials() map[string]any { return nil }
8585

8686
// ModelDefaults reports the Juju model-defaults specific to the provider.
8787
func (l *LXD) ModelDefaults() map[string]string { return l.modelDefaults }

internal/providers/microk8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (m *MicroK8s) GroupName() string {
108108
}
109109

110110
// Credentials reports the section of Juju's credentials.yaml for the provider
111-
func (m MicroK8s) Credentials() map[string]interface{} { return nil }
111+
func (m MicroK8s) Credentials() map[string]any { return nil }
112112

113113
// ModelDefaults reports the Juju model-defaults specific to the provider.
114114
func (m *MicroK8s) ModelDefaults() map[string]string { return m.modelDefaults }

internal/providers/providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Provider interface {
3030
// to allow non-root users to interact with the provider (where applicable).
3131
GroupName() string
3232
// Credentials reports the section of Juju's credentials.yaml for the provider.
33-
Credentials() map[string]interface{}
33+
Credentials() map[string]any
3434
// ModelDefaults reports the Juju model-defaults specific to the provider.
3535
ModelDefaults() map[string]string
3636
// BootstrapConstraints reports the Juju bootstrap-constraints specific to the provider.

internal/snapd/client_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ import (
1212
// createTestServer creates a test HTTP server with a Unix socket listener.
1313
func createTestServer(t *testing.T, handler http.Handler) (*httptest.Server, string) {
1414
t.Helper()
15-
15+
1616
// Create temporary directory for socket
1717
tmpDir := t.TempDir()
1818
socketPath := filepath.Join(tmpDir, "snapd.socket")
19-
19+
2020
// Create Unix listener
2121
listener, err := net.Listen("unix", socketPath)
2222
if err != nil {
2323
t.Fatalf("Failed to create Unix listener: %v", err)
2424
}
25-
25+
2626
// Create test server with custom listener
2727
server := httptest.NewUnstartedServer(handler)
2828
server.Listener = listener
2929
server.Start()
30-
30+
3131
return server, socketPath
3232
}
3333

@@ -36,7 +36,7 @@ func TestSnap_Success(t *testing.T) {
3636
if r.URL.Path != "/v2/snaps/test-snap" {
3737
t.Errorf("Expected path '/v2/snaps/test-snap', got: %s", r.URL.Path)
3838
}
39-
39+
4040
resp := response{
4141
Type: "sync",
4242
Status: "OK",
@@ -55,18 +55,18 @@ func TestSnap_Success(t *testing.T) {
5555
t.Fatalf("failed to marshal snap: %v", err)
5656
}
5757
resp.Result = result
58-
58+
5959
w.Header().Set("Content-Type", "application/json")
6060
w.WriteHeader(http.StatusOK)
6161
json.NewEncoder(w).Encode(resp)
6262
})
63-
63+
6464
server, socketPath := createTestServer(t, handler)
6565
defer server.Close()
66-
66+
6767
client := NewClient(&Config{Socket: socketPath})
6868
snap, err := client.Snap("test-snap")
69-
69+
7070
if err != nil {
7171
t.Fatalf("Expected no error, got: %v", err)
7272
}
@@ -90,13 +90,13 @@ func TestSnap_NotFound(t *testing.T) {
9090
}
9191
json.NewEncoder(w).Encode(resp)
9292
})
93-
93+
9494
server, socketPath := createTestServer(t, handler)
9595
defer server.Close()
96-
96+
9797
client := NewClient(&Config{Socket: socketPath})
9898
_, err := client.Snap("nonexistent")
99-
99+
100100
if err == nil {
101101
t.Fatal("Expected error for non-existent snap")
102102
}
@@ -114,13 +114,13 @@ func TestSnap_UnexpectedStatusCode(t *testing.T) {
114114
}
115115
json.NewEncoder(w).Encode(resp)
116116
})
117-
117+
118118
server, socketPath := createTestServer(t, handler)
119119
defer server.Close()
120-
120+
121121
client := NewClient(&Config{Socket: socketPath})
122122
_, err := client.Snap("test-snap")
123-
123+
124124
if err == nil {
125125
t.Fatal("Expected error for 500 status code")
126126
}
@@ -134,7 +134,7 @@ func TestFindOne_Success(t *testing.T) {
134134
if r.URL.Query().Get("name") != "test-snap" {
135135
t.Errorf("Expected query param name=test-snap, got: %s", r.URL.Query().Get("name"))
136136
}
137-
137+
138138
resp := response{
139139
Type: "sync",
140140
Status: "OK",
@@ -162,18 +162,18 @@ func TestFindOne_Success(t *testing.T) {
162162
t.Fatalf("failed to marshal snaps response: %v", err)
163163
}
164164
resp.Result = result
165-
165+
166166
w.Header().Set("Content-Type", "application/json")
167167
w.WriteHeader(http.StatusOK)
168168
json.NewEncoder(w).Encode(resp)
169169
})
170-
170+
171171
server, socketPath := createTestServer(t, handler)
172172
defer server.Close()
173-
173+
174174
client := NewClient(&Config{Socket: socketPath})
175175
snap, err := client.FindOne("test-snap")
176-
176+
177177
if err != nil {
178178
t.Fatalf("Expected no error, got: %v", err)
179179
}
@@ -194,13 +194,13 @@ func TestFindOne_NotFound(t *testing.T) {
194194
}
195195
json.NewEncoder(w).Encode(resp)
196196
})
197-
197+
198198
server, socketPath := createTestServer(t, handler)
199199
defer server.Close()
200-
200+
201201
client := NewClient(&Config{Socket: socketPath})
202202
_, err := client.FindOne("nonexistent")
203-
203+
204204
if err == nil {
205205
t.Fatal("Expected error for non-existent snap")
206206
}
@@ -221,18 +221,18 @@ func TestFindOne_EmptyResults(t *testing.T) {
221221
t.Fatalf("failed to marshal snaps: %v", err)
222222
}
223223
resp.Result = result
224-
224+
225225
w.Header().Set("Content-Type", "application/json")
226226
w.WriteHeader(http.StatusOK)
227227
json.NewEncoder(w).Encode(resp)
228228
})
229-
229+
230230
server, socketPath := createTestServer(t, handler)
231231
defer server.Close()
232-
232+
233233
client := NewClient(&Config{Socket: socketPath})
234234
_, err := client.FindOne("nonexistent")
235-
235+
236236
if err == nil {
237237
t.Fatal("Expected error for empty results")
238238
}

0 commit comments

Comments
 (0)