From 38c38357d71732567531e94969b8cc977fc9ca7e Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Mon, 5 Sep 2022 20:34:24 +0200 Subject: [PATCH] start to use a proper cli library --- cmd/owl/main.go | 33 ++++++++++ cmd/owl/web.go | 28 +++++++++ cmd/{owl-web => owl/web}/aliases_test.go | 4 +- cmd/{owl-web => owl/web}/handler.go | 2 +- cmd/{owl-web => owl/web}/multi_user_test.go | 4 +- cmd/{owl-web => owl/web}/post_test.go | 4 +- cmd/{owl-web => owl/web}/rss_test.go | 4 +- cmd/{owl-web/main.go => owl/web/server.go} | 63 ++++---------------- cmd/{owl-web => owl/web}/single_user_test.go | 4 +- cmd/{owl-web => owl/web}/webmention_test.go | 4 +- go.mod | 15 +++-- go.sum | 16 +++-- 12 files changed, 106 insertions(+), 75 deletions(-) create mode 100644 cmd/owl/main.go create mode 100644 cmd/owl/web.go rename cmd/{owl-web => owl/web}/aliases_test.go (99%) rename cmd/{owl-web => owl/web}/handler.go (99%) rename cmd/{owl-web => owl/web}/multi_user_test.go (98%) rename cmd/{owl-web => owl/web}/post_test.go (93%) rename cmd/{owl-web => owl/web}/rss_test.go (95%) rename cmd/{owl-web/main.go => owl/web/server.go} (55%) rename cmd/{owl-web => owl/web}/single_user_test.go (98%) rename cmd/{owl-web => owl/web}/webmention_test.go (98%) diff --git a/cmd/owl/main.go b/cmd/owl/main.go new file mode 100644 index 0000000..1ceeda0 --- /dev/null +++ b/cmd/owl/main.go @@ -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() +} diff --git a/cmd/owl/web.go b/cmd/owl/web.go new file mode 100644 index 0000000..8dc71d6 --- /dev/null +++ b/cmd/owl/web.go @@ -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) + }, +} diff --git a/cmd/owl-web/aliases_test.go b/cmd/owl/web/aliases_test.go similarity index 99% rename from cmd/owl-web/aliases_test.go rename to cmd/owl/web/aliases_test.go index 98c5dc4..2c6c161 100644 --- a/cmd/owl-web/aliases_test.go +++ b/cmd/owl/web/aliases_test.go @@ -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" diff --git a/cmd/owl-web/handler.go b/cmd/owl/web/handler.go similarity index 99% rename from cmd/owl-web/handler.go rename to cmd/owl/web/handler.go index a71c29e..38c855c 100644 --- a/cmd/owl-web/handler.go +++ b/cmd/owl/web/handler.go @@ -1,4 +1,4 @@ -package main +package web import ( "h4kor/owl-blogs" diff --git a/cmd/owl-web/multi_user_test.go b/cmd/owl/web/multi_user_test.go similarity index 98% rename from cmd/owl-web/multi_user_test.go rename to cmd/owl/web/multi_user_test.go index d55ab36..70c26a3 100644 --- a/cmd/owl-web/multi_user_test.go +++ b/cmd/owl/web/multi_user_test.go @@ -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" diff --git a/cmd/owl-web/post_test.go b/cmd/owl/web/post_test.go similarity index 93% rename from cmd/owl-web/post_test.go rename to cmd/owl/web/post_test.go index 67dbc0d..b3efe63 100644 --- a/cmd/owl-web/post_test.go +++ b/cmd/owl/web/post_test.go @@ -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" diff --git a/cmd/owl-web/rss_test.go b/cmd/owl/web/rss_test.go similarity index 95% rename from cmd/owl-web/rss_test.go rename to cmd/owl/web/rss_test.go index c1c2ff6..0967d7d 100644 --- a/cmd/owl-web/rss_test.go +++ b/cmd/owl/web/rss_test.go @@ -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" diff --git a/cmd/owl-web/main.go b/cmd/owl/web/server.go similarity index 55% rename from cmd/owl-web/main.go rename to cmd/owl/web/server.go index 57dbe10..dc90604 100644 --- a/cmd/owl-web/main.go +++ b/cmd/owl/web/server.go @@ -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 - Specify the repository to use. Defaults to '.'") - println("-port - Specify the port to use, Default is '8080'") - println("-user - 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 { diff --git a/cmd/owl-web/single_user_test.go b/cmd/owl/web/single_user_test.go similarity index 98% rename from cmd/owl-web/single_user_test.go rename to cmd/owl/web/single_user_test.go index e657a41..0dc61d8 100644 --- a/cmd/owl-web/single_user_test.go +++ b/cmd/owl/web/single_user_test.go @@ -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" diff --git a/cmd/owl-web/webmention_test.go b/cmd/owl/web/webmention_test.go similarity index 98% rename from cmd/owl-web/webmention_test.go rename to cmd/owl/web/webmention_test.go index 38b363f..0c9caa4 100644 --- a/cmd/owl-web/webmention_test.go +++ b/cmd/owl/web/webmention_test.go @@ -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" diff --git a/go.mod b/go.mod index 11cb084..910f421 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index d929445..1783953 100644 --- a/go.sum +++ b/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=