Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit a098ed6

Browse files
committed
Add options to set custom cert and key path
1 parent af13180 commit a098ed6

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ $ mkcert -cert-file cert.pem -key-file key.pem localhost 127.0.0.1 more.hostname
3131
## Usage
3232

3333
```
34-
$ dirhttps -h
3534
Serving contents of current directory by HTTPS.
3635
3736
Usage:
3837
dirhttps [flags]
3938
4039
Flags:
4140
--cache Enable client side caching
41+
-c, --cert string Certificate file (default "/home/maetthu/.config/dirhttps/cert.pem")
4242
-h, --help help for dirhttps
43+
-k, --key string Key file (default "/home/maetthu/.config/dirhttps/key.pem")
4344
-l, --listen string Listen address (default ":8443")
4445
--no-cors Disable CORS handling
4546
--version version for dirhttps
4647
```
4748

48-
4949
### Examples
5050

5151
* Basic usage

cmd/root.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@ import (
1313
)
1414

1515
const (
16-
CertFilename = "cert.pem"
17-
KeyFilename = "key.pem"
16+
certFilename = "cert.pem"
17+
keyFilename = "key.pem"
1818
)
1919

20+
var configDir string
21+
2022
var rootCmd = &cobra.Command{
2123
Use: "dirhttps",
2224
Short: "Serving contents of current directory by HTTPS.",
2325
Args: cobra.NoArgs,
2426
Version: fmt.Sprintf("%s -- %s", version.Version, version.Commit),
2527
Run: func(cmd *cobra.Command, args []string) {
26-
home, err := homedir.Dir()
28+
29+
certFile, err := cmd.Flags().GetString("cert")
2730

2831
if err != nil {
29-
log.Fatalf("Error determining home directory: %s", err)
32+
log.Fatalf("Error parsing cert flag: %s", err)
3033
}
3134

32-
configDir := fmt.Sprintf("%s/.config/dirhttps", home)
35+
keyFile, err := cmd.Flags().GetString("key")
36+
37+
if err != nil {
38+
log.Fatalf("Error parsing key flag: %s", err)
39+
}
3340

34-
certFile := filepath.Join(configDir, CertFilename)
35-
keyFile := filepath.Join(configDir, KeyFilename)
3641
certAvailable := true
3742

3843
if _, err := os.Stat(certFile); err != nil {
@@ -46,10 +51,9 @@ var rootCmd = &cobra.Command{
4651
if !certAvailable {
4752
log.Printf("Necessary certificate and/or key file not found.")
4853
log.Fatalf(
49-
"Place a certificate \"%s\" and a key file \"%s\" to %s",
50-
CertFilename,
51-
KeyFilename,
52-
configDir,
54+
"Store a certificate to \"%s\" and a key file to \"%s\" or provide the --cert and --key flags",
55+
certFilename,
56+
keyFilename,
5357
)
5458
}
5559

@@ -62,7 +66,7 @@ var rootCmd = &cobra.Command{
6266
addr, err := cmd.Flags().GetString("listen")
6367

6468
if err != nil {
65-
log.Fatalf("Error parsing listen flage: %s", err)
69+
log.Fatalf("Error parsing listen flag: %s", err)
6670
}
6771

6872
log.Printf("Listening for HTTPS connections on %s", addr)
@@ -87,6 +91,7 @@ var rootCmd = &cobra.Command{
8791
},
8892
}
8993

94+
// Execute runs root command
9095
func Execute() {
9196
if err := rootCmd.Execute(); err != nil {
9297
fmt.Println(err)
@@ -95,7 +100,20 @@ func Execute() {
95100
}
96101

97102
func init(){
103+
home, err := homedir.Dir()
104+
105+
if err != nil {
106+
log.Fatalf("Error determining home directory: %s", err)
107+
}
108+
109+
configDir := fmt.Sprintf("%s/.config/dirhttps", home)
110+
111+
certFile := filepath.Join(configDir, certFilename)
112+
keyFile := filepath.Join(configDir, keyFilename)
113+
98114
rootCmd.Flags().StringP("listen", "l", ":8443", "Listen address")
115+
rootCmd.Flags().StringP("cert", "c", certFile, "Certificate file")
116+
rootCmd.Flags().StringP("key", "k", keyFile, "Key file")
99117
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
100118
rootCmd.Flags().Bool("cache", false, "Enable client side caching")
101119
}

0 commit comments

Comments
 (0)