-
Notifications
You must be signed in to change notification settings - Fork 12
/
bbs_flags.go
86 lines (69 loc) · 1.8 KB
/
bbs_flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package commands
import (
"errors"
"fmt"
"os"
"net/url"
"github.com/spf13/cobra"
)
var (
bbsUrl string
)
// errors
var (
errMissingBBSUrl = errors.New("BBS URL not set. Please specify one with the '--bbsURL' flag or the 'BBS_URL' environment variable.")
)
func AddBBSFlags(cmd *cobra.Command) {
AddTLSFlags(cmd)
cmd.Flags().StringVar(&bbsUrl, "bbsURL", "", "URL of BBS server to target [environment variable equivalent: BBS_URL]")
cmd.PreRunE = BBSPrehook
}
func BBSPrehook(cmd *cobra.Command, args []string) error {
if err := setBBSFlags(cmd, args); err != nil {
return err
}
return tlsPreHook(cmd, args)
}
func setBBSFlags(cmd *cobra.Command, args []string) error {
var err, returnErr error
if bbsUrl == "" {
bbsUrl = os.Getenv("BBS_URL")
}
if bbsUrl == "" {
returnErr = NewCFDotValidationError(cmd, errMissingBBSUrl)
return returnErr
}
Config.BBSUrl = bbsUrl
var parsedURL *url.URL
if parsedURL, err = url.Parse(Config.BBSUrl); err != nil {
returnErr = NewCFDotValidationError(
cmd,
fmt.Errorf(
"The value '%s' is not a valid BBS URL. Please specify one with the '--bbsURL' flag or the 'BBS_URL' environment variable.",
Config.BBSUrl),
)
return returnErr
}
if parsedURL.Scheme != "https" {
returnErr = NewCFDotValidationError(
cmd,
fmt.Errorf(
"The URL '%s' does not have an 'https' scheme. Please "+
"specify one with the '--bbsURL' flag or the 'BBS_URL' environment "+
"variable.", Config.BBSUrl),
)
return returnErr
}
return nil
}
func validateReadableFile(cmd *cobra.Command, filename, filetype string) error {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return NewCFDotValidationError(
cmd,
fmt.Errorf("%s file '%s' doesn't exist or is not readable: %s", filetype, filename, err.Error()),
)
}
return nil
}