-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor jenfile into project pkg and use straight yaml marshalling
Signed-off-by: Mathieu Frenette <[email protected]>
- Loading branch information
Showing
9 changed files
with
66 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package project | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/Samasource/jen/src/internal/constant" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// JenFile represents the configuration file in a project's root dir | ||
type JenFile struct { | ||
Version string | ||
TemplateName string | ||
Variables map[string]string | ||
} | ||
|
||
// Save saves jen file into given project directory | ||
func (jenFile JenFile) Save(dir string) error { | ||
jenFile.Version = constant.JenFileVersion | ||
doc, err := yaml.Marshal(jenFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
filePath := path.Join(dir, constant.JenFileName) | ||
return ioutil.WriteFile(filePath, doc, os.ModePerm) | ||
} | ||
|
||
// Load loads the jen file from given project directory | ||
func Load(dir string) (*JenFile, error) { | ||
specFilePath := filepath.Join(dir, constant.JenFileName) | ||
buf, err := ioutil.ReadFile(specFilePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("loading jen file: %w", err) | ||
} | ||
var jenFile JenFile | ||
err = yaml.Unmarshal(buf, &jenFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("unmarshalling jen file yaml: %w", err) | ||
} | ||
|
||
if jenFile.Version != constant.JenFileVersion { | ||
return nil, fmt.Errorf("unsupported jenfile version %s (expected %s)", jenFile.Version, constant.JenFileVersion) | ||
} | ||
return &jenFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters