|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "log" |
6 | 7 | "os" |
7 | 8 | "os/exec" |
8 | | - "regexp" |
9 | 9 |
|
10 | 10 | "github.com/fatih/color" |
11 | 11 |
|
12 | 12 | "github.com/spf13/cobra" |
| 13 | + |
| 14 | + // "github.com/pkg/errors" |
| 15 | + |
| 16 | + "encoding/json" |
| 17 | + "io/ioutil" |
| 18 | + "net/http" |
| 19 | + "time" |
13 | 20 | ) |
14 | 21 |
|
15 | 22 | // configCmd represents the config command |
@@ -38,27 +45,95 @@ func init() { |
38 | 45 |
|
39 | 46 | } |
40 | 47 |
|
| 48 | +func showConfig() { |
| 49 | + color.Blue("Current configuration") |
| 50 | + fmt.Println("DODPATH = ", os.Getenv("DODPATH")) |
| 51 | + pwd, err := os.Getwd() |
| 52 | + if err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + fmt.Println("Current Working Directory = ", pwd) |
| 57 | + fmt.Println("DevOpsDays web directory = ", webdir) |
| 58 | + color.Blue("Checking your config...") |
| 59 | + checkHugo() |
| 60 | + checkGit() |
| 61 | + fmt.Print("Press 'Enter' to continue...") |
| 62 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 63 | +} |
| 64 | + |
41 | 65 | // checkHugo tests whether or not a compatible version of the Hugo static site generator is instealled. |
42 | 66 | // |
43 | 67 | // Currently, the list of supported versions is hard-coded using the `supportedVersions` variable, but this should be moved elsewhere eventually. |
44 | 68 | func checkHugo() { |
45 | | - supportedVersions := map[string]bool{"0.36.1": true, "0.37": true, "0.37.1": true} |
46 | | - out, err := exec.Command("hugo", "version").Output() |
| 69 | + // supportedVersions := map[string]bool{"0.36.1": true, "0.37": true, "0.37.1": true} |
| 70 | + currentThemeVersion, currentHugoVersion, currentCliVersion, err := getCurrentVersions() |
47 | 71 | if err != nil { |
48 | 72 | log.Fatal(err) |
49 | 73 | } |
50 | | - s := string(out[:]) |
51 | | - re := regexp.MustCompile(`[0-9]+(\.[0-9]+)*`) |
52 | | - hugoVersion := re.FindString(s) |
53 | | - if supportedVersions[hugoVersion] { |
54 | | - fmt.Println("\u2713 Hugo version", hugoVersion, "is okay") |
| 74 | + fmt.Println("\u2713 Supported cli version is ", currentCliVersion) |
| 75 | + |
| 76 | + if currentHugoVersion == getHugoVersion() { |
| 77 | + color.Green("\u2713 Hugo version %s is okay", getHugoVersion()) |
55 | 78 | } else { |
56 | | - fmt.Println("\u2717 Hugo version", hugoVersion, "is incompatible.") |
57 | | - fmt.Println("Supported Versions are:") |
58 | | - for k := range supportedVersions { |
59 | | - fmt.Println(k) |
60 | | - } |
| 79 | + color.Red("\u2717 Hugo version %s is incompatible.", getHugoVersion()) |
| 80 | + color.Red("Supported Version is: %s", currentHugoVersion) |
61 | 81 | } |
| 82 | + |
| 83 | + if currentThemeVersion == getThemeVersion() { |
| 84 | + color.Green("\u2713 Theme version %s is okay", getThemeVersion()) |
| 85 | + } else { |
| 86 | + color.Red("\u2717 Theme version %s is incompatible.", getThemeVersion()) |
| 87 | + color.Red("Supported Version is: %s", currentThemeVersion) |
| 88 | + } |
| 89 | + |
| 90 | + if currentCliVersion == Version { |
| 91 | + color.Green("\u2713 devopsdays-cli version %s is okay", Version) |
| 92 | + } else { |
| 93 | + color.Red("\u2717 devopsdays-cli version %s is incompatible.", Version) |
| 94 | + color.Red("Supported Version is: %s", currentCliVersion) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +type currentVersion struct { |
| 99 | + ThemeVersion string `json:"theme_version"` |
| 100 | + HugoVersion string `json:"hugo_version"` |
| 101 | + DevopsdaysCliVersion string `json:"devopsdays_cli_version"` |
| 102 | +} |
| 103 | + |
| 104 | +// getCurrentVersions returns the supported version of the devopsdays Hugo theme, hugo, and devopsdays-cli |
| 105 | +func getCurrentVersions() (themeVersion string, hugoVersion string, devopsdaysCliVersion string, err error) { |
| 106 | + |
| 107 | + url := "https://rawgit.com/devopsdays/devopsdays-web/master/metadata.json" |
| 108 | + |
| 109 | + devopsdaysClient := http.Client{ |
| 110 | + Timeout: time.Second * 2, // Maximum of 2 secs |
| 111 | + } |
| 112 | + |
| 113 | + req, err := http.NewRequest(http.MethodGet, url, nil) |
| 114 | + if err != nil { |
| 115 | + log.Fatal(err) |
| 116 | + } |
| 117 | + |
| 118 | + req.Header.Set("User-Agent", "devopsdays-cli") |
| 119 | + |
| 120 | + res, getErr := devopsdaysClient.Do(req) |
| 121 | + if getErr != nil { |
| 122 | + log.Fatal(getErr) |
| 123 | + } |
| 124 | + |
| 125 | + body, readErr := ioutil.ReadAll(res.Body) |
| 126 | + if readErr != nil { |
| 127 | + log.Fatal(readErr) |
| 128 | + } |
| 129 | + |
| 130 | + myCurrentVersion := currentVersion{} |
| 131 | + jsonErr := json.Unmarshal(body, &myCurrentVersion) |
| 132 | + if jsonErr != nil { |
| 133 | + log.Fatal(jsonErr) |
| 134 | + } |
| 135 | + |
| 136 | + return myCurrentVersion.ThemeVersion, myCurrentVersion.HugoVersion, myCurrentVersion.DevopsdaysCliVersion, nil |
62 | 137 | } |
63 | 138 |
|
64 | 139 | func checkGit() { |
|
0 commit comments