-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (120 loc) · 4.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"github.com/exlskills/eocsutil/config"
"github.com/exlskills/eocsutil/eocs"
"github.com/exlskills/eocsutil/eocsuri"
"github.com/exlskills/eocsutil/extfmt"
"github.com/exlskills/eocsutil/ghserver"
"github.com/exlskills/eocsutil/gitutils"
"github.com/exlskills/eocsutil/mdutils"
"github.com/exlskills/eocsutil/olx"
"github.com/exlskills/eocsutil/pdf"
"gopkg.in/alecthomas/kingpin.v2"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
serveGitHubHooks = kingpin.Command("serve-gh-hook", "Serve GitHub course update hooks to auto-update courses")
convertCmd = kingpin.Command("convert", "Convert a course from one supported format to another")
convertForce = convertCmd.Flag("force", "Ignore if the destination already exists, just write the files/folders").Default("false").Bool()
convertFromFormat = convertCmd.Flag("from-format", "The source format to convert from").Required().String()
convertFromURI = convertCmd.Flag("from-uri", "The URI to the source").Required().String()
convertToFormat = convertCmd.Flag("to-format", "The destination format to convert to").Required().String()
convertToURI = convertCmd.Flag("to-uri", "The destination URI").Required().String()
verifyCmd = kingpin.Command("verify", "Check that a course conforms to a supported format")
verifyFormat = verifyCmd.Flag("format", "The format to which the course should conform to").Default("eocs").String()
verifyURI = verifyCmd.Flag("uri", "The URI of the source of the course").Required().String()
)
var Log = config.Cfg().GetLogger()
func init() {
extfmt.RegisterExtFmt("eocs", eocs.NewEOCSFormat())
extfmt.RegisterExtFmt("olx", olx.NewOLXExtFmt())
extfmt.RegisterExtFmt("pdf", pdf.NewPDFExtFmt())
}
func main() {
// Do this to ensure that our on exit traps work
run()
time.Sleep(time.Second * 1)
}
func run() {
defer mdutils.GracefulTeardown()
go gracefulShutdown()
rand.Seed(time.Now().UnixNano())
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("0.1").Author("EXL Inc.")
kingpin.CommandLine.Help = "EXL Open Courseware Standard - Utilities"
switch kingpin.Parse() {
case "convert":
if strings.HasPrefix(*convertToURI, "mongodb://") {
err := eocs.NewEOCSFormat().Push(*convertFromURI, *convertToURI, false)
if err != nil {
Log.Errorf("Course push failed: %s", err.Error())
return
}
return
}
// This is non-MongoDB only flow below !!!!!!!!!!!! See eocs/eocs.go for MongoDB logic
Log.Info("Importing course for conversion ...")
ir, err := getExtFmtF(*convertFromFormat).Import(verifyAndCleanURIF(*convertFromURI))
if err != nil {
Log.Errorf("Course import failed with: %s", err.Error())
return
}
Log.Info("Successfully imported course %s for conversion, now exporting ...", ir.GetDisplayName())
err = gitutils.SetCourseComponentsTimestamps(*convertFromURI, ir)
if err != nil {
Log.Info("Git reader failed - Timestamps will not be assigned")
}
err = getExtFmtF(*convertToFormat).Export(ir, verifyAndCleanURIF(*convertToURI), *convertForce)
if err != nil {
Log.Errorf("Course export failed with: %s", err.Error())
return
}
Log.Infof("Successfully exported course: %s", ir.GetDisplayName())
case "verify":
Log.Info("Importing course for verification ...")
ir, err := getExtFmtF(*verifyFormat).Import(verifyAndCleanURIF(*verifyURI))
if err != nil {
Log.Errorf("Course import verification failed with: %s", err.Error())
return
}
Log.Infof("Successfully verified course: %s", ir.GetDisplayName())
return
case "serve-gh-hook":
Log.Info("Serve GitHub Hooks ...")
ghserver.ServeGH()
return
default:
Log.Fatal("Unknown command")
}
}
func getExtFmtF(key string) extfmt.ExtFmt {
impl := extfmt.GetImplementation(key)
if impl == nil {
Log.Fatalf(fmt.Sprintf("invalid format type: %s", key))
}
return impl
}
func verifyAndCleanURIF(uri string) string {
var err error
uri, err = eocsuri.VerifyAndClean(uri)
if err != nil {
Log.Fatalf("invalid uri: %s", err.Error())
}
return uri
}
func gracefulShutdown() {
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
signal.Notify(s, syscall.SIGTERM)
go func() {
<-s
fmt.Println("Shutting down gracefully.")
mdutils.GracefulTeardown()
os.Exit(0)
}()
}