summaryrefslogtreecommitdiff
path: root/cmd/updater/versionCmd.go
blob: 4b700ef1e7c1bbc4de22b6d9e354d6c44be5db19 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"

	"github.com/algorand/go-algorand/util/s3"
)

var (
	destFile        string
	versionBucket   string
	specificVersion uint64
	semanticOutput  bool
)

func init() {
	versionCmd.AddCommand(checkCmd)
	versionCmd.AddCommand(getCmd)

	checkCmd.Flags().StringVarP(&versionBucket, "bucket", "b", "", "S3 bucket containing updates.")
	checkCmd.Flags().BoolVarP(&semanticOutput, "semantic", "s", false, "Human readable semantic version output.")

	getCmd.Flags().StringVarP(&destFile, "outputFile", "o", "", "Path for downloaded file (required).")
	getCmd.Flags().StringVarP(&versionBucket, "bucket", "b", "", "S3 bucket containing updates.")
	getCmd.Flags().Uint64VarP(&specificVersion, "version", "v", 0, "Specific version to download.")
	getCmd.MarkFlagRequired("outputFile")
}

var versionCmd = &cobra.Command{
	Use:   "ver",
	Short: "Get latest version number or download latest version",
	Long:  `Allows checking the version of the latest update and downloading it `,
	Run: func(cmd *cobra.Command, args []string) {
		// Fall back
		cmd.HelpFunc()(cmd, args)
	},
}

var checkCmd = &cobra.Command{
	Use:   "check",
	Short: "Check the latest version available",
	Long:  `Check the latest version available`,
	Run: func(cmd *cobra.Command, args []string) {
		if versionBucket == "" {
			versionBucket = s3.GetS3ReleaseBucket()
		}
		s3Session, err := s3.MakeS3SessionForDownloadWithBucket(versionBucket)
		if err != nil {
			exitErrorf("Error creating s3 session %s\n", err.Error())
		} else {
			version, _, err := s3Session.GetLatestUpdateVersion(channel)
			if err != nil {
				exitErrorf("Error getting latest version from s3 %s\n", err.Error())
			}

			if version == 0 {
				fmt.Fprintf(os.Stderr, "no updates found for channel '%s'\n", channel)
				os.Exit(1)
			}

			if semanticOutput {
				major, minor, patch, err := s3.GetVersionPartsFromVersion(version)
				if err != nil {
					exitErrorf("Problem converting '%d' to a semantic version string: %v", version, err)
				}
				fmt.Fprintf(os.Stdout, "%d.%d.%d\n", major, minor, patch)
			} else {
				fmt.Fprintf(os.Stdout, "%d\n", version)
			}
		}
	},
}

var getCmd = &cobra.Command{
	Use:   "get",
	Short: "Download the latest version available",
	Long:  `Download the latest version available`,
	Run: func(cmd *cobra.Command, args []string) {
		if versionBucket == "" {
			versionBucket = s3.GetS3ReleaseBucket()
		}
		s3Session, err := s3.MakeS3SessionForDownloadWithBucket(versionBucket)
		if err != nil {
			exitErrorf("Error creating s3 session %s\n", err.Error())
		} else {
			version, name, err := s3Session.GetUpdateVersion(channel, specificVersion)
			if err != nil {
				exitErrorf("Error getting latest version from s3 %s\n", err.Error())
			}
			if version == 0 {
				exitErrorf("No updates found\n")
			}

			file, err := os.Create(os.ExpandEnv(destFile))
			defer file.Close()
			if err != nil {
				exitErrorf("Error creating output file: %s\n", err.Error())
			}

			err = s3Session.DownloadFile(name, file)
			if err != nil {
				exitErrorf("Error downloading file: %s\n", err.Error())
				// script should delete the file.
			}
		}
	},
}