-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.go
More file actions
51 lines (42 loc) · 1.15 KB
/
update.go
File metadata and controls
51 lines (42 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"fmt"
"log"
"github.com/creativeprojects/go-selfupdate"
"github.com/spf13/cobra"
"github.com/codesphere-cloud/cs-go/pkg/cs"
)
type UpdateCmd struct {
cmd *cobra.Command
}
func (c *UpdateCmd) RunE(_ *cobra.Command, args []string) error {
return SelfUpdate()
}
func AddUpdateCmd(rootCmd *cobra.Command) {
update := UpdateCmd{
cmd: &cobra.Command{
Use: "update",
Short: "Update Codesphere CLI",
Long: `Updates the Codesphere CLI to the latest release from GitHub.`,
},
}
rootCmd.AddCommand(update.cmd)
update.cmd.RunE = update.RunE
}
func SelfUpdate() error {
currentVersion := cs.Version()
latest, err := selfupdate.UpdateSelf(context.Background(), currentVersion, selfupdate.ParseSlug("codesphere-cloud/cs-go"))
if err != nil {
return fmt.Errorf("update failed: %w", err)
}
if latest.LessOrEqual(currentVersion) {
log.Println("Current cs CLI is the latest version", currentVersion)
return nil
}
log.Println("Successfully updated to version", latest.Version())
log.Println("Release notes:\n", latest.ReleaseNotes)
return nil
}