Skip to content

Commit

Permalink
feat: config file schema versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
zaremba-tomasz committed Oct 3, 2023
1 parent 4d9a793 commit cdcc960
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions assets/versions/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cliVersion": "v0.5.0",
"configFileSchemaVersion": "v1"
}
9 changes: 9 additions & 0 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import (

"github.com/nearform/initium-cli/src/services/k8s"
"github.com/nearform/initium-cli/src/services/project"
"github.com/nearform/initium-cli/src/services/versions"
"github.com/nearform/initium-cli/src/utils/defaults"
"github.com/urfave/cli/v2"
)

const (
persistFlag = "persist"

configFileSchemaVersionFlagName = "schema-version"
)

func (c icli) InitGithubCMD(cCtx *cli.Context) error {
Expand Down Expand Up @@ -83,6 +86,12 @@ func (c icli) InitConfigCMD(ctx *cli.Context) error {
config = config + next
}

configFileSchemaVersion, err := versions.LoadConfigFileSchemaVersion(c.Resources)
if err != nil {
return err
}
config = fmt.Sprintf("%s: %s\n", configFileSchemaVersionFlagName, configFileSchemaVersion) + config

if ctx.Bool(persistFlag) {
f, err := os.OpenFile(filepath.Join(ctx.String(projectDirectoryFlag), defaults.ConfigFile), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import (
)

func compareConfig(t *testing.T, appName string, registry string, writer io.Writer) {
configTemplate := fmt.Sprintf(`app-name: %s
currentConfigVersion := "v1"
configTemplate := fmt.Sprintf(`schema-version: %s
app-name: %s
cluster-endpoint: null
container-registry: %s
default-branch: main
dockerfile-name: null
registry-user: null
runtime-version: null
`,
currentConfigVersion,
appName,
registry,
)
Expand Down
27 changes: 27 additions & 0 deletions src/services/versions/versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package versions

import (
"encoding/json"
"io/fs"
"path"
)

type VersionsFile struct {
CliVersion string `json:"cliVersion"`
ConfigFileSchemaVersion string `json:"configFileSchemaVersion"`
}

func LoadConfigFileSchemaVersion(resources fs.FS) (string, error) {
bytes, err := fs.ReadFile(resources, path.Join("assets", "versions", "versions.json"))
if err != nil {
return "", err
}

var versionsFile VersionsFile
err = json.Unmarshal(bytes, &versionsFile)
if err != nil {
return "", err
}

return versionsFile.ConfigFileSchemaVersion, nil
}

0 comments on commit cdcc960

Please sign in to comment.