Skip to content

Commit

Permalink
feat: add option to clean files not generated by releasepost (#36)
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Vernin <[email protected]>
  • Loading branch information
olblak committed May 17, 2024
1 parent bc15e5a commit 8a3e211
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 41 deletions.
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
configFile string
e engine.Engine
dryRun bool
cleanRun bool

rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -36,7 +37,10 @@ var (
dryrun.Enabled = true
fmt.Println("Dry run mode enabled, no changelog will be saved to disk")
}
err = e.Run()
err = e.Run(cleanRun)
if cleanRun {
fmt.Println("Clean run mode enabled, releasepost will remove any files not created by releasepost in changelogs directories !")
}
if err != nil {
fmt.Printf("Failed to run releasepost: %v", err)
os.Exit(2)
Expand All @@ -59,6 +63,7 @@ It can creates files using different formats like markdown, asciidoctor, or json
func init() {
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "releasepost configuration file")
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry run mode")
rootCmd.PersistentFlags().BoolVar(&cleanRun, "clean", false, "Clean run, removes files from changelog directories not created by releasepost.")
rootCmd.AddCommand(
versionCmd,
)
Expand Down
70 changes: 70 additions & 0 deletions internal/core/engine/clean.go
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
}
4 changes: 4 additions & 0 deletions internal/core/engine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ func (e *Engine) Init() error {
e.runners = append(e.runners, runner)
}

if len(e.config.Changelogs) == 0 {
return fmt.Errorf("no changelog found in configuration file")
}

return nil
}
2 changes: 2 additions & 0 deletions internal/core/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"github.com/updatecli/releasepost/internal/core/config"
"github.com/updatecli/releasepost/internal/core/result"
"github.com/updatecli/releasepost/internal/core/runner"
)

Expand All @@ -11,4 +12,5 @@ Engine is the main structure of the application
type Engine struct {
config config.Config
runners []runner.Runner
result result.Result
}
14 changes: 12 additions & 2 deletions internal/core/engine/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
Run executes the engine.
It will run all the runners and save the changelogs to disk.
*/
func (e *Engine) Run() error {
func (e *Engine) Run(cleanRun bool) error {

for i := range e.config.Changelogs {
changelogs, err := e.runners[i].Run()
Expand All @@ -35,9 +35,19 @@ func (e *Engine) Run() error {
fmt.Printf("unable to save changelog index to disk: %v\n", err.Error())
continue
}

err = result.ChangelogResult.UpdateResult(&e.result)
if err != nil {
fmt.Printf("unable to update result: %v\n", err.Error())
continue
}
}

if err := e.Clean(cleanRun); err != nil {
fmt.Printf("unable to clean changelog: %v\n", err.Error())
}

result.ChangelogResult.String()
fmt.Println(e.result)

return nil
}
59 changes: 59 additions & 0 deletions internal/core/result/changelog.go
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
}
38 changes: 0 additions & 38 deletions internal/core/result/main.go
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
}
35 changes: 35 additions & 0 deletions internal/core/result/result.go
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
}

0 comments on commit 8a3e211

Please sign in to comment.