github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/scripts/ci/update-version-list/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/thoas/go-funk"
    12  
    13  	"github.com/ActiveState/cli/internal/condition"
    14  	"github.com/ActiveState/cli/internal/constants"
    15  	"github.com/ActiveState/cli/internal/fileutils"
    16  	"github.com/ActiveState/cli/internal/httputil"
    17  	"github.com/ActiveState/cli/internal/updater"
    18  )
    19  
    20  // Where the master version file lives on S3.
    21  const S3PrefixURL = "https://state-tool.s3.amazonaws.com/"
    22  const S3Bucket = "update/state/"
    23  const VersionsJson = "versions.json"
    24  
    25  // Valid channels to update the master version file with.
    26  var ValidChannels = []string{constants.BetaChannel, constants.ReleaseChannel}
    27  
    28  func init() {
    29  	if !condition.OnCI() {
    30  		// Allow testing with artifacts produced by `state run generate-test-update`
    31  		ValidChannels = append(ValidChannels, "test-channel")
    32  	}
    33  }
    34  
    35  func main() {
    36  	if len(os.Args) != 2 {
    37  		log.Fatalf("Usage: %s <build-dir>", os.Args[0])
    38  	}
    39  
    40  	// Fetch the current master list from S3.
    41  	versions := []updater.AvailableUpdate{}
    42  	fmt.Printf("Fetching master %s file from S3\n", VersionsJson)
    43  	bytes, err := httputil.Get(S3PrefixURL + S3Bucket + VersionsJson)
    44  	if err != nil {
    45  		log.Fatalf("Failed to fetch file: %s", err.Error())
    46  	}
    47  	err = json.Unmarshal(bytes, &versions)
    48  	if err != nil {
    49  		log.Fatalf("Failed to decode JSON: %s", err.Error())
    50  	}
    51  
    52  	// Find info.json files to add to the master list and add them.
    53  	updated := false
    54  	buildDir := os.Args[1]
    55  	fmt.Printf("Searching for info.json files in %s\n", buildDir)
    56  	files, err := fileutils.ListDir(buildDir, false)
    57  	if err != nil {
    58  		log.Fatalf("Failed to search %s: %s", buildDir, err.Error())
    59  	}
    60  	for _, file := range files {
    61  		if file.Name() != "info.json" {
    62  			continue
    63  		}
    64  		channel := strings.Split(file.RelativePath(), string(filepath.Separator))[0]
    65  		if !funk.Contains(ValidChannels, channel) {
    66  			continue
    67  		}
    68  		fmt.Printf("Found %s\n", file.RelativePath())
    69  		bytes, err := fileutils.ReadFile(file.Path())
    70  		if err != nil {
    71  			log.Fatalf("Unable to read file: %s", err.Error())
    72  		}
    73  		info := updater.AvailableUpdate{}
    74  		err = json.Unmarshal(bytes, &info)
    75  		if err != nil {
    76  			log.Fatalf("Unable to decode JSON: %s", err.Error())
    77  		}
    78  		info.Path = S3PrefixURL + S3Bucket + info.Path // convert relative path to full URL
    79  		versions = append(versions, info)
    80  		updated = true
    81  	}
    82  
    83  	if !updated {
    84  		fmt.Println("No updates found.")
    85  		return
    86  	}
    87  
    88  	// Write the updated list to disk. The s3-deployer script should pick it up and upload it.
    89  	localVersionsJson := filepath.Join(buildDir, VersionsJson)
    90  	fmt.Printf("Writing updated %s locally to %s\n", VersionsJson, localVersionsJson)
    91  	bytes, err = json.Marshal(versions)
    92  	if err != nil {
    93  		log.Fatalf("Failed to encode JSON: %s", err.Error())
    94  	}
    95  	err = fileutils.WriteFile(localVersionsJson, bytes)
    96  	if err != nil {
    97  		log.Fatalf("Failed to write file: %s", err.Error())
    98  	}
    99  }