2019-04-19 21:56:39 +00:00
|
|
|
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-12-02 12:33:47 +00:00
|
|
|
"errors"
|
2019-04-19 21:56:39 +00:00
|
|
|
"fmt"
|
2020-12-02 12:33:47 +00:00
|
|
|
"net/http"
|
2020-12-04 15:28:02 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2020-12-02 12:33:47 +00:00
|
|
|
|
2019-04-19 21:56:39 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-11-12 12:08:56 +00:00
|
|
|
"github.com/baez90/goveal/internal/app/server"
|
|
|
|
|
2019-04-19 21:56:39 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-04 15:28:02 +00:00
|
|
|
host string
|
|
|
|
port uint16
|
|
|
|
openBrowser bool
|
|
|
|
serveCmd = &cobra.Command{
|
2019-04-19 21:56:39 +00:00
|
|
|
Use: "serve",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Short: "",
|
|
|
|
Long: ``,
|
2020-12-02 12:33:47 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
2020-12-04 15:28:02 +00:00
|
|
|
var srv *server.HTTPServer
|
|
|
|
srv, err = server.NewHTTPServer(server.Config{
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
MarkdownPath: args[0],
|
|
|
|
RevealParams: ¶ms,
|
|
|
|
})
|
2019-04-19 21:56:39 +00:00
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error while setting up server: %v", err)
|
2020-12-02 12:33:47 +00:00
|
|
|
return
|
2019-04-19 21:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
listenUrl := fmt.Sprintf("http://%s/", srv.ListenAddress())
|
|
|
|
log.Infof("Going to listen on %s", listenUrl)
|
2019-04-19 21:56:39 +00:00
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
if openBrowser {
|
|
|
|
log.Info("Opening browser...")
|
|
|
|
openBrowserInBackground(listenUrl)
|
2020-12-02 12:33:47 +00:00
|
|
|
}
|
2019-04-20 00:28:27 +00:00
|
|
|
|
2020-12-04 15:28:02 +00:00
|
|
|
if err = srv.Serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2020-12-02 12:33:47 +00:00
|
|
|
log.Errorf("Error while running serve command: %v", err)
|
2019-04-19 21:56:39 +00:00
|
|
|
}
|
2020-12-02 12:33:47 +00:00
|
|
|
return nil
|
2019-04-19 21:56:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
|
|
|
|
serveCmd.Flags().StringVar(&host, "host", "localhost", "host the CLI should listen on")
|
|
|
|
serveCmd.Flags().Uint16Var(&port, "port", 2233, "port the CLI should listen on")
|
2020-12-04 15:28:02 +00:00
|
|
|
serveCmd.Flags().BoolVar(&openBrowser, "open-browser", true, "if the browser should be opened at the URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
func openBrowserInBackground(url string) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
err = exec.Command("xdg-open", url).Start()
|
|
|
|
case "windows":
|
|
|
|
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
|
|
|
case "darwin":
|
|
|
|
err = exec.Command("open", url).Start()
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported platform")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
2019-04-19 21:56:39 +00:00
|
|
|
}
|