Skip to content

Commit

Permalink
Add release notes (#146)
Browse files Browse the repository at this point in the history
* Improving version detection + minor changes

* uniform version detection + refactoring

* solve nuclei updating everytime

- pdtm-api for nuclei also include the nuclei-templates data
- have to handle it different way than other tools
- add the os, arch, go version in fetchTool api

* Add Makefile & Update requrl syntax with fmt.Sprintf

* remove typo

* show change log for update commands

---------

Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
  • Loading branch information
ShubhamRasal and Mzack9999 authored May 19, 2023
1 parent 63b49f2 commit a35c35f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/projectdiscovery/pdtm
go 1.19

require (
github.com/charmbracelet/glamour v0.6.0
github.com/google/go-github v17.0.0+incompatible
github.com/projectdiscovery/goflags v0.1.8
github.com/projectdiscovery/gologger v1.1.10
Expand All @@ -18,7 +19,6 @@ require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/glamour v0.6.0 // indirect
github.com/cheggaaa/pb/v3 v3.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Options struct {
Version bool
ShowPath bool
DisableUpdateCheck bool
DisableChangeLog bool
}

// ParseOptions parses the command line flags provided by a user
Expand Down Expand Up @@ -89,6 +90,7 @@ func ParseOptions() *Options {
flagSet.BoolVar(&options.Version, "version", false, "show version of the project"),
flagSet.BoolVarP(&options.Verbose, "verbose", "v", false, "show verbose output"),
flagSet.BoolVarP(&options.NoColor, "no-color", "nc", false, "disable output content coloring (ANSI escape codes)"),
flagSet.BoolVarP(&options.DisableChangeLog, "dc", "disable-changelog", false, "disable release changelog in output"),
)

if err := flagSet.Parse(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (r *Runner) Run() error {
continue
}
if i, ok := utils.Contains(toolList, tool); ok {
if err := pkg.Update(r.options.Path, toolList[i]); err != nil {
if err := pkg.Update(r.options.Path, toolList[i], r.options.DisableChangeLog); err != nil {
if err == types.ErrIsUpToDate {
gologger.Info().Msgf("%s: %s", tool, err)
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestUpdateSameVersion(t *testing.T) {
assert.Nil(t, err)

// updating a tool to the same version should trigger an error
err = Update(pathBin, tool)
err = Update(pathBin, tool, true)
assert.Equal(t, "already up to date", err.Error())
}

Expand All @@ -89,6 +89,6 @@ func TestUpdateNonExistingTool(t *testing.T) {
defer os.RemoveAll(pathBin)

// updating non existing tool should error
err = Update(pathBin, tool)
err = Update(pathBin, tool, true)
assert.NotNil(t, err)
}
27 changes: 26 additions & 1 deletion pkg/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"os"
"strings"

"github.com/charmbracelet/glamour"
ospath "github.com/projectdiscovery/pdtm/pkg/path"
"github.com/projectdiscovery/pdtm/pkg/types"
"github.com/projectdiscovery/pdtm/pkg/version"
updateutils "github.com/projectdiscovery/utils/update"

"github.com/projectdiscovery/gologger"
)

// Update updates a given tool
func Update(path string, tool types.Tool) error {
func Update(path string, tool types.Tool, disableChangeLog bool) error {
if executablePath, exists := ospath.GetExecutablePath(path, tool.Name); exists {
if isUpToDate(tool, path) {
return types.ErrIsUpToDate
Expand All @@ -26,6 +28,9 @@ func Update(path string, tool types.Tool) error {
if err != nil {
return err
}
if !disableChangeLog {
showReleaseNotes(tool.Name)
}
gologger.Info().Msgf("updated %s to %s (%s)", tool.Name, version, au.BrightGreen("latest").String())
return nil
} else {
Expand All @@ -37,3 +42,23 @@ func isUpToDate(tool types.Tool, path string) bool {
v, err := version.ExtractInstalledVersion(tool, path)
return err == nil && strings.EqualFold(tool.Version, v)
}

func showReleaseNotes(toolname string) {
gh, err := updateutils.NewghReleaseDownloader(toolname)
if err != nil {
gologger.Fatal().Label("updater").Msgf("failed to download latest release got %v", err)
}
gh.SetToolName(toolname)
output := gh.Latest.GetBody()
// adjust colors for both dark / light terminal themes
r, err := glamour.NewTermRenderer(glamour.WithAutoStyle())
if err != nil {
gologger.Error().Msgf("markdown rendering not supported: %v", err)
}
if rendered, err := r.Render(output); err == nil {
output = rendered
} else {
gologger.Error().Msg(err.Error())
}
gologger.Print().Msgf("%v\n", output)
}
2 changes: 1 addition & 1 deletion pkg/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetUpdaterCallback(toolName string) func() {
gologger.Error().Msgf("failed to fetch details of %v skipping update: %v", toolName, err)
return
}
err = pkg.Update(dp, tool)
err = pkg.Update(dp, tool, false)
if err == types.ErrIsUpToDate {
gologger.Info().Msgf("%s: %s", toolName, err)
} else {
Expand Down

0 comments on commit a35c35f

Please sign in to comment.