Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created fogg exp yaml_migrate command #282

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/exp/yaml_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package exp

import (
"github.com/chanzuckerberg/fogg/exp/yaml_migrate"
"github.com/spf13/cobra"
)

func init() {
ExpCmd.AddCommand(yamlMigrateCmd)
}

var yamlMigrateCmd = &cobra.Command{
Use: "yaml_migrate",
Short: "Converts fogg.json to fogg.yml",
Long: "This command will convert the fogg.json to a fogg.yml file type.",
RunE: func(cmd *cobra.Command, args []string) error {
return yaml_migrate.JSONtoYML()
},
}
14 changes: 7 additions & 7 deletions config/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ type TravisCI struct {
}

type Config struct {
Accounts map[string]Account `json:"accounts"`
Defaults Defaults `json:"defaults"`
Docker bool `json:"docker,omitempty"`
Envs map[string]Env `json:"envs"`
Modules map[string]Module `json:"modules"`
Plugins Plugins `json:"plugins,omitempty"`
TravisCI *TravisCI `json:"travis_ci,omitempty"`
Accounts map[string]Account `json:"accounts" json:"accounts"`
Defaults Defaults `json:"defaults" json:"defaults"`
Docker bool `json:"docker,omitempty" json:"docker,omitempty"`
Envs map[string]Env `json:"envs" json:"envs"`
Modules map[string]Module `json:"modules" json:"modules"`
Plugins Plugins `json:"plugins,omitempty" json:"plugins,omitempty"`
TravisCI *TravisCI `json:"travis_ci,omitempty" json:"travis_ci,omitempty"`
}

func ReadConfig(b []byte) (*Config, error) {
Expand Down
57 changes: 57 additions & 0 deletions exp/yaml_migrate/yaml_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package yaml_migrate

import (
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v2"
)

//TODO: Better Error Handling
echanakira marked this conversation as resolved.
Show resolved Hide resolved
//Converts fogg.json to fogg.yml
func JSONtoYML() error {
jsonFile, err := os.Open("fogg.json")
check(err)
fmt.Println("Successfully Opened fogg.json")
echanakira marked this conversation as resolved.
Show resolved Hide resolved
defer jsonFile.Close()

//Read the fogg.json file, convert it to yml format
byteValue, _ := ioutil.ReadAll(jsonFile)
echanakira marked this conversation as resolved.
Show resolved Hide resolved
ymlData, _ := JSONtoYAML(byteValue)
echanakira marked this conversation as resolved.
Show resolved Hide resolved

generateYMLFile(ymlData)
fmt.Println("Successfully created fogg.yml")
echanakira marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

//Convert an existing json file into yml data
func JSONtoYAML(jsonFileData []byte) ([]byte, error) {
var jsonObj interface{}

//Convert jsonFileData into a generic interface representing json
err := yaml.Unmarshal(jsonFileData, &jsonObj)
if err != nil {
return nil, err
}

// Convert generic json object into yml data
return yaml.Marshal(jsonObj)
}

//TODO: Consider overwritteing yml file
echanakira marked this conversation as resolved.
Show resolved Hide resolved
// Create YML file
func generateYMLFile(ymlData []byte) {
file, createErr := os.Create("fogg.yml")
check(createErr)
echanakira marked this conversation as resolved.
Show resolved Hide resolved
defer file.Close()

writeErr := ioutil.WriteFile("fogg.yml", ymlData, 0644)
check(writeErr)
}

func check(e error) {
if e != nil {
panic(e)
}
}
24 changes: 21 additions & 3 deletions init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/json"

"github.com/chanzuckerberg/fogg/config"
"github.com/chanzuckerberg/fogg/config/v1"
v1 "github.com/chanzuckerberg/fogg/config/v1"
"github.com/chanzuckerberg/fogg/errs"
prompt "github.com/segmentio/go-prompt"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)

const AWSProviderVersion = "1.27.0"
Expand All @@ -25,15 +26,32 @@ func userPrompt() (string, string, string, string, string, string) {

func writeConfig(fs afero.Fs, config *v1.Config) error {
json, e := json.MarshalIndent(config, "", " ")
yaml, yamlErr := yaml.Marshal(config)
echanakira marked this conversation as resolved.
Show resolved Hide resolved

if e != nil {
return errs.WrapInternal(e, "unable to marshal json")
} else if yamlErr != nil {
return errs.WrapInternal(yamlErr, "unable to marshal yaml")
}
configFile, e := fs.Create("fogg.json")
yamlConfigFile, yamlErr := fs.Create("fogg.yml")

if e != nil {
return errs.WrapInternal(e, "unable to create config file fogg.json")

} else if yamlErr != nil {
return errs.WrapInternal(yamlErr, "unable to create config file fogg.yml")
}
_, jsonError := configFile.Write(json)
_, yamlError := yamlConfigFile.Write(yaml)

//Created to return error
if jsonError != nil {
return jsonError
} else if yamlError != nil {
return yamlError
}
_, e3 := configFile.Write(json)
return e3
return nil
}

func Init(fs afero.Fs) error {
Expand Down