-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add update command to handle updating mtc-cli (#1)
- Loading branch information
Showing
4 changed files
with
129 additions
and
12 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,49 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"runtime" | ||
|
||
"github.com/creativeprojects/go-selfupdate" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func update(version string) error { | ||
latest, found, err := selfupdate.DetectLatest(context.Background(), selfupdate.ParseSlug("morethancertified/mtc-cli")) | ||
if err != nil { | ||
return fmt.Errorf("error occurred while detecting version: %w", err) | ||
} | ||
if !found { | ||
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH) | ||
} | ||
|
||
if latest.LessOrEqual(version) { | ||
log.Printf("Current version (%s) is the latest", version) | ||
return nil | ||
} | ||
|
||
exe, err := selfupdate.ExecutablePath() | ||
if err != nil { | ||
return errors.New("could not locate executable path") | ||
} | ||
if err := selfupdate.UpdateTo(context.Background(), latest.AssetURL, latest.AssetName, exe); err != nil { | ||
return fmt.Errorf("error occurred while updating binary: %w", err) | ||
} | ||
log.Printf("Successfully updated to version %s", latest.Version()) | ||
return nil | ||
} | ||
|
||
var updateCmd = &cobra.Command{ | ||
Use: "update", | ||
Short: "Update the mtc-cli to the latest version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
update(Version) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(updateCmd) | ||
} |
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