Skip to content

Commit

Permalink
cli: pass config structure to Snapshots structure instead of one spec…
Browse files Browse the repository at this point in the history
…ific option

Signed-off-by: Jérôme Jutteau <[email protected]>
  • Loading branch information
jerome-jutteau committed Mar 16, 2022
1 parent 761f0bf commit efa7bb5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/frieza/cli_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func clean(customConfigPath string, snapshotName *string, plan bool, autoApprove
if err != nil {
log.Fatalf("Cannot load configuration: %s", err.Error())
}
snapshot, err := SnapshotLoad(*snapshotName, config.SnapshotFolderPath)
snapshot, err := SnapshotLoad(*snapshotName, config)
if err != nil {
log.Fatalf("Error load snapshot %s: %s", *snapshotName, err.Error())
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/frieza/cli_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func snapshotNew(customConfigPath string, args []string) {
if err != nil {
log.Fatalf("Cannot load configuration: %s", err.Error())
}
if _, err = SnapshotLoad(snapshotName, config.SnapshotFolderPath); err == nil {
if _, err = SnapshotLoad(snapshotName, config); err == nil {
log.Fatalf("Snapshot %s already exist", snapshotName)
}

Expand Down Expand Up @@ -121,6 +121,7 @@ func snapshotNew(customConfigPath string, args []string) {
Version: SnapshotVersion(),
Name: snapshotName,
Date: date,
Config: config,
}
for i, provider := range providers {
snapshot.Data = append(snapshot.Data, SnapshotData{
Expand Down Expand Up @@ -151,7 +152,7 @@ func snapshotLs(customConfigPath string) {
continue
}
snapshotName := strings.TrimSuffix(file.Name(), ".json")
if snapshot, err := SnapshotLoad(snapshotName, config.SnapshotFolderPath); err == nil {
if snapshot, err := SnapshotLoad(snapshotName, config); err == nil {
fmt.Println(snapshot.Name)
}
}
Expand All @@ -169,7 +170,7 @@ func snapshotDescribe(customConfigPath string, snapshotName *string) {
if err != nil {
log.Fatalf("Error while reading snapshots: %s", err.Error())
}
snapshot, err := SnapshotLoad(*snapshotName, config.SnapshotFolderPath)
snapshot, err := SnapshotLoad(*snapshotName, config)
if err != nil {
log.Fatalf("Cannot load snapshot %s: %s", *snapshotName, err.Error())
}
Expand All @@ -185,7 +186,7 @@ func snapshotRm(customConfigPath string, snapshotName *string) {
if err != nil {
log.Fatalf("Cannot load configuration: %s", err.Error())
}
snapshot, err := SnapshotLoad(*snapshotName, config.SnapshotFolderPath)
snapshot, err := SnapshotLoad(*snapshotName, config)
if err != nil {
log.Fatalf("Error while deleting snapshot %s: %s", *snapshotName, err.Error())
}
Expand Down
20 changes: 10 additions & 10 deletions internal/common/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
type Objects = map[ObjectType][]Object

type Snapshot struct {
Version int `json:"version"`
Name string `json:"name"`
Date string `json:"date"`
Data []SnapshotData `json:"data"`
FolderPath string `json:"-"`
Version int `json:"version"`
Name string `json:"name"`
Date string `json:"date"`
Data []SnapshotData `json:"data"`
Config *Config `json:"-"`
}

type SnapshotData struct {
Expand Down Expand Up @@ -130,7 +130,7 @@ func ObjectsPrint(provider *Provider, objects *Objects) string {
}

func (snapshot *Snapshot) Write() error {
if err := os.MkdirAll(snapshot.FolderPath, os.ModePerm); err != nil {
if err := os.MkdirAll(snapshot.Config.SnapshotFolderPath, os.ModePerm); err != nil {
return err
}
json_bytes, err := json.MarshalIndent(snapshot, "", " ")
Expand All @@ -144,13 +144,13 @@ func (snapshot *Snapshot) Write() error {
}

func (snapshot *Snapshot) Path() string {
return path.Join(snapshot.FolderPath, snapshot.Name+".json")
return path.Join(snapshot.Config.SnapshotFolderPath, snapshot.Name+".json")
}

func SnapshotLoad(name string, snapshotFolderPath string) (*Snapshot, error) {
func SnapshotLoad(name string, config *Config) (*Snapshot, error) {
snapshot := &Snapshot{
Name: name,
FolderPath: snapshotFolderPath,
Name: name,
Config: config,
}
snapshot_json, err := ioutil.ReadFile(snapshot.Path())
if err != nil {
Expand Down

0 comments on commit efa7bb5

Please sign in to comment.