Skip to content

Commit

Permalink
Merge pull request #144 from nicholasSUSE/update-lifecycle-status
Browse files Browse the repository at this point in the history
Update lifecycle status
  • Loading branch information
nicholasSUSE authored Jul 30, 2024
2 parents 2a7c333 + d6502a2 commit 3525298
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 114 deletions.
5 changes: 4 additions & 1 deletion pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ func (g *Git) FetchAndCheckoutBranch(branch string) error {

// FetchBranch fetches a branch
func (g *Git) FetchBranch(branch string) error {
cmd := exec.Command("git", "-C", g.Dir, "fetch", "origin", branch+":"+branch)

remote := g.Remotes["https://github.com/rancher/charts"]

cmd := exec.Command("git", "-C", g.Dir, "fetch", remote, branch+":"+branch)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down
97 changes: 97 additions & 0 deletions pkg/lifecycle/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package lifecycle

import (
"encoding/json"
"errors"
"io"
"os"

"github.com/go-git/go-billy/v5"
"github.com/rancher/charts-build-scripts/pkg/filesystem"
"github.com/rancher/charts-build-scripts/pkg/path"
)

// SaveState will save the lifecycle-status state to state.json file at charts repo
func (s *Status) SaveState() error {
// Marshal the Project struct into JSON
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}

// Write the JSON data to a file
if err := os.WriteFile(s.StateFile, data, 0644); err != nil {
return err
}

return nil
}

// LoadState will load the lifecycle-status state from an existing state.json file at charts repo
func LoadState(rootFs billy.Filesystem) (*Status, error) {
// get the absolute path for the state.json file
stateFilePath := filesystem.GetAbsPath(rootFs, path.RepositoryStateFile)
s := &Status{
StateFile: stateFilePath,
}

exist, err := s.checkStateFileExist()
if err != nil {
return nil, err
}
if !exist {
return nil, err
}

// Read the file content
file, _ := os.Open(s.StateFile)
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}

// Unmarshal the JSON data into the struct
if err := json.Unmarshal(data, &s); err != nil {
return nil, err
}

return s, nil
}

// checkStateFileExist will check if the state.json file exists at the charts repo
func (s *Status) checkStateFileExist() (bool, error) {
if _, err := os.Stat(s.StateFile); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}

return false, err
}

return true, nil
}

// createStateFile will create a new state file at the charts repo
func (s *Status) createStateFile() error {
stateFilePath := filesystem.GetAbsPath(s.ld.rootFs, path.RepositoryStateFile)

if _, err := os.Create(stateFilePath); err != nil {
return err
}

s.StateFile = stateFilePath
return nil
}

// initState will create a new state file and save the state to it
func (s *Status) initState() error {
if err := s.createStateFile(); err != nil {
return err
}

if err := s.SaveState(); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 3525298

Please sign in to comment.