graphql-engine/cli/metadata/version/version.go
Aravind Shankar bb63d7e60e
cli: allow managing actions (#3859)
Co-authored-by: Rishichandra Wawhal <rishichandra.wawhal@gmail.com>
Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com>
Co-authored-by: Aravind <aravindkp@outlook.in>
Co-authored-by: Anon Ray <ecthiender@users.noreply.github.com>
Co-authored-by: Shahidh K Muhammed <muhammedshahid.k@gmail.com>
2020-02-24 21:44:46 +05:30

90 lines
1.5 KiB
Go

package version
import (
"io/ioutil"
"path/filepath"
"github.com/hasura/graphql-engine/cli"
"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 {
return &VersionConfig{
MetadataDir: baseDir,
}
}
func (a *VersionConfig) Validate() error {
return nil
}
func (a *VersionConfig) CreateFiles() error {
v := Version{
Version: 2,
}
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.Join(a.MetadataDir, fileName): data,
}, nil
}
func (a *VersionConfig) Name() string {
return "version"
}