|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
4 | 6 | "os" |
| 7 | + "strings" |
5 | 8 |
|
6 | 9 | "github.com/aohorodnyk/uid" |
| 10 | + "github.com/blang/semver/v4" |
| 11 | + "github.com/google/go-github/v42/github" |
7 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
8 | 13 | ) |
9 | 14 |
|
| 15 | +func GetCML(version string) string { |
| 16 | + // default latest if unset |
| 17 | + if version == "" { |
| 18 | + client := github.NewClient(nil) |
| 19 | + release, _, err := client.Repositories.GetLatestRelease(context.Background(), "iterative", "cml") |
| 20 | + if err != nil { |
| 21 | + // GitHub API failed |
| 22 | + return getNPMCML("@dvcorg/cml") |
| 23 | + } |
| 24 | + for _, asset := range release.Assets { |
| 25 | + if *asset.Name == "cml-linux" { |
| 26 | + return getGHCML(*asset.BrowserDownloadURL) |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + // handle "v"semver |
| 31 | + if strings.HasPrefix(version, "v") { |
| 32 | + ver, err := semver.Make(version[1:]) |
| 33 | + if err == nil { |
| 34 | + return getSemverCML(ver) |
| 35 | + } |
| 36 | + } |
| 37 | + // handle semver |
| 38 | + ver, err := semver.Make(version) |
| 39 | + if err == nil { |
| 40 | + return getSemverCML(ver) |
| 41 | + } |
| 42 | + // user must know best, npm install <string> |
| 43 | + if version != "" { |
| 44 | + return getNPMCML(version) |
| 45 | + } |
| 46 | + // original fallback, some error has forced this |
| 47 | + return getNPMCML("@dvcorg/cml") |
| 48 | +} |
| 49 | + |
| 50 | +func getGHCML(v string) string { |
| 51 | + return fmt.Sprintf(`sudo mkdir -p /opt/cml/ |
| 52 | +sudo curl --location --url %s --output /opt/cml/cml-linux |
| 53 | +sudo chmod +x /opt/cml/cml-linux |
| 54 | +sudo ln -s /opt/cml/cml-linux /usr/bin/cml`, v) |
| 55 | +} |
| 56 | + |
| 57 | +func getNPMCML(v string) string { |
| 58 | + npmCML := "sudo npm config set user 0 && sudo npm install --global %s" |
| 59 | + return fmt.Sprintf(npmCML, v) |
| 60 | +} |
| 61 | + |
| 62 | +func getSemverCML(sv semver.Version) string { |
| 63 | + directDownloadVersion, _ := semver.ParseRange(">=0.10.0") |
| 64 | + if directDownloadVersion(sv) { |
| 65 | + client := github.NewClient(nil) |
| 66 | + release, _, err := client.Repositories.GetReleaseByTag(context.Background(), "iterative", "cml", "v"+sv.String()) |
| 67 | + if err == nil { |
| 68 | + for _, asset := range release.Assets { |
| 69 | + if *asset.Name == "cml-linux" { |
| 70 | + return getGHCML(*asset.BrowserDownloadURL) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + // npm install |
| 76 | + return getNPMCML("@dvcorg/cml@v" + sv.String()) |
| 77 | +} |
| 78 | + |
10 | 79 | func MachinePrefix(d *schema.ResourceData) string { |
11 | 80 | prefix := "" |
12 | 81 | if _, hasMachine := d.GetOk("machine"); hasMachine { |
|
0 commit comments