-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from coreruleset/self-update
feat: add self-updater
- Loading branch information
Showing
9 changed files
with
384 additions
and
22 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,55 @@ | ||
// Copyright 2022 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/coreruleset/crs-toolchain/internal/updater" | ||
) | ||
|
||
// selfUpdateCmd represents the self-update command | ||
var selfUpdateCmd = createSelfUpdateCommand() | ||
|
||
func init() { | ||
buildSelfUpdateCommand() | ||
} | ||
|
||
func createSelfUpdateCommand() *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 get it and replace this binary.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
version := "dev" | ||
if rootCmd.Version != "" { | ||
version = rootCmd.Version | ||
} | ||
newVersion, err := updater.Updater(version, "") | ||
if err != nil { | ||
return err | ||
} | ||
if newVersion != "" { | ||
logger.Info().Msgf("Updated to version %s", newVersion) | ||
} else { | ||
logger.Info().Msg("No updates available") | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func buildSelfUpdateCommand() { | ||
rootCmd.AddCommand(selfUpdateCmd) | ||
} | ||
|
||
func rebuildSelfUpdateCommand() { | ||
if selfUpdateCmd != nil { | ||
selfUpdateCmd.Parent().RemoveCommand(selfUpdateCmd) | ||
} | ||
|
||
selfUpdateCmd = createSelfUpdateCommand() | ||
buildSelfUpdateCommand() | ||
} |
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,82 @@ | ||
// Copyright 2022 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/coreruleset/crs-toolchain/internal/updater" | ||
) | ||
|
||
type selfUpdateTestSuite struct { | ||
suite.Suite | ||
tempDir string | ||
executablePath string | ||
} | ||
|
||
func (s *selfUpdateTestSuite) SetupTest() { | ||
var err error | ||
rebuildSelfUpdateCommand() | ||
s.tempDir, err = os.MkdirTemp("", "self-update-tests") | ||
s.NoError(err) | ||
|
||
s.executablePath = path.Join(s.tempDir, "crs-toolchain") | ||
err = os.WriteFile(s.executablePath, []byte("Fake Binary"), fs.ModePerm) | ||
s.NoError(err) | ||
} | ||
|
||
func (s *selfUpdateTestSuite) TearDownTest() { | ||
err := os.RemoveAll(s.tempDir) | ||
s.NoError(err) | ||
} | ||
|
||
func TestRunSelfUpdateTestSuite(t *testing.T) { | ||
suite.Run(t, new(selfUpdateTestSuite)) | ||
} | ||
|
||
func (s *selfUpdateTestSuite) TestSelfUpdateDev() { | ||
_, err := updater.Updater("v0.0.0-dev", s.executablePath) | ||
s.NoError(err) | ||
} | ||
|
||
func (s *selfUpdateTestSuite) TestSelfUpdateBigVersion() { | ||
newVersion, err := updater.Updater("v10000.1.1", s.executablePath) | ||
s.NoError(err) | ||
s.Equal("v10000.1.1", newVersion) | ||
} | ||
|
||
func (s *selfUpdateTestSuite) TestSelfUpdateWithExecutablePath() { | ||
newVersion, err := updater.Updater("v1.3.7", s.executablePath) | ||
s.NoError(err) | ||
s.NotEmpty(newVersion) | ||
|
||
s.FileExists(s.executablePath, "The executable should exist") | ||
contents, err := os.ReadFile(s.executablePath) | ||
s.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("crs-toolchain version %s", newVersion) | ||
s.Contains(versionString, out.String()) | ||
} else { | ||
s.Equal("exit status 1", err.Error()) | ||
oldBinaryWithUnsupportedVersionFlagError := "Error: unknown command \"version\" for \"crs-toolchain\"\nRun 'crs-toolchain --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,36 @@ | ||
// Copyright 2023 OWASP Core Rule Set Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/coreruleset/crs-toolchain/internal/updater" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number of crs-toolchain", | ||
Long: `All software has versions. This is crs-toolchain's`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("crs-toolchain", rootCmd.Version) | ||
// do not run when in CI (e.g. GitHub Actions) | ||
if os.Getenv("CI") != "true" { | ||
latest, err := updater.LatestVersion() | ||
if err != nil { | ||
logger.Error().Err(err).Msg("Failed to check for updates") | ||
} else if latest != "" { | ||
fmt.Println("Latest version is:", latest) | ||
fmt.Println("Run 'crs-toolchain 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.