Skip to content

Commit 7cd6997

Browse files
committed
Add check for webdir validity
Fixes #32 Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent 3ff924a commit 7cd6997

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

cmd/root.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"bytes"
77
"fmt"
88
"os"
9-
"strings"
109

10+
"github.com/devopsdays/devopsdays-cli/helpers/paths"
1111
"github.com/dimiro1/banner"
1212
"github.com/mattn/go-colorable"
1313
"github.com/spf13/cobra"
@@ -23,7 +23,7 @@ var myBanner = `
2323
`
2424

2525
// webdir is the path to the source files for the Hugo website
26-
var webdir = setWebdir()
26+
var webdir = paths.GetWebdir()
2727

2828
// const webdir = "/Users/mattstratton/src/devopsdays-web"
2929

@@ -90,17 +90,25 @@ func initConfig() {
9090
}
9191
}
9292

93-
func setWebdir() string {
94-
if os.Getenv("DODPATH") == "" {
95-
pwd, err := os.Getwd()
96-
if err != nil {
97-
fmt.Println(err)
98-
os.Exit(1)
99-
}
100-
return pwd
101-
}
102-
s := os.Getenv("DODPATH")
103-
s = strings.TrimSuffix(s, "/")
104-
s = strings.TrimSuffix(s, "\\")
105-
return s
106-
}
93+
// func setWebdir() string {
94+
// if os.Getenv("DODPATH") == "" {
95+
// pwd, err := os.Getwd()
96+
// if err != nil {
97+
// fmt.Println(err)
98+
// os.Exit(1)
99+
// }
100+
// return pwd
101+
// }
102+
// s := os.Getenv("DODPATH")
103+
// s = strings.TrimSuffix(s, "/")
104+
// s = strings.TrimSuffix(s, "\\")
105+
// return s
106+
// }
107+
108+
// func validateWebDir(webdir string) bool {
109+
// filename := "a-nonexistent-file"
110+
// if _, err := os.Stat(filename); os.IsNotExist(err) {
111+
// fmt.Printf("file does not exist")
112+
// }
113+
114+
// }

helpers/paths/webdir.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package paths
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"strings"
78
)
89

@@ -14,11 +15,27 @@ func GetWebdir() string {
1415
fmt.Println(err)
1516
os.Exit(1)
1617
}
17-
return pwd
18+
if validateWebDir(pwd) != true {
19+
return pwd
20+
}
21+
fmt.Println("devopsdays-web directory is invalid")
22+
os.Exit(1)
1823
}
1924
s := os.Getenv("DODPATH")
2025
s = strings.TrimSuffix(s, "/")
2126
s = strings.TrimSuffix(s, "\\")
27+
if validateWebDir(s) != true {
28+
fmt.Println("devopsdays directory is invalid")
29+
os.Exit(1)
30+
}
2231
return s
32+
}
2333

34+
func validateWebDir(webdir string) bool {
35+
36+
themeFile := filepath.Join(webdir, "themes", "devopsdays-theme", "theme.toml")
37+
if _, err := os.Stat(themeFile); os.IsNotExist(err) {
38+
return false
39+
}
40+
return true
2441
}

0 commit comments

Comments
 (0)