graphql-engine/cli/internal/metadataobject/version/version.go
Aravind K P 3136ffad1b cli: update go.mod
>

### Description
Update `go.mod` to allow other packages to import [v2.0.0 versions](https://blog.golang.org/v2-go-modules).

### Changelog

- [x] `CHANGELOG.md` is updated with user-facing content relevant to this PR. If no changelog is required, then add the `no-changelog-required` label.

### Affected components

- [x] CLI

https://github.com/hasura/graphql-engine-mono/pull/1584

GitOrigin-RevId: a5d17ad20289d1cd7217763f56ef3ba6552d69c4
2021-06-16 11:45:07 +00:00

91 lines
1.6 KiB
Go

package version
import (
"io/ioutil"
"path/filepath"
"github.com/hasura/graphql-engine/cli/v2"
"gopkg.in/yaml.v2"
)
const (
fileName string = "version.yaml"
)
type Version struct {
Version int `json:"version" yaml:"version"`
}
type VersionConfig struct {
MetadataDir string
}
func New(ec *cli.ExecutionContext, baseDir string) *VersionConfig {
ec.Version.GetServerFeatureFlags()
return &VersionConfig{
MetadataDir: baseDir,
}
}
func (a *VersionConfig) Validate() error {
return nil
}
func (a *VersionConfig) CreateFiles() error {
v := Version{
Version: 3,
}
data, err := yaml.Marshal(v)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(a.MetadataDir, fileName), data, 0644)
if err != nil {
return err
}
return nil
}
func (a *VersionConfig) Build(metadata *yaml.MapSlice) error {
data, err := ioutil.ReadFile(filepath.Join(a.MetadataDir, fileName))
if err != nil {
return err
}
var v Version
err = yaml.Unmarshal(data, &v)
if err != nil {
return err
}
item := yaml.MapItem{
Key: "version",
Value: v.Version,
}
*metadata = append(*metadata, item)
return nil
}
func (a *VersionConfig) Export(metadata yaml.MapSlice) (map[string][]byte, error) {
var version int
for _, item := range metadata {
k, ok := item.Key.(string)
if !ok || k != "version" {
continue
}
version = item.Value.(int)
}
v := Version{
Version: version,
}
data, err := yaml.Marshal(v)
if err != nil {
return nil, err
}
return map[string][]byte{
filepath.ToSlash(filepath.Join(a.MetadataDir, fileName)): data,
}, nil
}
func (a *VersionConfig) Name() string {
return "version"
}