Skip to content

Commit faae1d2

Browse files
committed
Add tests for version, etc
Config command now checks for supported versions via the json file on the repo Fixes #124 Fixes #123 Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent bec5466 commit faae1d2

4 files changed

Lines changed: 117 additions & 13 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"cSpell.words": [
33
"devopsdays",
4+
"dimiro",
5+
"fatih",
6+
"mattn",
7+
"mattstratton",
48
"toml"
59
]
610
}

commands/config.go

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package commands
22

33
import (
4+
"bufio"
45
"fmt"
56
"log"
67
"os"
78
"os/exec"
8-
"regexp"
99

1010
"github.com/fatih/color"
1111

1212
"github.com/spf13/cobra"
13+
14+
// "github.com/pkg/errors"
15+
16+
"encoding/json"
17+
"io/ioutil"
18+
"net/http"
19+
"time"
1320
)
1421

1522
// configCmd represents the config command
@@ -38,27 +45,95 @@ func init() {
3845

3946
}
4047

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+
4165
// checkHugo tests whether or not a compatible version of the Hugo static site generator is instealled.
4266
//
4367
// Currently, the list of supported versions is hard-coded using the `supportedVersions` variable, but this should be moved elsewhere eventually.
4468
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()
4771
if err != nil {
4872
log.Fatal(err)
4973
}
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())
5578
} 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)
6181
}
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
62137
}
63138

64139
func checkGit() {

commands/prompt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func mainPrompt() (err error) {
2222
"Show a speaker",
2323
"Show a talk",
2424
"Test",
25+
"Check your configuration",
26+
"Show the version",
2527
"Quit the application",
2628
},
2729
}
@@ -42,6 +44,11 @@ func mainPrompt() (err error) {
4244
showTalkPrompt("", "")
4345
case "Test":
4446
model.ShowEvent()
47+
case "Check your configuration":
48+
showConfig()
49+
case "Show the version":
50+
showVersion()
51+
4552
}
4653
}
4754

commands/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package commands
22

3+
// cSpell:disable
34
import (
5+
"bufio"
46
"fmt"
57
"log"
8+
"os"
69
"os/exec"
710
"path/filepath"
811
"regexp"
@@ -11,6 +14,8 @@ import (
1114
"github.com/spf13/cobra"
1215
)
1316

17+
// cSpell:enable
18+
1419
var (
1520
// Version is the current version of the devopsdays-cli tool. Unless set elsewhere, it is referred to as "master"
1621
Version = "master"
@@ -35,6 +40,15 @@ var versionCmd = &cobra.Command{
3540
},
3641
}
3742

43+
func showVersion() {
44+
fmt.Println("devopsdays-cli version: ", Version)
45+
fmt.Println("devopsdays-cli build: ", Build)
46+
fmt.Println("hugo version: ", getHugoVersion())
47+
fmt.Println("devopsdays-theme version: ", getThemeVersion())
48+
fmt.Print("Press 'Enter' to continue...")
49+
bufio.NewReader(os.Stdin).ReadBytes('\n')
50+
}
51+
3852
func getHugoVersion() (hugoVersion string) {
3953
out, err := exec.Command("hugo", "version").Output()
4054
if err != nil {
@@ -58,6 +72,10 @@ func getThemeVersion() (themeVersion string) {
5872

5973
}
6074

75+
func getCliVersion() (cliVersion string) {
76+
return Version
77+
}
78+
6179
// Theme represents the currently installed devopsdays-theme Hugo theme.
6280
// The field Version represents the current version.
6381
type Theme struct {

0 commit comments

Comments
 (0)