2024-02-19 08:16:27 +00:00
|
|
|
package update
|
|
|
|
|
|
|
|
import (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2024-02-19 08:16:27 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2024-02-19 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the update command.
|
|
|
|
var Command = &cli.Command{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "update the woodpecker-cli to the latest version",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "force update even if the latest version is already installed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: update,
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
func update(ctx context.Context, c *cli.Command) error {
|
2024-02-19 08:16:27 +00:00
|
|
|
log.Info().Msg("Checking for updates ...")
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
newVersion, err := CheckForUpdate(ctx, c.Bool("force"))
|
2024-02-19 08:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if newVersion == nil {
|
|
|
|
fmt.Println("You are using the latest version of woodpecker-cli")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("New version %s is available! Updating ...", newVersion.Version)
|
|
|
|
|
|
|
|
var tarFilePath string
|
2024-07-17 23:26:35 +00:00
|
|
|
tarFilePath, err = downloadNewVersion(ctx, newVersion.AssetURL)
|
2024-02-19 08:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("New version %s has been downloaded successfully! Installing ...", newVersion.Version)
|
|
|
|
|
|
|
|
binFile, err := extractNewVersion(tarFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("New version %s has been extracted to %s", newVersion.Version, binFile)
|
|
|
|
|
|
|
|
executablePathOrSymlink, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
executablePath, err := filepath.EvalSymlinks(executablePathOrSymlink)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(binFile, executablePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("woodpecker-cli has been updated to version %s successfully!", newVersion.Version)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|