-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to clean files not generated by releasepost (#36)
Signed-off-by: Olivier Vernin <[email protected]>
- Loading branch information
Showing
8 changed files
with
188 additions
and
41 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 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,70 @@ | ||
package engine | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/updatecli/releasepost/internal/core/result" | ||
) | ||
|
||
// Clean analyze changelog monitored directories and remove any changelog that | ||
// weren't created or modified by releasepost. | ||
func (e *Engine) Clean(clean bool) error { | ||
|
||
fmt.Printf("\n\nCleaning\n") | ||
for dirpath, dir := range e.result.Dir { | ||
files, err := filepath.Glob(filepath.Join(dirpath, "*")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, f := range files { | ||
info, err := os.Stat(f) | ||
if err != nil { | ||
fmt.Printf("unable to get file info: %v\n", err.Error()) | ||
continue | ||
} | ||
|
||
if info.IsDir() { | ||
continue | ||
} | ||
|
||
if !dir.IsFileExist(f) { | ||
err := filepath.Walk(f, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
result.ChangelogResult.UnTracked = append(result.ChangelogResult.UnTracked, f) | ||
if clean { | ||
fmt.Printf("\t* removing %s\n", f) | ||
err = os.Remove(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
if len(result.ChangelogResult.UnTracked) > 0 { | ||
fmt.Printf("Untracked files detected:\n") | ||
for _, f := range result.ChangelogResult.UnTracked { | ||
fmt.Printf("\t* %s\n", f) | ||
} | ||
} | ||
|
||
fmt.Printf("\n\n") | ||
return 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package result | ||
|
||
import "path/filepath" | ||
|
||
const ( | ||
// Created state | ||
CREATEDSTATE = "created" | ||
// Modified state | ||
MODIFIEDSTATE = "modified" | ||
// UnModified state | ||
UNMODIFIEDSTATE = "unmodified" | ||
// UnTracked state | ||
UNTRACKEDSTATE = "untracked" | ||
) | ||
|
||
type Changelog struct { | ||
Created []string `json:"created"` | ||
Modified []string `json:"modified"` | ||
UnModified []string `json:"unmodified"` | ||
UnTracked []string `json:"untracked"` | ||
} | ||
|
||
func (c Changelog) ExitCode() int { | ||
if len(c.Created) == 0 && len(c.Modified) == 0 { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
// UpdateResult update the result with the current changelog information | ||
func (c Changelog) UpdateResult(result *Result) error { | ||
|
||
parser := func(files []string, state string) { | ||
for _, f := range files { | ||
dirname := filepath.Dir(f) | ||
|
||
if result.Dir[dirname].IsFileExist(f) { | ||
continue | ||
} | ||
|
||
if result.Dir == nil { | ||
result.Dir = make(map[string]FileResults) | ||
} | ||
|
||
result.Dir[dirname] = append( | ||
result.Dir[dirname], | ||
FileResult{ | ||
Path: f, | ||
State: state, | ||
}) | ||
} | ||
} | ||
|
||
parser(c.Created, CREATEDSTATE) | ||
parser(c.Modified, MODIFIEDSTATE) | ||
parser(c.UnModified, UNMODIFIEDSTATE) | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,5 @@ | ||
package result | ||
|
||
import "fmt" | ||
|
||
type Changelog struct { | ||
Created []string `json:"created"` | ||
Modified []string `json:"modified"` | ||
UnModified []string `json:"unmodified"` | ||
} | ||
|
||
var ( | ||
ChangelogResult Changelog | ||
) | ||
|
||
func (c Changelog) String() { | ||
if len(c.Created) > 0 { | ||
fmt.Println("Changelogs reposted:") | ||
for _, v := range c.Created { | ||
fmt.Printf("\t* %s\n", v) | ||
} | ||
} | ||
|
||
if len(c.Modified) > 0 { | ||
fmt.Println("Changelogs modified:") | ||
for _, v := range c.Modified { | ||
fmt.Printf("\t* %s\n", v) | ||
} | ||
} | ||
|
||
if len(c.UnModified) > 0 { | ||
fmt.Println("Changelogs unmodified:") | ||
for _, v := range c.UnModified { | ||
fmt.Printf("\t* %s\n", v) | ||
} | ||
} | ||
} | ||
|
||
func (c Changelog) ExitCode() int { | ||
if len(c.Created) == 0 && len(c.Modified) == 0 { | ||
return 1 | ||
} | ||
return 0 | ||
} |
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,35 @@ | ||
package result | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type FileResult struct { | ||
State string | ||
Path string | ||
} | ||
|
||
type FileResults []FileResult | ||
|
||
type Result struct { | ||
Dir map[string]FileResults | ||
} | ||
|
||
func (f FileResults) IsFileExist(file string) bool { | ||
for _, f := range f { | ||
if f.Path == file { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (f Result) String() string { | ||
s := "Result\n" | ||
for _, files := range f.Dir { | ||
for _, f := range files { | ||
s = fmt.Sprintf("%s\t* %s (%s)\n", s, f.Path, f.State) | ||
} | ||
} | ||
return s | ||
} |