-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add self update Signed-off-by: Felipe Zipitria <[email protected]> * chore: add version and self-update cmds Signed-off-by: Felipe Zipitria <[email protected]> * Update cmd/self_update.go Co-authored-by: Max Leske <[email protected]> --------- Signed-off-by: Felipe Zipitria <[email protected]> Co-authored-by: Max Leske <[email protected]>
- Loading branch information
Showing
9 changed files
with
302 additions
and
7 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,37 @@ | ||
// Copyright 2022 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/coreruleset/go-ftw/internal/updater" | ||
) | ||
|
||
// NewSelfUpdateCommand represents the self-update command | ||
func NewSelfUpdateCommand(version string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "self-update", | ||
Short: "Performs self-update", | ||
Long: "Checks GitHub releases for the latest version of this command. If a new version is available, " + | ||
"it will fetch it and replace this binary.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if version == "dev" { | ||
log.Info().Msg("You are running a development version, skipping self-update") | ||
return nil | ||
} | ||
newVersion, err := updater.Updater(version, "") | ||
if err != nil { | ||
return err | ||
} | ||
if newVersion != "" { | ||
log.Info().Msgf("Updated to version %s", newVersion) | ||
} else { | ||
log.Info().Msg("No updates available") | ||
} | ||
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2022 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type selfUpdateTestSuite struct { | ||
suite.Suite | ||
tempDir string | ||
executablePath string | ||
} | ||
|
||
func (s *selfUpdateTestSuite) SetupTest() { | ||
var err error | ||
s.tempDir, err = os.MkdirTemp("", "self-update-tests") | ||
s.Require().NoError(err) | ||
|
||
s.executablePath = path.Join(s.tempDir, "ftw") | ||
err = os.WriteFile(s.executablePath, []byte("Fake Binary"), fs.ModePerm) | ||
s.Require().NoError(err) | ||
} | ||
|
||
func (s *selfUpdateTestSuite) TearDownTest() { | ||
err := os.RemoveAll(s.tempDir) | ||
s.Require().NoError(err) | ||
} | ||
|
||
// Do not run test suite until there is a new release with the "version" command | ||
func TestRunSelfUpdateTestSuite(t *testing.T) { | ||
suite.Run(t, new(selfUpdateTestSuite)) | ||
} | ||
|
||
// func (s *selfUpdateTestSuite) TestSelfUpdateDev() { | ||
// _, err := updater.Updater("v0.0.0-dev", s.executablePath) | ||
// s.Require().NoError(err) | ||
//} | ||
// | ||
//func (s *selfUpdateTestSuite) TestSelfUpdateBigVersion() { | ||
// newVersion, err := updater.Updater("v10000.1.1", s.executablePath) | ||
// s.Require().NoError(err) | ||
// s.Equal("v10000.1.1", newVersion) | ||
//} | ||
// | ||
//func (s *selfUpdateTestSuite) TestSelfUpdateWithExecutablePath() { | ||
// newVersion, err := updater.Updater("v1.3.7", s.executablePath) | ||
// s.Require().NoError(err) | ||
// s.NotEmpty(newVersion) | ||
// | ||
// s.FileExists(s.executablePath, "The executable should exist") | ||
// contents, err := os.ReadFile(s.executablePath) | ||
// s.Require().NoError(err) | ||
// s.NotContains(string(contents), "Fake Binary", "The executable should be replaced") | ||
// | ||
// var out, stderr bytes.Buffer | ||
// | ||
// cmd := exec.Command(s.executablePath, "version") | ||
// cmd.Stdout = &out | ||
// cmd.Stderr = &stderr | ||
// | ||
// err = cmd.Run() | ||
// if err == nil { | ||
// versionString := fmt.Sprintf("ftw %s", newVersion) | ||
// s.Contains(out.String(), versionString) | ||
// } else { | ||
// s.Equal("exit status 1", err.Error()) | ||
// oldBinaryWithUnsupportedVersionFlagError := "Error: unknown command \"version\" for \"go-ftw\"\nRun 'go-ftw --help' for usage.\n" | ||
// s.Equal(oldBinaryWithUnsupportedVersionFlagError, stderr.String()) | ||
// } | ||
//} |
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,34 @@ | ||
// Copyright 2023 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/coreruleset/go-ftw/internal/updater" | ||
) | ||
|
||
func NewVersionCommand(version string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number of go-ftw", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("go-ftw", version) | ||
// do not run when in CI (e.g. GitHub Actions) | ||
if os.Getenv("CI") != "true" { | ||
latest, err := updater.LatestVersion() | ||
if err != nil { | ||
log.Error().Err(err).Msg("Failed to check for updates") | ||
} else if latest != "" { | ||
fmt.Println("Latest version is:", latest) | ||
fmt.Println("Run 'go-ftw self-update' to update") | ||
} | ||
} | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.