Skip to content

Commit

Permalink
Unified version control (#32)
Browse files Browse the repository at this point in the history
* Change version control approach

* Require an update only if the major/minor version has changed

* Define VERSION within each main file

Otherwise, it will not be updated during build

* Change the if statements

* Make minor improvements

* Test patch update
  • Loading branch information
kavir1698 authored Apr 4, 2024
1 parent 9efbc26 commit 06457f6
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 36 deletions.
20 changes: 20 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ before:

builds:
- id: "datasetIngestor"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}} # This will set the VERSION variable in the binary to the github tag
env:
- CGO_ENABLED=0
dir: ./cmd/datasetIngestor/
Expand All @@ -35,6 +39,10 @@ builds:
binary: datasetIngestor

- id: "datasetArchiver"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetArchiver/
Expand All @@ -48,6 +56,10 @@ builds:
binary: datasetArchiver

- id: "datasetRetriever"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetRetriever/
Expand All @@ -61,6 +73,10 @@ builds:
binary: datasetRetriever

- id: "datasetCleaner"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetCleaner/
Expand All @@ -74,6 +90,10 @@ builds:
binary: datasetCleaner

- id: "datasetGetProposal"
flags:
- -trimpath
ldflags:
- -s -w -X main.VERSION={{.Version}}
env:
- CGO_ENABLED=0
dir: ./cmd/datasetGetProposal/
Expand Down
3 changes: 2 additions & 1 deletion cmd/datasetArchiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/fatih/color"
)

var VERSION string

func main() {

var client = &http.Client{
Expand All @@ -34,7 +36,6 @@ func main() {

const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages"
const APP = "datasetArchiver"
const VERSION = "1.1.9"
var scanner = bufio.NewScanner(os.Stdin)

var APIServer string
Expand Down
3 changes: 2 additions & 1 deletion cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func isFlagPassed(name string) bool {
return found
}

var VERSION string

func main() {

var client = &http.Client{
Expand All @@ -60,7 +62,6 @@ func main() {

const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages"
const APP = "datasetCleaner"
const VERSION = "1.0.6"

var APIServer string
var env string
Expand Down
3 changes: 2 additions & 1 deletion cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/fatih/color"
)

var VERSION string

func main() {

var client = &http.Client{
Expand All @@ -32,7 +34,6 @@ func main() {

const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages"
const APP = "datasetGetProposal"
const VERSION = "1.1.6"

var APIServer string
var env string
Expand Down
4 changes: 2 additions & 2 deletions cmd/datasetIngestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func isFlagPassed(name string) bool {
return found
}

var VERSION string

func main() {
var tooLargeDatasets = 0
var emptyDatasets = 0
Expand Down Expand Up @@ -96,10 +98,8 @@ func main() {

const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages"
const APP = "datasetIngestor"
const VERSION = "1.1.31"

var scanner = bufio.NewScanner(os.Stdin)

var APIServer string
var RSYNCServer string
var env string
Expand Down
6 changes: 6 additions & 0 deletions cmd/datasetIngestor/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import (
"testing"
)

func init() {
os.Setenv("TEST_MODE", "true")
}

// TestMainOutput is a test function that verifies the output of the main function.
// It captures the stdout, runs the main function, and checks if the output contains the expected strings.
// This just checks if the main function prints the help message.
func TestMainOutput(t *testing.T) {
oldTestMode := "false"
defer os.Setenv("TEST_MODE", oldTestMode)
// Capture stdout
// The variable 'old' stores the original value of the standard output (os.Stdout).
old := os.Stdout
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetPublishData/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const RETRIEVELocation string = "/data/archiveManager/retrieve/"
const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages/#sec-5"

const APP = "datasetPublishData"
const VERSION = "1.0.1"

var APIServer string = PROD_API_SERVER
var RSYNCServer string = PROD_RSYNC_RETRIEVE_SERVER
Expand All @@ -63,6 +62,7 @@ var client = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: false}},
Timeout: 10 * time.Second}
var scanner = bufio.NewScanner(os.Stdin)
var VERSION string

type PageData struct {
Doi string
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetPublishDataRetrieve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const RETRIEVELocation string = "/data/archiveManager/retrieve/"
const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages/#sec-5"

const APP = "datasetPublishDataRetrieve"
const VERSION = "1.0.1"

var APIServer string = PROD_API_SERVER
var RSYNCServer string = PROD_RSYNC_RETRIEVE_SERVER
Expand All @@ -47,6 +46,7 @@ var client = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: false}},
Timeout: 10 * time.Second}
var scanner = bufio.NewScanner(os.Stdin)
var VERSION string

func main() {

Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetRetriever/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages/#sec-5"

// TODO Windows
const APP = "datasetRetriever"
const VERSION = "1.1.14"

var APIServer string = PROD_API_SERVER
var RSYNCServer string = PROD_RSYNC_RETRIEVE_SERVER
Expand All @@ -53,6 +52,7 @@ var client = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: false}},
Timeout: 10 * time.Second}
var scanner = bufio.NewScanner(os.Stdin)
var VERSION string

func main() {
// check input parameters
Expand Down
3 changes: 2 additions & 1 deletion cmd/waitForJobFinished/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Job struct {
JobStatusMessage string
}

var VERSION string

func main() {

var client = &http.Client{
Expand All @@ -38,7 +40,6 @@ func main() {

const MANUAL string = "http://melanie.gitpages.psi.ch/SciCatPages"
const APP = "waitForJobFinished"
const VERSION = "1.0.1"

var APIServer string
var env string
Expand Down
67 changes: 42 additions & 25 deletions datasetUtils/checkForNewVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package datasetUtils

import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"encoding/json"
version "github.com/mcuadros/go-version"
"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

var (
GitHubAPI = "https://api.github.com/repos/paulscherrerinstitute/scicat-cli/releases/latest"
GitHubAPI = "https://api.github.com/repos/paulscherrerinstitute/scicat-cli/releases/latest"
DeployLocation = "https://github.com/paulscherrerinstitute/scicat-cli/releases/download"
)

Expand All @@ -30,35 +30,39 @@ func fetchLatestVersion(client *http.Client) (string, error) {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return "", fmt.Errorf("got %s fetching %s", resp.Status, GitHubAPI)
}

var release Release
err = json.NewDecoder(resp.Body).Decode(&release)
if err != nil {
return "", err
}

return strings.TrimSpace(release.TagName), nil
}

// Make sure the version number is stripped of the 'v' prefix. That's required for `strconv.Atoi` to work.
func generateDownloadURL(deployLocation, latestVersion, osName string) string {
if strings.ToLower(osName) == "windows" {
return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.zip", deployLocation, latestVersion, latestVersion, strings.Title(osName))
}
return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.tar.gz", deployLocation, latestVersion, latestVersion, strings.Title(osName))
if strings.ToLower(osName) == "windows" {
return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.zip", deployLocation, latestVersion, latestVersion, strings.Title(osName))
}
return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.tar.gz", deployLocation, latestVersion, latestVersion, strings.Title(osName))
}

func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) error {
// avoid checking for new version in test mode
if os.Getenv("TEST_MODE") == "true" {
return nil
}
latestVersion, err := fetchLatestVersion(client)
if err != nil {
log.Printf("Can not find info about latest version for this program: %s\n", err)
return err
}

latestVersion = strings.TrimPrefix(latestVersion, "v")
_, err = strconv.Atoi(strings.Split(latestVersion, ".")[0])
if err != nil {
Expand All @@ -69,14 +73,25 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
log.Fatalf("Illegal version number:%v", VERSION)
}
log.Printf("Latest version: %s", latestVersion)

// Get the operating system name
osName := runtime.GOOS

// Generate the download URL
downloadURL := generateDownloadURL(DeployLocation, latestVersion, osName)

// Split the versions into parts
currentParts := strings.Split(VERSION, ".")
latestParts := strings.Split(latestVersion, ".")

// Convert the major and minor parts to integers
currentMajor, _ := strconv.Atoi(currentParts[0])
currentMinor, _ := strconv.Atoi(currentParts[1])
latestMajor, _ := strconv.Atoi(latestParts[0])
latestMinor, _ := strconv.Atoi(latestParts[1])

if version.Compare(latestVersion, VERSION, ">") {
// Notify an update if the version has changed
log.Println("You should upgrade to a newer version")
log.Println("Current Version: ", VERSION)
log.Println("Latest Version: ", latestVersion)
Expand All @@ -87,28 +102,30 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
} else {
log.Printf("Browser: %s\nCommand: curl -L -O %s; tar xzf scicat-cli_.%s_%s_x86_64.tar.gz; cd scicat-cli; chmod +x %s\n", downloadURL, downloadURL, latestVersion, strings.Title(osName), APP)
}

if interactiveFlag {
log.Print("Do you want to continue with current version (y/N) ? ")
continueyn, _ := userInput.ReadLine()
if continueyn != "y\n" {
return fmt.Errorf("Execution stopped, please update the program now.")
}
}
} else {
log.Println("Your version of this program is up-to-date")
}
if interactiveFlag && (latestMajor > currentMajor || latestMinor > currentMinor) {
log.Print("Do you want to continue with current version (y/N) ? ")
continueyn, err := userInput.ReadLine()
if err != nil {
return fmt.Errorf("failed to read user input: %v", err)
}
if strings.TrimSpace(continueyn) != "y" {
return fmt.Errorf("Execution stopped, please update the program now.")
}
}
return nil
}

// UserInput is an interface that defines a method to read a line of input. We use this so that we can test interactive mode.
type UserInput interface {
ReadLine() (string, error)
ReadLine() (string, error)
}

type StdinUserInput struct {}
type StdinUserInput struct{}

func (StdinUserInput) ReadLine() (string, error) {
reader := bufio.NewReader(os.Stdin)
return reader.ReadString('\n')
reader := bufio.NewReader(os.Stdin)
return reader.ReadString('\n')
}
13 changes: 11 additions & 2 deletions datasetUtils/checkForNewVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func TestCheckForNewVersion(t *testing.T) {
interactiveFlag: true,
userInput: "n\n",
},
{
name: "New path available, interactive mode",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v0.9.1"}`,
expectedLog: "You should upgrade to a newer version",
expectedError: nil,
interactiveFlag: true,
userInput: "y\n",
},
}

for _, tt := range tests {
Expand All @@ -144,8 +153,8 @@ func TestCheckForNewVersion(t *testing.T) {

// Call CheckForNewVersion
err := CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput})
if err != nil && err.Error() != tt.expectedLog {
t.Errorf("got error %v, want %v", err, tt.expectedLog)
if err != nil && err.Error() != tt.expectedError.Error() {
t.Errorf("got error %v, want %v", err, tt.expectedLog)
}

// Check the log output
Expand Down

0 comments on commit 06457f6

Please sign in to comment.