Skip to content

Commit

Permalink
Better error handling, require seed
Browse files Browse the repository at this point in the history
  • Loading branch information
danielingegneri committed Apr 14, 2021
1 parent 39691de commit 12d6fb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
9 changes: 7 additions & 2 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"git.jcu.edu.au/cft/cfds/crypt"
"github.com/ansel1/merry"
"github.com/spf13/cobra"
)

Expand All @@ -13,11 +14,15 @@ var decryptCmd = &cobra.Command{
Use: "decrypt",
Short: "Given a seed and a password, will decrpyt to STDOUT",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if seed == "" {
return merry.New("Requires seed parameter")
}
decrypted, err := crypt.Decrypt(args[0], seed)
if err != nil {
panic(err)
return err
}
println(decrypted)
return nil
},
}
9 changes: 7 additions & 2 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"git.jcu.edu.au/cft/cfds/crypt"
"github.com/ansel1/merry"
"github.com/spf13/cobra"
)

Expand All @@ -13,11 +14,15 @@ var encryptCmd = &cobra.Command{
Use: "encrypt",
Short: "Given a seed and a password, will encrypt to STDOUT, encoded as base64",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if seed == "" {
return merry.New("Requires seed parameter")
}
encrypted, err := crypt.Encrypt(args[0], seed)
if err != nil {
panic(err)
return err
}
println(encrypted)
return nil
},
}
29 changes: 17 additions & 12 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"git.jcu.edu.au/cft/cfds/crypt"
"git.jcu.edu.au/cft/cfds/datasources"
"github.com/ansel1/merry"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
Expand All @@ -28,69 +29,73 @@ func init() {
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generates the neo-datasource.xml file that contains the Coldfusion datasources.",
Run: func(cmd *cobra.Command, args []string) {
generateXmlFile()
RunE: func(cmd *cobra.Command, args []string) error {
return generateXmlFile()
},
}

func generateXmlFile() {
func generateXmlFile() error {
if seed == "" {
return merry.New("Requires seed parameter")
}
templateDir := "templates"
flag.Parse()
if seed == "" || input == "" {
flag.Usage()
}
inFile, err := os.Open(input)
if err != nil {
panic(err)
return merry.Wrap(err)
}
defer inFile.Close()
d := yaml.NewDecoder(inFile)
doc := datasources.NewDoc()
err = d.Decode(doc)
if err != nil {
panic("Error reading datasource file " + err.Error())
return merry.New("Error reading datasource file").WithCause(err)
}

start := "<wddxPacket version='1.0'><header/><data><array length='2'><struct type='coldfusion.server.ConfigMap'>"
end := "</struct><struct type='coldfusion.server.ConfigMap'><var name='maxcachecount'><number>100.0</number></var></struct></array></data></wddxPacket>"

outFile, err := os.Create(output)
if err != nil {
panic(err)
return merry.Wrap(err)
}
defer outFile.Close()

templates := map[string]*template.Template{}

if _, err = outFile.WriteString(start); err != nil {
panic(err)
return merry.Wrap(err)
}
for ds, data := range doc.Datasources {
data.Name = ds
data.Password, err = crypt.Encrypt(data.Password, seed)
if err != nil {
panic("Could not encrypt password")
return merry.New("Could not encrypt password").WithCause(err)
}
key := strings.ToLower(data.Type)
templateFile := filepath.Join(templateDir, key+".xml")
if _, ok := templates[key]; !ok {
// Load template
content, err := ioutil.ReadFile(templateFile)
if err != nil {
panic("Cannot load template " + templateFile)
return merry.New("Cannot load template " + templateFile).WithCause(err)
}
templates[key], err = template.New(key).Parse(string(content))
if err != nil {
panic(err)
return merry.Wrap(err)
}
}
err = templates[key].Execute(outFile, data)
if err != nil {
panic(err)
return merry.Wrap(err)
}
}
if _, err = outFile.WriteString(end); err != nil {
panic(err)
return merry.Wrap(err)
}
fmt.Printf("Wrote %d datasources\n", len(doc.Datasources))
return nil
}

0 comments on commit 12d6fb6

Please sign in to comment.