Skip to content

Commit b4fa2b8

Browse files
committed
refactor(grpccmd): make oci config use yaml file in order to specify configs
- mainly to increase readability and overall code quality
1 parent 3445ea7 commit b4fa2b8

4 files changed

Lines changed: 29 additions & 31 deletions

File tree

api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type MetalConfig struct {
1414
Partition string `json:"partition"`
1515
// Logging contains logging configurations passed to metal-hammer
1616
Logging *Logging `json:"logging,omitempty"`
17-
OciConfigs map[string]*OciCredentials `json:"oci_config,omitempty"`
17+
OciConfigs map[string]*OciCredentials `json:"oci_configs,omitempty"`
1818
}
1919

2020
type Logging struct {
@@ -48,9 +48,9 @@ type CertificateAuth struct {
4848

4949
type OciCredentials struct {
5050
// Username that is capable of logging in to the registry
51-
Username string `json:"username,omitempty"`
51+
Username string `json:"username,omitempty" yaml:"username,omitempty"`
5252
// Password for the user
53-
Password string `json:"password,omitempty"`
53+
Password string `json:"password,omitempty" yaml:"password,omitempty"`
5454
}
5555

5656
// LogType defines which logging backend should be used

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/metal-stack/pixie
33
go 1.25
44

55
require (
6+
github.com/goccy/go-yaml v1.19.2
67
github.com/metal-stack/metal-api v0.42.4
78
github.com/metal-stack/v v1.0.3
89
github.com/pin/tftp/v3 v3.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
1515
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1616
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
1717
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
18+
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
19+
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
1820
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
1921
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
2022
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=

pixiecore/cli/grpccmd.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"strings"
2222

23+
yaml "github.com/goccy/go-yaml"
2324
"github.com/metal-stack/pixie/api"
2425
"github.com/metal-stack/pixie/pixiecore"
2526
"github.com/spf13/cobra"
@@ -222,44 +223,38 @@ func getMetalAPIConfig(cmd *cobra.Command) (*api.MetalConfig, error) {
222223
}
223224
}
224225

225-
metalHammerOciConfigs, err := cmd.Flags().GetStringSlice("metal-hammer-oci-configs")
226+
metalHammerOciConfigFilePath, err := cmd.Flags().GetString("metal-hammer-oci-config-file-path")
226227
if err != nil {
227228
return nil, fmt.Errorf("error reading flag: %w", err)
228229
}
229230

230231
ociConfigs := make(map[string]*api.OciCredentials)
232+
if metalHammerOciConfigFilePath != "" {
233+
metalHammerOciConfigFileContent, err := os.ReadFile(metalHammerOciConfigFilePath)
234+
if err != nil {
235+
return nil, fmt.Errorf("error retrieving oci configs file: %w", err)
236+
}
231237

232-
for _, c := range metalHammerOciConfigs {
233-
var (
234-
ociCredentials *api.OciCredentials
235-
registryURL string
236-
)
238+
type MetalHammerOciConfig struct {
239+
RegistryURL string `yaml:"registry-url"`
240+
Credentials *api.OciCredentials
241+
}
242+
type MetalHammerOciConfigs struct {
243+
OciConfigs []MetalHammerOciConfig `yaml:"oci-configs"`
244+
}
245+
var metalHammerOciConfigs MetalHammerOciConfigs
246+
err = yaml.Unmarshal(metalHammerOciConfigFileContent, &metalHammerOciConfigs)
247+
if err != nil {
248+
return nil, fmt.Errorf("error parsing oci configs: %w", err)
249+
}
237250

238-
parts := strings.SplitSeq(c, ",")
239-
for p := range parts {
240-
kv := strings.SplitN(strings.TrimSpace(p), "=", 2)
241-
if len(kv) != 2 {
242-
return nil, fmt.Errorf("invalid key-value pair in OCI config: %q", p)
251+
for _, config := range metalHammerOciConfigs.OciConfigs {
252+
if config.RegistryURL == "" {
253+
return nil, fmt.Errorf("no registry url specified for oci config: %+v", config)
243254
}
244255

245-
k := strings.ToLower(strings.TrimSpace(kv[0]))
246-
v := strings.TrimSpace(kv[1])
247-
switch k {
248-
case "registry_url":
249-
if v == "" {
250-
return nil, fmt.Errorf("no registry url specified for oci config: %s", c)
251-
}
252-
registryURL = v
253-
case "username":
254-
ociCredentials.Username = v
255-
case "password":
256-
ociCredentials.Password = v
257-
default:
258-
return nil, fmt.Errorf("unknown key %q in OCI config", k)
259-
}
256+
ociConfigs[config.RegistryURL] = config.Credentials
260257
}
261-
262-
ociConfigs[registryURL] = ociCredentials
263258
}
264259

265260
return &api.MetalConfig{

0 commit comments

Comments
 (0)