start to use a proper cli library
This commit is contained in:
parent
4aaa14e30c
commit
38c38357d7
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var repoPath string
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "owl",
|
||||
Short: "Owl Blogs is a not so static blog generator",
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
// },
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&repoPath, "repo", ".", "Path to the repository to use.")
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
Execute()
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
web "h4kor/owl-blogs/cmd/owl/web"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var port int
|
||||
var unsafe bool
|
||||
var user string
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(webCmd)
|
||||
|
||||
rootCmd.PersistentFlags().IntVar(&port, "port", 8080, "Port to use")
|
||||
rootCmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "Allow unsafe html")
|
||||
rootCmd.PersistentFlags().StringVar(&user, "user", "", "Start server in single user mode.")
|
||||
}
|
||||
|
||||
var webCmd = &cobra.Command{
|
||||
Use: "web",
|
||||
Short: "Start the web server",
|
||||
Long: `Start the web server`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
web.StartServer(repoPath, port, unsafe, user)
|
||||
},
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package web
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
|
@ -1,8 +1,8 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
|
@ -1,7 +1,7 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
|
@ -1,7 +1,7 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package web
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
|
@ -34,63 +34,20 @@ func SingleUserRouter(repo *owl.Repository) http.Handler {
|
|||
return router
|
||||
}
|
||||
|
||||
func main() {
|
||||
println("owl web server")
|
||||
println("Parameters")
|
||||
println("-repo <repo> - Specify the repository to use. Defaults to '.'")
|
||||
println("-port <port> - Specify the port to use, Default is '8080'")
|
||||
println("-user <name> - Start server in single user mode.")
|
||||
println("-unsafe - Allow unsafe html.")
|
||||
var repoName string
|
||||
var port int
|
||||
var singleUserName string
|
||||
var allowRawHTML bool = false
|
||||
for i, arg := range os.Args[0:len(os.Args)] {
|
||||
if arg == "-port" {
|
||||
if i+1 >= len(os.Args) {
|
||||
println("-port requires a port number")
|
||||
os.Exit(1)
|
||||
}
|
||||
port, _ = strconv.Atoi(os.Args[i+1])
|
||||
}
|
||||
if arg == "-repo" {
|
||||
if i+1 >= len(os.Args) {
|
||||
println("-repo requires a repopath")
|
||||
os.Exit(1)
|
||||
}
|
||||
repoName = os.Args[i+1]
|
||||
}
|
||||
if arg == "-user" {
|
||||
if i+1 >= len(os.Args) {
|
||||
println("-user requires a username")
|
||||
os.Exit(1)
|
||||
}
|
||||
singleUserName = os.Args[i+1]
|
||||
}
|
||||
if arg == "-unsafe" {
|
||||
allowRawHTML = true
|
||||
}
|
||||
}
|
||||
if repoName == "" {
|
||||
repoName = "."
|
||||
}
|
||||
if port == 0 {
|
||||
port = 8080
|
||||
}
|
||||
|
||||
func StartServer(repoPath string, port int, unsafe bool, user string) {
|
||||
var repo owl.Repository
|
||||
var err error
|
||||
if singleUserName != "" {
|
||||
if user != "" {
|
||||
println("Single user mode")
|
||||
println("Repository:", repoName)
|
||||
println("User:", singleUserName)
|
||||
repo, err = owl.OpenSingleUserRepo(repoName, singleUserName)
|
||||
println("Repository:", repoPath)
|
||||
println("User:", user)
|
||||
repo, err = owl.OpenSingleUserRepo(repoPath, user)
|
||||
} else {
|
||||
println("Multi user mode")
|
||||
println("Repository:", repoName)
|
||||
repo, err = owl.OpenRepository(repoName)
|
||||
println("Repository:", repoPath)
|
||||
repo, err = owl.OpenRepository(repoPath)
|
||||
}
|
||||
repo.SetAllowRawHtml(allowRawHTML)
|
||||
repo.SetAllowRawHtml(unsafe)
|
||||
|
||||
if err != nil {
|
||||
println("Error opening repository: ", err.Error())
|
||||
|
@ -98,7 +55,7 @@ func main() {
|
|||
}
|
||||
|
||||
var router http.Handler
|
||||
if singleUserName == "" {
|
||||
if user == "" {
|
||||
println("Multi user mode Router used")
|
||||
router = Router(&repo)
|
||||
} else {
|
|
@ -1,8 +1,8 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
owl "h4kor/owl-blogs"
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
|
@ -1,8 +1,8 @@
|
|||
package main_test
|
||||
package web_test
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
main "h4kor/owl-blogs/cmd/owl-web"
|
||||
main "h4kor/owl-blogs/cmd/owl/web"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
15
go.mod
15
go.mod
|
@ -3,9 +3,14 @@ module h4kor/owl-blogs
|
|||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||
github.com/yuin/goldmark v1.4.13 // indirect
|
||||
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
github.com/julienschmidt/httprouter v1.3.0
|
||||
github.com/spf13/cobra v1.5.0
|
||||
github.com/yuin/goldmark v1.4.13
|
||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
)
|
||||
|
|
16
go.sum
16
go.sum
|
@ -1,11 +1,19 @@
|
|||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
|
||||
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
|
||||
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
|
||||
github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0=
|
||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
|
||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
|
|
Loading…
Reference in New Issue