2018-11-16 02:52:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-13 16:42:23 +00:00
|
|
|
"flag"
|
2019-03-14 07:00:47 +00:00
|
|
|
"fmt"
|
2018-11-16 02:52:55 +00:00
|
|
|
"log"
|
2019-04-10 13:05:12 +00:00
|
|
|
"math/rand"
|
2019-03-14 05:58:51 +00:00
|
|
|
"os"
|
2019-04-10 13:05:12 +00:00
|
|
|
"time"
|
2019-03-14 05:58:51 +00:00
|
|
|
|
2019-03-14 07:00:47 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-11-16 02:52:55 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
const usage = `stick - Shareable Git-powered notebooks
|
2019-03-20 01:30:33 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
Usage:
|
|
|
|
%s [<options...>] <command>
|
2019-04-10 13:05:12 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
Commands:
|
|
|
|
help Print help information
|
|
|
|
init Create a notebook
|
|
|
|
resolve Resolve merge conflicts
|
|
|
|
serve Serve shared notebooks
|
|
|
|
version Print version information
|
2019-03-14 05:58:51 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
Global options:
|
|
|
|
`
|
2019-03-13 11:46:33 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
var (
|
2020-10-29 01:06:34 +00:00
|
|
|
stick *server
|
2020-05-13 16:42:23 +00:00
|
|
|
)
|
2019-03-13 11:46:33 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
func main() {
|
2019-03-20 01:30:33 +00:00
|
|
|
configPath := ""
|
|
|
|
debug := false
|
2019-03-15 00:48:15 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
2019-03-15 00:48:15 +00:00
|
|
|
}
|
2020-05-13 16:42:23 +00:00
|
|
|
flag.BoolVar(&debug, "debug", false, "run in debug mode")
|
|
|
|
flag.StringVar(&configPath, "config", "~/.config/stick/stick.yml", "path to configuration file")
|
|
|
|
flag.Parse()
|
2019-03-15 00:48:15 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2019-03-14 07:00:47 +00:00
|
|
|
|
2020-05-13 16:42:23 +00:00
|
|
|
command := flag.Arg(0)
|
|
|
|
switch command {
|
|
|
|
case "init":
|
2020-10-29 01:06:34 +00:00
|
|
|
initialize(configPath, debug)
|
2020-05-13 16:42:23 +00:00
|
|
|
|
|
|
|
path := flag.Arg(1)
|
|
|
|
if path == "" {
|
|
|
|
fmt.Println("USAGE: stick init <path> [cloneurl]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneURL := flag.Arg(2)
|
|
|
|
|
2020-10-29 01:06:34 +00:00
|
|
|
_, err := initializeRepository(path, cloneURL)
|
2020-05-13 16:42:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to initialize notebook"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("notebook initialized")
|
|
|
|
case "resolve":
|
2020-10-29 01:06:34 +00:00
|
|
|
initialize(configPath, debug)
|
2020-05-13 16:42:23 +00:00
|
|
|
|
|
|
|
path := flag.Arg(1)
|
|
|
|
if path == "" {
|
|
|
|
fmt.Println("USAGE: stick resolve <path>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: initialize editor to resolve conflict
|
|
|
|
case "serve":
|
2020-10-29 01:06:34 +00:00
|
|
|
initialize(configPath, debug)
|
2020-05-13 16:42:23 +00:00
|
|
|
|
|
|
|
address := flag.Arg(1)
|
|
|
|
if stick.Config.Serve == "" && address == "" {
|
|
|
|
fmt.Println("USAGE: stick serve <[address]:[port]>")
|
|
|
|
return
|
|
|
|
} else if address != "" {
|
|
|
|
stick.Config.Serve = address
|
|
|
|
}
|
|
|
|
|
|
|
|
registerSignalHandlers()
|
|
|
|
|
|
|
|
log.Printf("serving shared notebooks on %s", stick.Config.Serve)
|
|
|
|
serveWeb()
|
|
|
|
default:
|
|
|
|
flag.Usage()
|
|
|
|
}
|
2019-03-14 05:58:51 +00:00
|
|
|
}
|